File: address.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (389 lines) | stat: -rw-r--r-- 12,672 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
Module for serialization/deserialization and handling of KNX addresses.

The module can handle:

* individual addresses of devices.
* (logical) group addresses.
* xknx internal group addresses.

The module supports all different writings of group addresses:

* 3rn level: "1/2/3"
* 2nd level: "1/2"
* Free format: "123"
"""

from __future__ import annotations

from abc import ABC, abstractmethod
from enum import Enum
from re import compile as re_compile
from typing import ClassVar, Union

from xknx.exceptions import CouldNotParseAddress
from xknx.typing import Self

GroupAddressableType = Union["GroupAddress", str, int]
IndividualAddressableType = Union["IndividualAddress", str, int]
InternalGroupAddressableType = Union["InternalGroupAddress", str]
DeviceAddressableType = GroupAddressableType | InternalGroupAddressableType
DeviceGroupAddress = Union["GroupAddress", "InternalGroupAddress"]


INVALID_PREFIX_MESSAGE = "Invalid prefix for internal group address"


def parse_device_group_address(
    address: DeviceAddressableType,
) -> DeviceGroupAddress:
    """Parse an Addressable type to GroupAddress or InternalGroupAddress."""
    try:
        group_address = GroupAddress(address)  # type: ignore[arg-type]  # InternalGroupAddress will raise
    except CouldNotParseAddress as ex:
        if isinstance(address, str | InternalGroupAddress):
            try:
                return InternalGroupAddress(address)
            except CouldNotParseAddress as internal_ex:
                # prefer to raise original exception from GroupAddress
                if internal_ex.message != INVALID_PREFIX_MESSAGE:
                    raise internal_ex
        raise ex

    if group_address.raw == 0:
        raise CouldNotParseAddress(address, "Broadcast address invalid for devices")
    return group_address


class BaseAddress(ABC):
    """Base class for all knx address types."""

    __slots__ = ("raw",)

    raw: int

    @abstractmethod
    def __init__(
        self, address: IndividualAddressableType | GroupAddressableType
    ) -> None:
        """Initialize Address instance. To be implemented in derived class."""

    @classmethod
    def from_knx(cls: type[Self], raw: bytes) -> Self:
        """Parse/deserialize from KNX/IP raw data."""
        return cls(int.from_bytes(raw, "big"))

    def to_knx(self) -> bytes:
        """
        Serialize to KNX/IP raw data.

        Returns a bytes object with length of 2 from the raw value.
        """
        return int.to_bytes(self.raw, 2, "big")

    def __eq__(self, other: object | None) -> bool:
        """
        Implement the equal operator.

        Returns `True` if we check against the same subclass and the
        raw Value matches.
        """
        return isinstance(other, self.__class__) and self.raw == other.raw

    def __hash__(self) -> int:
        """Hash Address so it can be used as dict key."""
        return hash((self.__class__, self.raw))


class IndividualAddress(BaseAddress):
    """Class for handling KNX individual addresses."""

    __slots__ = ()

    MAX_AREA = 15
    MAX_MAIN = 15
    MAX_LINE = 255
    ADDRESS_RE = re_compile(
        r"^(?P<area>\d{1,2})\.(?P<main>\d{1,2})\.(?P<line>\d{1,3})$"
    )

    def __init__(self, address: IndividualAddressableType) -> None:
        """Initialize IndividualAddress class."""
        if isinstance(address, int):
            self.raw = address
        elif isinstance(address, IndividualAddress):
            self.raw = address.raw
        elif isinstance(address, str):
            if address.isdigit():
                self.raw = int(address)
            else:
                self.raw = self.__string_to_int(address)
        else:
            raise CouldNotParseAddress(address, message="Invalid type")

        if not 0 <= self.raw <= 65535:
            raise CouldNotParseAddress(
                address, message="Address out of range (0..65535)"
            )

    def __string_to_int(self, address: str) -> int:
        """
        Parse `address` as string to an integer and do some simple checks.

        Returns the integer representation of `address` if all checks are valid:
        * string matches against the regular expression
        * area, main and line are inside its range

        In any other case, we raise an `CouldNotParseAddress` exception.
        """
        match = self.ADDRESS_RE.match(address)
        if not match:
            raise CouldNotParseAddress(address, message="Invalid format")
        area = int(match.group("area"))
        main = int(match.group("main"))
        line = int(match.group("line"))
        if area > self.MAX_AREA:
            raise CouldNotParseAddress(
                address, message=f"Area part out of range (0..{self.MAX_AREA})"
            )
        if main > self.MAX_MAIN:
            raise CouldNotParseAddress(
                address, message=f"Line part out of range (0..{self.MAX_MAIN})"
            )
        if line > self.MAX_LINE:
            raise CouldNotParseAddress(
                address, message=f"Device part out of range (0..{self.MAX_LINE})"
            )
        return (area << 12) + (main << 8) + line

    @property
    def area(self) -> int:
        """Return area part of individual address."""
        return (self.raw >> 12) & self.MAX_AREA

    @property
    def main(self) -> int:
        """Return main part of individual address."""
        return (self.raw >> 8) & self.MAX_MAIN

    @property
    def line(self) -> int:
        """Return line part of individual address."""
        return self.raw & self.MAX_LINE

    @property
    def is_device(self) -> bool:
        """Return `True` if this address is a valid device address."""
        return self.line != 0

    @property
    def is_line(self) -> bool:
        """Return `True` if this address is a valid line address."""
        return not self.is_device

    def __str__(self) -> str:
        """Return object as in KNX notation (e.g. '1.2.3')."""
        return f"{self.area}.{self.main}.{self.line}"

    def __repr__(self) -> str:
        """Return this object as parsable string."""
        return f'IndividualAddress("{self}")'


class GroupAddressType(Enum):
    """
    Possible types of `GroupAddress`.

    KNX knows three types of group addresses:
    * FREE, a integer or hex representation
    * SHORT, a representation like '1/123', without middle groups
    * LONG, a representation like '1/2/34', with middle groups
    """

    FREE = 0
    SHORT = 2
    LONG = 3


class GroupAddress(BaseAddress):
    """Class for handling KNX group addresses."""

    __slots__ = ()

    # overridden by XKNX class on initialization to have consistent global string representation
    address_format: ClassVar[GroupAddressType] = GroupAddressType.LONG

    MAX_MAIN = 31
    MAX_MIDDLE = 7
    MAX_SUB_LONG = 255
    MAX_SUB_SHORT = 2047
    MAX_FREE = 65535

    ADDRESS_RE = re_compile(
        r"^(?P<main>\d{1,2})(/(?P<middle>\d{1,2}))?/(?P<sub>\d{1,4})$"
    )

    def __init__(self, address: GroupAddressableType) -> None:
        """Initialize GroupAddress class."""
        if isinstance(address, int):
            self.raw = address
        elif isinstance(address, GroupAddress):
            self.raw = address.raw
        elif isinstance(address, str):
            if address.isdigit():
                self.raw = int(address)
            else:
                self.raw = self.__string_to_int(address)
        else:
            raise CouldNotParseAddress(address, message="Invalid type")

        if not 0 <= self.raw <= 65535:
            raise CouldNotParseAddress(
                address, message="Address out of range (0..65535)"
            )

    def __string_to_int(self, address: str) -> int:
        """
        Parse `address` as string to an integer and do some simple checks.

        Returns the integer representation of `address` if all checks are valid:
        * string matches against the regular expression
        * main, middle and sub are inside its range

        In any other case, we raise an `CouldNotParseAddress` exception.
        """
        match = self.ADDRESS_RE.match(address)
        if not match:
            raise CouldNotParseAddress(address, message="Invalid format")
        main = int(match.group("main"))
        middle = (
            int(match.group("middle")) if match.group("middle") is not None else None
        )
        sub = int(match.group("sub"))
        if main > self.MAX_MAIN:
            raise CouldNotParseAddress(
                address, message=f"Main group out of range (0..{self.MAX_MAIN})"
            )
        if middle is not None:
            if middle > self.MAX_MIDDLE:
                raise CouldNotParseAddress(
                    address, message=f"Middle group out of range (0..{self.MAX_MIDDLE})"
                )
            if sub > self.MAX_SUB_LONG:
                raise CouldNotParseAddress(
                    address, message=f"Sub group out of range (0..{self.MAX_SUB_LONG})"
                )
        elif sub > self.MAX_SUB_SHORT:
            raise CouldNotParseAddress(
                address, message=f"Sub group out of range (0..{self.MAX_SUB_SHORT})"
            )
        return (
            (main << 11) + (middle << 8) + sub
            if middle is not None
            else (main << 11) + sub
        )

    @property
    def main(self) -> int | None:
        """
        Return the main group part as an integer.

        Works only if the group dont uses `GroupAddressType.FREE`, returns `None`
        in any other case.
        """
        return (
            (self.raw >> 11) & self.MAX_MAIN
            if self.address_format != GroupAddressType.FREE
            else None
        )

    @property
    def middle(self) -> int | None:
        """
        Return the middle group part as an integer.

        Works only if the group uses `GroupAddressType.LONG`, returns `None` in
        any other case.
        """
        return (
            (self.raw >> 8) & self.MAX_MIDDLE
            if self.address_format == GroupAddressType.LONG
            else None
        )

    @property
    def sub(self) -> int:
        """
        Return the sub group part as an integer.

        Works with any `GroupAddressType`, as we always have sub groups.
        """
        if self.address_format == GroupAddressType.SHORT:
            return self.raw & self.MAX_SUB_SHORT
        if self.address_format == GroupAddressType.LONG:
            return self.raw & self.MAX_SUB_LONG
        return self.raw

    def __str__(self) -> str:
        """
        Return object as in KNX notation (e.g. '1/2/3').

        Honors the used `GroupAddressType` of this group.
        """
        if self.address_format == GroupAddressType.LONG:
            return f"{self.main}/{self.middle}/{self.sub}"
        if self.address_format == GroupAddressType.SHORT:
            return f"{self.main}/{self.sub}"
        return f"{self.sub}"

    def __repr__(self) -> str:
        """Return object as parsable string."""
        return f'GroupAddress("{self}")'


class InternalGroupAddress:
    """Class for handling addresses used internally in xknx devices only."""

    __slots__ = ("raw",)

    def __init__(self, address: str | InternalGroupAddress) -> None:
        """Initialize InternalGroupAddress class."""
        self.raw: str

        if isinstance(address, InternalGroupAddress):
            self.raw = address.raw
            return
        if not isinstance(address, str):
            raise CouldNotParseAddress(address, message="Invalid type")

        prefix_length = 1
        if len(address) < 2 or address[0].lower() != "i":
            raise CouldNotParseAddress(address, message=INVALID_PREFIX_MESSAGE)
        if address[1] in "-_":
            prefix_length = 2

        _raw = address[prefix_length:].strip()
        if not _raw:
            raise CouldNotParseAddress(address, message="No chars after prefix")
        self.raw = f"i-{_raw}"

    def __str__(self) -> str:
        """Return object as readable string (e.g. 'i-123')."""
        return self.raw

    def __repr__(self) -> str:
        """Return object as parsable string."""
        return f'InternalGroupAddress("{self.raw}")'

    def __eq__(self, other: object | None) -> bool:
        """
        Implement the equal operator.

        Returns `True` if we check against the same subclass and the
        raw Value matches.
        """
        return isinstance(other, self.__class__) and self.raw == other.raw

    def __hash__(self) -> int:
        """Hash Address so it can be used as dict key."""
        return hash((self.__class__, self.raw))