Skip to content

Commit 983586a

Browse files
committed
Add a unit test
1 parent bdfe559 commit 983586a

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,
@@ -1201,3 +1204,61 @@ async def test_symfonisk_events(
12011204
)
12021205
)
12031206
]
1207+
1208+
1209+
async def test_entity_recomputation(zha_gateway: Gateway) -> None:
1210+
"""Test entity recomputation."""
1211+
zigpy_dev = await zigpy_device_from_json(
1212+
zha_gateway.application_controller,
1213+
"tests/data/devices/ikea-of-sweden-tradfri-bulb-gu10-ws-400lm.json",
1214+
)
1215+
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
1216+
1217+
event_listener = mock.Mock()
1218+
zha_device.on_all_events(event_listener)
1219+
1220+
entities1 = set(zha_device.platform_entities.values())
1221+
1222+
# We lose track of the color temperature
1223+
zha_device._zigpy_device.endpoints[1].light_color.add_unsupported_attribute(
1224+
Color.AttributeDefs.start_up_color_temperature.id
1225+
)
1226+
await zha_device.recompute_entities()
1227+
1228+
entities2 = set(zha_device.platform_entities.values())
1229+
assert entities2 - entities1 == set()
1230+
assert len(entities1 - entities2) == 1
1231+
assert (
1232+
list(entities1 - entities2)[0].unique_id
1233+
== "68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1234+
)
1235+
assert event_listener.mock_calls == [
1236+
call(
1237+
DeviceEntityRemovedEvent(
1238+
unique_id="68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1239+
)
1240+
)
1241+
]
1242+
1243+
event_listener.reset_mock()
1244+
1245+
# We add it back
1246+
zha_device._zigpy_device.endpoints[1].light_color.remove_unsupported_attribute(
1247+
Color.AttributeDefs.start_up_color_temperature.id
1248+
)
1249+
await zha_device.recompute_entities()
1250+
1251+
entities3 = set(zha_device.platform_entities.values())
1252+
assert (
1253+
list(entities3 - entities2)[0].unique_id
1254+
== "68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1255+
)
1256+
assert {e.unique_id for e in entities1} == {e.unique_id for e in entities3}
1257+
1258+
assert event_listener.mock_calls == [
1259+
call(
1260+
DeviceEntityAddedEvent(
1261+
unique_id="68:0a:e2:ff:fe:8f:fa:33-1-768-start_up_color_temperature"
1262+
)
1263+
)
1264+
]

0 commit comments

Comments
 (0)