|
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, |
@@ -1154,3 +1157,61 @@ async def test_somrig_events(zha_gateway: Gateway) -> None: |
1154 | 1157 | ) |
1155 | 1158 | ) |
1156 | 1159 | ] |
| 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