Skip to content

Commit

Permalink
Allow renaming devices with quirk v2 friendly_name (#283)
Browse files Browse the repository at this point in the history
Allow renaming devices
  • Loading branch information
puddly authored Oct 30, 2024
1 parent 8897c0f commit 69abf69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import pytest
from zigpy.exceptions import ZigbeeException
import zigpy.profiles.zha
from zigpy.quirks.registry import DeviceRegistry
from zigpy.quirks.v2 import QuirkBuilder
import zigpy.types
from zigpy.zcl.clusters import general
from zigpy.zcl.foundation import Status, WriteAttributesResponse
Expand All @@ -20,6 +22,7 @@
SIG_EP_TYPE,
create_mock_zigpy_device,
join_zigpy_device,
zigpy_device_from_json,
)
from zha.application import Platform
from zha.application.const import (
Expand Down Expand Up @@ -795,3 +798,25 @@ async def test_device_properties(
assert zha_device.is_router is None
assert zha_device.is_end_device is None
assert zha_device.is_coordinator is None


async def test_quirks_v2_device_renaming(zha_gateway: Gateway) -> None:
"""Test quirks v2 device renaming."""
registry = DeviceRegistry()

(
QuirkBuilder("CentraLite", "3405-L", registry=registry)
.friendly_name(manufacturer="Lowe's", model="IRIS Keypad V2")
.add_to_registry()
)

zigpy_dev = registry.get_device(
await zigpy_device_from_json(
zha_gateway.application_controller,
"tests/data/devices/centralite-3405-l.json",
)
)

zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
assert zha_device.model == "IRIS Keypad V2"
assert zha_device.manufacturer == "Lowe's"
18 changes: 18 additions & 0 deletions zha/zigbee/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import zigpy.exceptions
from zigpy.profiles import PROFILES
import zigpy.quirks
from zigpy.quirks.v2 import QuirksV2RegistryEntry
from zigpy.types import uint1_t, uint8_t, uint16_t
from zigpy.types.named import EUI64, NWK, ExtendedPanId
from zigpy.zcl.clusters import Cluster
Expand Down Expand Up @@ -266,6 +267,11 @@ def ieee(self) -> EUI64:
"""Return ieee address for device."""
return self._zigpy_device.ieee

@property
def quirk_metadata(self) -> QuirksV2RegistryEntry | None:
"""Return the quirk metadata for this device."""
return getattr(self._zigpy_device, "quirk_metadata", None)

@cached_property
def manufacturer(self) -> str:
"""Return manufacturer for device."""
Expand All @@ -277,6 +283,12 @@ def manufacturer(self) -> str:
return ""
return manufacturer

if (
self.quirk_metadata is not None
and self.quirk_metadata.friendly_name is not None
):
return self.quirk_metadata.friendly_name.manufacturer

if self._zigpy_device.manufacturer is None:
return UNKNOWN_MANUFACTURER

Expand All @@ -291,6 +303,12 @@ def model(self) -> str:
return f"Generic Zigbee Coordinator ({self.gateway.radio_type.pretty_name})"
return model

if (
self.quirk_metadata is not None
and self.quirk_metadata.friendly_name is not None
):
return self.quirk_metadata.friendly_name.model

if self._zigpy_device.model is None:
return UNKNOWN_MODEL

Expand Down

0 comments on commit 69abf69

Please sign in to comment.