Skip to content

Commit

Permalink
Device alerts (#292)
Browse files Browse the repository at this point in the history
* Device alerts

* Add a unit test
  • Loading branch information
puddly authored Nov 27, 2024
1 parent 9a53ced commit c494a3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
35 changes: 34 additions & 1 deletion tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
)
11 changes: 10 additions & 1 deletion zha/zigbee/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit c494a3e

Please sign in to comment.