Skip to content

Commit

Permalink
Load network key data at startup (#171)
Browse files Browse the repository at this point in the history
* Load key data at startup

* Add unit tests

* Revert unnecessary `_REQ`/`_RSP` schema additions
  • Loading branch information
puddly authored Nov 4, 2021
1 parent 069cd6e commit 792a0cc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ ENV/

# Visual Studio Code
.vscode

.DS_Store
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
author_email="schmidt.d@aon.at",
license="GPL-3.0",
packages=find_packages(exclude=["tests"]),
install_requires=["pyserial-asyncio", "zigpy>=0.37.0"],
install_requires=["pyserial-asyncio", "zigpy>=0.40.0"],
tests_require=["pytest", "pytest-asyncio", "asynctest"],
)
32 changes: 28 additions & 4 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import zigpy.config
import zigpy.device
import zigpy.neighbor
from zigpy.types import EUI64
from zigpy.types import EUI64, Channels
import zigpy.zdo.types as zdo_t

from zigpy_deconz import types as t
Expand Down Expand Up @@ -253,10 +253,34 @@ async def _version():
app._api._proto_ver = protocol_ver
return [version]

params = {
deconz_api.NetworkParameter.aps_designed_coordinator: [designed_coord],
deconz_api.NetworkParameter.nwk_address: [designed_coord],
deconz_api.NetworkParameter.protocol_version: [protocol_ver],
deconz_api.NetworkParameter.mac_address: [EUI64([0x01] * 8)],
deconz_api.NetworkParameter.nwk_address: [0x0000],
deconz_api.NetworkParameter.nwk_panid: [0x1234],
deconz_api.NetworkParameter.nwk_extended_panid: [EUI64([0x02] * 8)],
deconz_api.NetworkParameter.channel_mask: [Channels.CHANNEL_25],
deconz_api.NetworkParameter.aps_extended_panid: [EUI64([0x02] * 8)],
deconz_api.NetworkParameter.network_key: [0, t.Key([0x03] * 16)],
deconz_api.NetworkParameter.trust_center_address: [EUI64([0x04] * 8)],
deconz_api.NetworkParameter.link_key: [
EUI64([0x04] * 8),
t.Key(b"ZigBeeAlliance09"),
],
deconz_api.NetworkParameter.security_mode: [3],
deconz_api.NetworkParameter.current_channel: [25],
deconz_api.NetworkParameter.nwk_update_id: [0],
}

async def _read_param(param, *args):
if param == deconz_api.NetworkParameter.mac_address:
return (t.EUI64([0x01] * 8),)
return (designed_coord,)
try:
return params[param]
except KeyError:
raise zigpy_deconz.exception.CommandError(
deconz_api.Status.UNSUPPORTED, "Unsupported"
)

app._reset_watchdog = AsyncMock()
app.form_network = AsyncMock()
Expand Down
10 changes: 9 additions & 1 deletion zigpy_deconz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import serial
from zigpy.config import CONF_DEVICE_PATH
import zigpy.exceptions
from zigpy.types import APSStatus, Channels
from zigpy.types import APSStatus, Bool, Channels

from zigpy_deconz.exception import APIException, CommandError
import zigpy_deconz.types as t
Expand Down Expand Up @@ -179,12 +179,16 @@ class NetworkParameter(t.uint8_t, enum.Enum):
aps_extended_panid = 0x0B
trust_center_address = 0x0E
security_mode = 0x10
use_predefined_nwk_panid = 0x15
network_key = 0x18
link_key = 0x19
current_channel = 0x1C
permit_join = 0x21
protocol_version = 0x22
nwk_update_id = 0x24
watchdog_ttl = 0x26
nwk_frame_counter = 0x27
app_zdp_response_handling = 0x28


NETWORK_PARAMETER_SCHEMA = {
Expand All @@ -197,12 +201,16 @@ class NetworkParameter(t.uint8_t, enum.Enum):
NetworkParameter.aps_extended_panid: (t.ExtendedPanId,),
NetworkParameter.trust_center_address: (t.EUI64,),
NetworkParameter.security_mode: (t.uint8_t,),
NetworkParameter.use_predefined_nwk_panid: (Bool,),
NetworkParameter.network_key: (t.uint8_t, t.Key),
NetworkParameter.link_key: (t.EUI64, t.Key),
NetworkParameter.current_channel: (t.uint8_t,),
NetworkParameter.permit_join: (t.uint8_t,),
NetworkParameter.protocol_version: (t.uint16_t,),
NetworkParameter.nwk_update_id: (t.uint8_t,),
NetworkParameter.watchdog_ttl: (t.uint32_t,),
NetworkParameter.nwk_frame_counter: (t.uint32_t,),
NetworkParameter.app_zdp_response_handling: (t.uint16_t,),
}


Expand Down
29 changes: 29 additions & 0 deletions zigpy_deconz/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,40 @@ async def startup(self, auto_form=False):
NetworkParameter.channel_mask
]
await self._api[NetworkParameter.aps_extended_panid]

if self.state.network_information.network_key is None:
self.state.network_information.network_key = zigpy.state.Key()

(
_,
self.state.network_information.network_key.key,
) = await self._api.read_parameter(NetworkParameter.network_key, 0)
self.state.network_information.network_key.seq = 0
self.state.network_information.network_key.rx_counter = None
self.state.network_information.network_key.partner_ieee = None

try:
(self.state.network_information.network_key.tx_counter,) = await self._api[
NetworkParameter.nwk_frame_counter
]
except zigpy_deconz.exception.CommandError as ex:
assert ex.status == Status.UNSUPPORTED
self.state.network_information.network_key.tx_counter = None

if self.state.network_information.tc_link_key is None:
self.state.network_information.tc_link_key = zigpy.state.Key()

(self.state.network_information.tc_link_key.partner_ieee,) = await self._api[
NetworkParameter.trust_center_address
]
(
_,
self.state.network_information.tc_link_key.key,
) = await self._api.read_parameter(
NetworkParameter.link_key,
self.state.network_information.tc_link_key.partner_ieee,
)

(self.state.network_information.security_level,) = await self._api[
NetworkParameter.security_mode
]
Expand Down

0 comments on commit 792a0cc

Please sign in to comment.