File: timer_notify.py

package info (click to toggle)
python-xknx 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,064 kB
  • sloc: python: 40,895; javascript: 8,556; makefile: 32; sh: 12
file content (68 lines) | stat: -rw-r--r-- 2,198 bytes parent folder | download | duplicates (2)
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
60
61
62
63
64
65
66
67
68
"""
Module for Serialization and Deserialization of KNX Timer Notify.

This frame shall be sent during secure KNXnet/IP multicast group communication
to keep the multicast group member's timer values synchronized.
"""

from __future__ import annotations

from typing import Final

from xknx.exceptions import CouldNotParseKNXIP

from .body import KNXIPBody
from .knxip_enum import KNXIPServiceType


class TimerNotify(KNXIPBody):
    """Representation of a KNX Timer Notify."""

    SERVICE_TYPE = KNXIPServiceType.TIMER_NOTIFY
    LENGTH: Final = 30

    def __init__(
        self,
        timer_value: int = 0,
        serial_number: bytes = bytes(6),
        message_tag: bytes = bytes(2),
        message_authentication_code: bytes = bytes(16),
    ) -> None:
        """Initialize TimerNotify object."""
        self.timer_value = timer_value
        self.serial_number = serial_number
        self.message_tag = message_tag
        self.message_authentication_code = message_authentication_code

    def calculated_length(self) -> int:
        """Get length of KNX/IP body."""
        return TimerNotify.LENGTH

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        if len(raw) != TimerNotify.LENGTH:
            raise CouldNotParseKNXIP("TimerNotify has wrong length")
        self.timer_value = int.from_bytes(raw[:6], "big")
        self.serial_number = raw[6:12]
        self.message_tag = raw[12:14]
        self.message_authentication_code = raw[14:]
        return TimerNotify.LENGTH

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return (
            self.timer_value.to_bytes(6, "big")
            + self.serial_number
            + self.message_tag
            + self.message_authentication_code
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return (
            "<TimerNotify "
            f'timer_value="{self.timer_value}" '
            f'serial_number="{self.serial_number.hex()}" '
            f'message_tag="{self.message_tag.hex()}" '
            f'message_authentication_code="{self.message_authentication_code.hex()}" />'
        )