1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
"""
Module for Serialization and Deserialization of KNX Session Status.
A SESSION_STATUS may be sent by the KNXnet/IP secure server to the KNXnet/IP secure client
or by the KNXnet/IP secure client to the KNXnet/IP secure server in any stage of the
secure session handshake to indicate an error condition or status information.
"""
from __future__ import annotations
from typing import Final
from xknx.exceptions import CouldNotParseKNXIP
from .body import KNXIPBody
from .knxip_enum import KNXIPServiceType, SecureSessionStatusCode
class SessionStatus(KNXIPBody):
"""Representation of a KNX Session Status."""
SERVICE_TYPE = KNXIPServiceType.SESSION_STATUS
LENGTH: Final = 2
def __init__(
self,
status: SecureSessionStatusCode = SecureSessionStatusCode.STATUS_KEEPALIVE,
) -> None:
"""Initialize SessionStatus object."""
self.status = status
def calculated_length(self) -> int:
"""Get length of KNX/IP body."""
return SessionStatus.LENGTH
def from_knx(self, raw: bytes) -> int:
"""Parse/deserialize from KNX/IP raw data."""
if len(raw) != SessionStatus.LENGTH:
raise CouldNotParseKNXIP("SessionStatus has wrong length")
try:
self.status = SecureSessionStatusCode(raw[0])
except ValueError as err:
raise CouldNotParseKNXIP(
f"SessionStatus has unsupported status code: {raw[0]}"
) from err
return SessionStatus.LENGTH
def to_knx(self) -> bytes:
"""Serialize to KNX/IP raw data."""
return bytes(
(
self.status.value,
0x00, # reserved
)
)
def __repr__(self) -> str:
"""Return object as readable string."""
return f'<SessionStatus status="{self.status}" />'
|