Skip to content

Commit 8b00d74

Browse files
authored
Read the network key if the firmware supports it (#152)
1 parent 1166711 commit 8b00d74

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

zigpy_zigate/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import serial
1010
import zigpy.exceptions
11+
import zigpy.types
1112

1213
import zigpy_zigate.config
1314
import zigpy_zigate.uart
@@ -39,6 +40,7 @@ class CommandId(enum.IntEnum):
3940
MANAGEMENT_NETWORK_UPDATE_REQUEST = 0x004A
4041
SEND_RAW_APS_DATA_PACKET = 0x0530
4142
AHI_SET_TX_POWER = 0x0806
43+
GET_NETWORK_KEY = 0x0054
4244

4345

4446
class ResponseId(enum.IntEnum):
@@ -65,6 +67,7 @@ class ResponseId(enum.IntEnum):
6567
APS_DATA_CONFIRM_FAILED = 0x8702
6668
AHI_SET_TX_POWER_RSP = 0x8806
6769
EXTENDED_ERROR = 0x9999
70+
GET_NETWORK_KEY_LIST = 0x8054
6871

6972

7073
class SendSecurity(t.uint8_t, enum.Enum):
@@ -142,6 +145,7 @@ class FactoryNewRestartStatus(t.uint8_t, enum.Enum):
142145
),
143146
ResponseId.AHI_SET_TX_POWER_RSP: (t.uint8_t,),
144147
ResponseId.EXTENDED_ERROR: (t.Status,),
148+
ResponseId.GET_NETWORK_KEY_LIST: (zigpy.types.KeyData,),
145149
}
146150

147151
COMMANDS = {
@@ -212,6 +216,10 @@ class CommandError(zigpy.exceptions.APIException):
212216
pass
213217

214218

219+
class CommandNotSupportedError(CommandError):
220+
pass
221+
222+
215223
class ZiGate:
216224
def __init__(self, device_config: Dict[str, Any]):
217225
self._app = None
@@ -568,6 +576,16 @@ def handle_callback(self, *args):
568576
except Exception as e:
569577
LOGGER.exception("Exception running handler", exc_info=e)
570578

579+
async def get_network_key(self):
580+
rsp, _ = await self.command(
581+
CommandId.GET_NETWORK_KEY, wait_response=ResponseId.GET_NETWORK_KEY_LIST
582+
)
583+
584+
if rsp[0] == t.Status.UnhandledCommand:
585+
raise CommandNotSupportedError()
586+
587+
return rsp[0]
588+
571589
@classmethod
572590
async def probe(cls, device_config: Dict[str, Any]) -> bool:
573591
"""Probe port for the device presence."""

zigpy_zigate/zigbee/application.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
import zigpy.zdo
1515

1616
from zigpy_zigate import common as c, types as t
17-
from zigpy_zigate.api import PDM_EVENT, NoResponseError, ResponseId, ZiGate
17+
from zigpy_zigate.api import (
18+
PDM_EVENT,
19+
CommandNotSupportedError,
20+
NoResponseError,
21+
ResponseId,
22+
ZiGate,
23+
)
1824
from zigpy_zigate.config import (
1925
CONF_DEVICE,
2026
CONF_DEVICE_PATH,
@@ -91,6 +97,12 @@ async def load_network_info(self, *, load_devices: bool = False):
9197
zigpy.types.uint64_t(network_state[3]).serialize()
9298
)
9399

100+
try:
101+
network_key_data = await self._api.get_network_key()
102+
network_key = zigpy.state.Key(key=network_key_data)
103+
except CommandNotSupportedError:
104+
network_key = zigpy.state.Key()
105+
94106
self.state.network_info = zigpy.state.NetworkInfo(
95107
source=f"zigpy-zigate@{LIB_VERSION}",
96108
extended_pan_id=epid,
@@ -100,8 +112,7 @@ async def load_network_info(self, *, load_devices: bool = False):
100112
channel=network_state[4],
101113
channel_mask=zigpy.types.Channels.from_channel_list([network_state[4]]),
102114
security_level=5,
103-
# TODO: is it possible to read keys?
104-
# network_key=zigpy.state.Key(),
115+
network_key=network_key,
105116
# tc_link_key=zigpy.state.Key(),
106117
children=[],
107118
key_table=[],

0 commit comments

Comments
 (0)