File: __init__.py

package info (click to toggle)
python-bellows 0.40.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 992 kB
  • sloc: python: 13,630; sh: 7; makefile: 4
file content (109 lines) | stat: -rw-r--r-- 3,526 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
""""EZSP Protocol version 14 protocol handler."""
from __future__ import annotations

from typing import AsyncGenerator

import voluptuous as vol
from zigpy.exceptions import NetworkNotFormed
import zigpy.state

import bellows.config
import bellows.types as t

from . import commands, config
from ..v13 import EZSPv13


class EZSPv14(EZSPv13):
    """EZSP Version 14 Protocol version handler."""

    VERSION = 14
    COMMANDS = commands.COMMANDS
    SCHEMAS = {
        bellows.config.CONF_EZSP_CONFIG: vol.Schema(config.EZSP_SCHEMA),
        bellows.config.CONF_EZSP_POLICIES: vol.Schema(config.EZSP_POLICIES_SCH),
    }

    async def read_address_table(self) -> AsyncGenerator[tuple[t.NWK, t.EUI64], None]:
        (status, addr_table_size) = await self.getConfigurationValue(
            configId=t.EzspConfigId.CONFIG_ADDRESS_TABLE_SIZE
        )

        for idx in range(addr_table_size + 100):
            (status, nwk, eui64) = await self.getAddressTableInfo(index=idx)

            if status != t.sl_Status.OK:
                continue

            if eui64 in (
                t.EUI64.convert("00:00:00:00:00:00:00:00"),
                t.EUI64.convert("FF:FF:FF:FF:FF:FF:FF:FF"),
            ):
                continue

            yield nwk, eui64

    async def get_network_key(self) -> zigpy.state.Key:
        status, network_key_data, _ = await self.exportKey(
            context=t.SecurityManagerContextV13(
                core_key_type=t.SecurityManagerKeyType.NETWORK,
                key_index=0,
                derived_type=t.SecurityManagerDerivedKeyTypeV13.NONE,
                eui64=t.EUI64.convert("00:00:00:00:00:00:00:00"),
                multi_network_index=0,
                flags=t.SecurityManagerContextFlags.NONE,
                psa_key_alg_permission=0,
            )
        )

        assert status == t.sl_Status.OK

        (status, network_key_info) = await self.getNetworkKeyInfo()
        assert status == t.sl_Status.OK

        if not network_key_info.network_key_set:
            raise NetworkNotFormed("Network key is not set")

        return zigpy.state.Key(
            key=network_key_data,
            tx_counter=network_key_info.network_key_frame_counter,
            seq=network_key_info.network_key_sequence_number,
        )

    async def get_tc_link_key(self) -> zigpy.state.Key:
        status, tc_link_key_data, _ = await self.exportKey(
            context=t.SecurityManagerContextV13(
                core_key_type=t.SecurityManagerKeyType.TC_LINK,
                key_index=0,
                derived_type=t.SecurityManagerDerivedKeyTypeV13.NONE,
                eui64=t.EUI64.convert("00:00:00:00:00:00:00:00"),
                multi_network_index=0,
                flags=t.SecurityManagerContextFlags.NONE,
                psa_key_alg_permission=0,
            )
        )

        assert status == t.sl_Status.OK

        return zigpy.state.Key(key=tc_link_key_data)

    async def send_broadcast(
        self,
        address: t.BroadcastAddress,
        aps_frame: t.EmberApsFrame,
        radius: t.uint8_t,
        message_tag: t.uint8_t,
        aps_sequence: t.uint8_t,
        data: bytes,
    ) -> tuple[t.sl_Status, t.uint8_t]:
        status, sequence = await self.sendBroadcast(
            alias=0x0000,
            destination=address,
            sequence=aps_sequence,
            aps_frame=aps_frame,
            radius=radius,
            message_tag=message_tag,
            message=data,
        )

        return status, sequence