Skip to content

Commit

Permalink
Parse model info during load_network_info
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Nov 16, 2023
1 parent a55def7 commit e56caad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
38 changes: 5 additions & 33 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ async def test_deconz_dev_add_to_group(app, nwk, device_path):
app._groups = MagicMock()
app._groups.add_group.return_value = group

deconz = application.DeconzDevice(
deconz_api.FirmwareVersion(0x26580700), device_path, app, sentinel.ieee, nwk
)
deconz = application.DeconzDevice("Conbee II", app, sentinel.ieee, nwk)
deconz.endpoints = {
0: sentinel.zdo,
1: sentinel.ep1,
Expand All @@ -268,9 +266,7 @@ async def test_deconz_dev_add_to_group(app, nwk, device_path):
async def test_deconz_dev_remove_from_group(app, nwk, device_path):
group = MagicMock()
app.groups[sentinel.grp_id] = group
deconz = application.DeconzDevice(
deconz_api.FirmwareVersion(0x26580700), device_path, app, sentinel.ieee, nwk
)
deconz = application.DeconzDevice("Conbee II", app, sentinel.ieee, nwk)
deconz.endpoints = {
0: sentinel.zdo,
1: sentinel.ep1,
Expand All @@ -282,38 +278,16 @@ async def test_deconz_dev_remove_from_group(app, nwk, device_path):


def test_deconz_props(nwk, device_path):
deconz = application.DeconzDevice(
deconz_api.FirmwareVersion(0x26580700), device_path, app, sentinel.ieee, nwk
)
deconz = application.DeconzDevice("Conbee II", app, sentinel.ieee, nwk)
assert deconz.manufacturer is not None
assert deconz.model is not None


@pytest.mark.parametrize(
"name, firmware_version, device_path",
[
("ConBee", deconz_api.FirmwareVersion(0x00000500), "/dev/ttyUSB0"),
("ConBee II", deconz_api.FirmwareVersion(0x00000700), "/dev/ttyUSB0"),
("RaspBee", deconz_api.FirmwareVersion(0x00000500), "/dev/ttyS0"),
("RaspBee II", deconz_api.FirmwareVersion(0x00000700), "/dev/ttyS0"),
("RaspBee", deconz_api.FirmwareVersion(0x00000500), "/dev/ttyAMA0"),
("RaspBee II", deconz_api.FirmwareVersion(0x00000700), "/dev/ttyAMA0"),
],
)
def test_deconz_name(nwk, name, firmware_version, device_path):
deconz = application.DeconzDevice(
firmware_version, device_path, app, sentinel.ieee, nwk
)
assert deconz.model == name


async def test_deconz_new(app, nwk, device_path, monkeypatch):
mock_init = AsyncMock()
monkeypatch.setattr(zigpy.device.Device, "_initialize", mock_init)

deconz = await application.DeconzDevice.new(
app, sentinel.ieee, nwk, deconz_api.FirmwareVersion(0x26580700), device_path
)
deconz = await application.DeconzDevice.new(app, sentinel.ieee, nwk, "Conbee II")
assert isinstance(deconz, application.DeconzDevice)
assert mock_init.call_count == 1
mock_init.reset_mock()
Expand All @@ -325,9 +299,7 @@ async def test_deconz_new(app, nwk, device_path, monkeypatch):
22: MagicMock(),
}
app.devices[sentinel.ieee] = mock_dev
deconz = await application.DeconzDevice.new(
app, sentinel.ieee, nwk, deconz_api.FirmwareVersion(0x26580700), device_path
)
deconz = await application.DeconzDevice.new(app, sentinel.ieee, nwk, "Conbee II")
assert isinstance(deconz, application.DeconzDevice)
assert mock_init.call_count == 0

Expand Down
5 changes: 5 additions & 0 deletions tests/test_network_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def node_info():
nwk=t.NWK(0x0000),
ieee=t.EUI64.convert("93:2C:A9:34:D9:D0:5D:12"),
logical_type=zdo_t.LogicalType.Coordinator,
manufacturer="dresden elektronik",
model="Conbee II",
version="0x26580700",
)


Expand Down Expand Up @@ -263,6 +266,7 @@ async def test_load_network_info(
ieee=node_info.ieee, key=network_info.tc_link_key.key
),
("security_mode",): zigpy_deconz.api.SecurityMode.ONLY_TCLK,
("protocol_version",): 0x010E,
}

