diff --git a/tests/test_device.py b/tests/test_device.py index ef52b3e85..3ee84caca 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -10,7 +10,7 @@ from zigpy.exceptions import ZigbeeException import zigpy.profiles.zha from zigpy.quirks.registry import DeviceRegistry -from zigpy.quirks.v2 import QuirkBuilder +from zigpy.quirks.v2 import DeviceAlertLevel, DeviceAlertMetadata, QuirkBuilder import zigpy.types from zigpy.zcl.clusters import general from zigpy.zcl.foundation import Status, WriteAttributesResponse @@ -820,3 +820,36 @@ async def test_quirks_v2_device_renaming(zha_gateway: Gateway) -> None: zha_device = await join_zigpy_device(zha_gateway, zigpy_dev) assert zha_device.model == "IRIS Keypad V2" assert zha_device.manufacturer == "Lowe's" + + +async def test_quirks_v2_device_alerts(zha_gateway: Gateway) -> None: + """Test quirks v2 device alerts.""" + + # Normal device, no alerts + zigpy_dev = await zigpy_device_from_json( + zha_gateway.application_controller, + "tests/data/devices/ikea-of-sweden-tradfri-bulb-e26-opal-1000lm.json", + ) + zha_device = await join_zigpy_device(zha_gateway, zigpy_dev) + assert not zha_device.device_alerts + + # Explicit alerts + registry = DeviceRegistry() + + ( + QuirkBuilder("CentraLite", "3405-L", registry=registry) + .device_alert(level=DeviceAlertLevel.WARNING, message="Test warning") + .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.device_alerts == ( + DeviceAlertMetadata(level=DeviceAlertLevel.WARNING, message="Test warning"), + ) diff --git a/zha/zigbee/device.py b/zha/zigbee/device.py index 0cebe1856..ef2a51d67 100644 --- a/zha/zigbee/device.py +++ b/zha/zigbee/device.py @@ -5,6 +5,7 @@ from __future__ import annotations import asyncio +from collections.abc import Iterable from dataclasses import dataclass from enum import Enum from functools import cached_property @@ -16,7 +17,7 @@ import zigpy.exceptions from zigpy.profiles import PROFILES import zigpy.quirks -from zigpy.quirks.v2 import QuirksV2RegistryEntry +from zigpy.quirks.v2 import DeviceAlertMetadata, 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 @@ -314,6 +315,14 @@ def model(self) -> str: return self._zigpy_device.model + @cached_property + def device_alerts(self) -> Iterable[DeviceAlertMetadata]: + """Return device alerts for this device.""" + if self.quirk_metadata is None: + return [] + + return self.quirk_metadata.device_alerts + @cached_property def manufacturer_code(self) -> int | None: """Return the manufacturer code for the device."""