Skip to content

Commit 8af5782

Browse files
committed
Add a unit test
1 parent 32d7794 commit 8af5782

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/test_device.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from zigpy.zcl import ClusterType
1919
from zigpy.zcl.clusters import general
2020
from zigpy.zcl.clusters.general import Ota, PowerConfiguration
21+
from zigpy.zcl.clusters.lighting import Color
2122
from zigpy.zcl.foundation import Status, WriteAttributesResponse
2223
import zigpy.zdo.types as zdo_t
2324

@@ -49,6 +50,8 @@
4950
from zha.exceptions import ZHAException
5051
from zha.zigbee.device import (
5152
ClusterBinding,
53+
DeviceEntityAddedEvent,
54+
DeviceEntityRemovedEvent,
5255
DeviceFirmwareInfoUpdatedEvent,
5356
ZHAEvent,
5457
get_device_automation_triggers,
@@ -1154,3 +1157,61 @@ async def test_somrig_events(zha_gateway: Gateway) -> None:
11541157
)
11551158
)
11561159
]
1160+
1161+
1162+
async def test_entity_recomputation(zha_gateway: Gateway) -> None:
1163+
"""Test entity recomputation."""
1164+
zigpy_dev = await zigpy_device_from_json(
1165+
zha_gateway.application_controller,
1166+
"tests/data/devices/ikea-of-sweden-tradfri-bulb-gu10-ws-400lm.json",
1167+
)
1168+
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
1169+
1170+
event_listener = mock.Mock()
1171+
zha_device.on_all_events(event_listener)
1172+
1173+
entities1 = set(zha_device.platform_entities.values())
1174+
1175+
# We lose track of the color temperature
1176+
zha_device._zigpy_device.endpoints[1].light_color.add_unsupported_attribute(
1177+
Color.AttributeDefs.start_up_color_temperature.id
1178+
)
1179+
await zha_device.recompute_entities()
1180+
1181+
entities2 = set(zha_device.platform_entities.values())
1182+
assert entities2 - entities1 == set()
1183+
assert len(entities1 - entities2) == 1
1184+
assert (
1185+
list(entities1 - entities2)[0].unique_id
1186+
== "68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1187+
)
1188+
assert event_listener.mock_calls == [
1189+
call(
1190+
DeviceEntityRemovedEvent(
1191+
unique_id="68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1192+
)
1193+
)
1194+
]
1195+
1196+
event_listener.reset_mock()
1197+
1198+
# We add it back
1199+
zha_device._zigpy_device.endpoints[1].light_color.remove_unsupported_attribute(
1200+
Color.AttributeDefs.start_up_color_temperature.id
1201+
)
1202+
await zha_device.recompute_entities()
1203+
1204+
entities3 = set(zha_device.platform_entities.values())
1205+
assert (
1206+
list(entities3 - entities2)[0].unique_id
1207+
== "68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1208+
)
1209+
assert {e.unique_id for e in entities1} == {e.unique_id for e in entities3}
1210+
1211+
assert event_listener.mock_calls == [
1212+
call(
1213+
DeviceEntityAddedEvent(
1214+
unique_id="68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1215+
)
1216+
)
1217+
]

0 commit comments

Comments
 (0)