params.update(param_overrides)
Expand All @@ -280,6 +284,7 @@ async def read_param(param, *args):

return value

app._api.firmware_version = zigpy_deconz.api.FirmwareVersion(0x26580700)
app._api.read_parameter = AsyncMock(side_effect=read_param)

if error is not None:
Expand Down
54 changes: 32 additions & 22 deletions zigpy_deconz/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from zigpy_deconz.api import (
Deconz,
FirmwarePlatform,
FirmwareVersion,
IndexedEndpoint,
IndexedKey,
LinkKey,
Expand Down Expand Up @@ -126,8 +125,7 @@ async def start_network(self):
self,
self.state.node_info.ieee,
self.state.node_info.nwk,
self._api.firmware_version,
self._config[zigpy.config.CONF_DEVICE][zigpy.config.CONF_DEVICE_PATH],
self.state.node_info.model,
)

self.devices[self.state.node_info.ieee] = coordinator
Expand Down Expand Up @@ -288,15 +286,6 @@ async def load_network_info(self, *, load_devices=False):
network_info = self.state.network_info
node_info = self.state.node_info

network_info.source = (
f"zigpy-deconz@{importlib.metadata.version('zigpy-deconz')}"
)
network_info.metadata = {
"deconz": {
"version": f"{int(self._api.firmware_version):#010x}",
}
}

ieee = await self._api.read_parameter(NetworkParameter.mac_address)
node_info.ieee = zigpy.types.EUI64(ieee)
designed_coord = await self._api.read_parameter(
Expand All @@ -310,6 +299,33 @@ async def load_network_info(self, *, load_devices=False):

node_info.nwk = await self._api.read_parameter(NetworkParameter.nwk_address)

node_info.manufacturer = "dresden elektronik"

if re.match(
r"/dev/tty(S|AMA|ACM)\d+",
self._config[zigpy.config.CONF_DEVICE][zigpy.config.CONF_DEVICE_PATH],
):
node_info.model = "Raspbee"

Check warning on line 308 in zigpy_deconz/zigbee/application.py

View check run for this annotation

Codecov / codecov/patch

zigpy_deconz/zigbee/application.py#L308

Added line #L308 was not covered by tests
else:
node_info.model = "Conbee"

node_info.model += {
FirmwarePlatform.Conbee: "",
FirmwarePlatform.Conbee_II: " II",
FirmwarePlatform.Conbee_III: " III",
}[self._api.firmware_version.platform]

node_info.version = f"{int(self._api.firmware_version):#010x}"

network_info.source = (
f"zigpy-deconz@{importlib.metadata.version('zigpy-deconz')}"
)
network_info.metadata = {
"deconz": {
"version": node_info.version,
}
}

network_info.pan_id = await self._api.read_parameter(NetworkParameter.nwk_panid)
network_info.extended_pan_id = await self._api.read_parameter(
NetworkParameter.aps_extended_panid
Expand Down Expand Up @@ -578,17 +594,11 @@ async def _delayed_neighbour_scan(self) -> None:
class DeconzDevice(zigpy.device.Device):
"""Zigpy Device representing Coordinator."""

def __init__(self, version: FirmwareVersion, device_path: str, *args):
def __init__(self, model: str, *args):
"""Initialize instance."""

super().__init__(*args)
is_gpio_device = re.match(r"/dev/tty(S|AMA|ACM)\d+", device_path)
self._model = "RaspBee" if is_gpio_device else "ConBee"
self._model += {
FirmwarePlatform.Conbee: "",
FirmwarePlatform.Conbee_II: " II",
FirmwarePlatform.Conbee_III: " III",
}[version.platform]
self._model = model

async def add_to_group(self, grp_id: int, name: str = None) -> None:
group = self.application.groups.add_group(grp_id, name)
Expand All @@ -615,9 +625,9 @@ def model(self):
return self._model

@classmethod
async def new(cls, application, ieee, nwk, version: int, device_path: str):
async def new(cls, application, ieee, nwk, model: str):
"""Create or replace zigpy device."""
dev = cls(version, device_path, application, ieee, nwk)
dev = cls(model, application, ieee, nwk)

if ieee in application.devices:
from_dev = application.get_device(ieee=ieee)
Expand Down

0 comments on commit e56caad

Please sign in to comment.