Skip to content

Commit c494a3e

Browse files
authored
Device alerts (#292)
* Device alerts * Add a unit test
1 parent 9a53ced commit c494a3e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

tests/test_device.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from zigpy.exceptions import ZigbeeException
1111
import zigpy.profiles.zha
1212
from zigpy.quirks.registry import DeviceRegistry
13-
from zigpy.quirks.v2 import QuirkBuilder
13+
from zigpy.quirks.v2 import DeviceAlertLevel, DeviceAlertMetadata, QuirkBuilder
1414
import zigpy.types
1515
from zigpy.zcl.clusters import general
1616
from zigpy.zcl.foundation import Status, WriteAttributesResponse
@@ -820,3 +820,36 @@ async def test_quirks_v2_device_renaming(zha_gateway: Gateway) -> None:
820820
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
821821
assert zha_device.model == "IRIS Keypad V2"
822822
assert zha_device.manufacturer == "Lowe's"
823+
824+
825+
async def test_quirks_v2_device_alerts(zha_gateway: Gateway) -> None:
826+
"""Test quirks v2 device alerts."""
827+
828+
# Normal device, no alerts
829+
zigpy_dev = await zigpy_device_from_json(
830+
zha_gateway.application_controller,
831+
"tests/data/devices/ikea-of-sweden-tradfri-bulb-e26-opal-1000lm.json",
832+
)
833+
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
834+
assert not zha_device.device_alerts
835+
836+
# Explicit alerts
837+
registry = DeviceRegistry()
838+
839+
(
840+
QuirkBuilder("CentraLite", "3405-L", registry=registry)
841+
.device_alert(level=DeviceAlertLevel.WARNING, message="Test warning")
842+
.add_to_registry()
843+
)
844+
845+
zigpy_dev = registry.get_device(
846+
await zigpy_device_from_json(
847+
zha_gateway.application_controller,
848+
"tests/data/devices/centralite-3405-l.json",
849+
)
850+
)
851+
852+
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
853+
assert zha_device.device_alerts == (
854+
DeviceAlertMetadata(level=DeviceAlertLevel.WARNING, message="Test warning"),
855+
)

zha/zigbee/device.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import asyncio
8+
from collections.abc import Iterable
89
from dataclasses import dataclass
910
from enum import Enum
1011
from functools import cached_property
@@ -16,7 +17,7 @@
1617
import zigpy.exceptions
1718
from zigpy.profiles import PROFILES
1819
import zigpy.quirks
19-
from zigpy.quirks.v2 import QuirksV2RegistryEntry
20+
from zigpy.quirks.v2 import DeviceAlertMetadata, QuirksV2RegistryEntry
2021
from zigpy.types import uint1_t, uint8_t, uint16_t
2122
from zigpy.types.named import EUI64, NWK, ExtendedPanId
2223
from zigpy.zcl.clusters import Cluster
@@ -314,6 +315,14 @@ def model(self) -> str:
314315

315316
return self._zigpy_device.model
316317

318+
@cached_property
319+
def device_alerts(self) -> Iterable[DeviceAlertMetadata]:
320+
"""Return device alerts for this device."""
321+
if self.quirk_metadata is None:
322+
return []
323+
324+
return self.quirk_metadata.device_alerts
325+
317326
@cached_property
318327
def manufacturer_code(self) -> int | None:
319328
"""Return the manufacturer code for the device."""

0 commit comments

Comments
 (0)