|
18 | 18 | from zigpy.zcl import ClusterType
|
19 | 19 | from zigpy.zcl.clusters import general
|
20 | 20 | from zigpy.zcl.clusters.general import Ota, PowerConfiguration
|
| 21 | +from zigpy.zcl.clusters.lighting import Color |
21 | 22 | from zigpy.zcl.foundation import Status, WriteAttributesResponse
|
22 | 23 | import zigpy.zdo.types as zdo_t
|
23 | 24 |
|
|
49 | 50 | from zha.exceptions import ZHAException
|
50 | 51 | from zha.zigbee.device import (
|
51 | 52 | ClusterBinding,
|
| 53 | + DeviceEntityAddedEvent, |
| 54 | + DeviceEntityRemovedEvent, |
52 | 55 | DeviceFirmwareInfoUpdatedEvent,
|
53 | 56 | ZHAEvent,
|
54 | 57 | get_device_automation_triggers,
|
@@ -1201,3 +1204,61 @@ async def test_symfonisk_events(
|
1201 | 1204 | )
|
1202 | 1205 | )
|
1203 | 1206 | ]
|
| 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