File: search_request_extended.py

package info (click to toggle)
python-xknx 3.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,072 kB
  • sloc: python: 41,085; javascript: 8,556; makefile: 32; sh: 12
file content (57 lines) | stat: -rw-r--r-- 1,749 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
"""
Module for Serialization and Deserialization of KNX Extended Search Requests.

Extended Search Requests are used to search for KNX/IP devices within the network that support for instance IP Secure.

See AN184 in version 03 from the KNX specifications.
"""

from __future__ import annotations

from .body import KNXIPBody
from .hpai import HPAI
from .knxip_enum import KNXIPServiceType
from .srp import SRP


class SearchRequestExtended(KNXIPBody):
    """Representation of a KNX Search Request Extended."""

    SERVICE_TYPE = KNXIPServiceType.SEARCH_REQUEST_EXTENDED

    def __init__(
        self,
        discovery_endpoint: HPAI | None = None,
        srps: list[SRP] | None = None,
    ) -> None:
        """Initialize SearchRequestExtended object."""
        self.discovery_endpoint = discovery_endpoint or HPAI()
        self.srps = srps or []

    def calculated_length(self) -> int:
        """Get length of KNX/IP body."""
        return HPAI.LENGTH + sum(srp.payload_size for srp in self.srps)

    def from_knx(self, raw: bytes) -> int:
        """Parse/deserialize from KNX/IP raw data."""
        pos: int = self.discovery_endpoint.from_knx(raw)
        while raw[pos:]:
            srp = SRP.from_knx(raw[pos:])
            pos += len(srp)
            self.srps.append(srp)

        return pos

    def to_knx(self) -> bytes:
        """Serialize to KNX/IP raw data."""
        return self.discovery_endpoint.to_knx() + b"".join(
            bytes(srp) for srp in self.srps
        )

    def __repr__(self) -> str:
        """Return object as readable string."""
        return (
            "<SearchRequestExtended "
            f'discovery_endpoint="{self.discovery_endpoint}" '
            f'srps="{self.srps}" />'
        )