Skip to content

Commit 34d7c19

Browse files
committed
rework client side entity handling
1 parent 0ee9d9c commit 34d7c19

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

zha/application/platforms/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -496,21 +496,20 @@ def __init__(
496496
self.PLATFORM = entity_info.platform
497497
self._device: WebSocketClientDevice = device
498498
self._entity_info: BaseEntityInfoType = entity_info
499-
self._attr_enabled = self._entity_info.enabled
500-
self._attr_fallback_name = self._entity_info.fallback_name
501-
self._attr_translation_key = self._entity_info.translation_key
502-
self._attr_entity_category = self._entity_info.entity_category
503-
self._attr_entity_registry_enabled_default = (
504-
self._entity_info.entity_registry_enabled_default
505-
)
506-
self._attr_device_class = self._entity_info.device_class
507-
self._attr_state_class = self._entity_info.state_class
499+
self._update_attrs_from_entity_info()
508500

509501
@property
510502
def info_object(self) -> BaseEntityInfoType:
511503
"""Return a representation of the alarm control panel."""
512504
return self._entity_info
513505

506+
@info_object.setter
507+
def info_object(self, entity_info: BaseEntityInfoType) -> None:
508+
"""Set the entity info object."""
509+
self._entity_info = entity_info
510+
self._update_attrs_from_entity_info()
511+
self.maybe_emit_state_changed_event()
512+
514513
@property
515514
def state(self) -> dict[str, Any]:
516515
"""Return the arguments to use in the command."""
@@ -521,6 +520,7 @@ def state(self, value: dict[str, Any]) -> None:
521520
"""Set the state of the entity."""
522521
self._entity_info.state = value
523522
self._attr_enabled = self._entity_info.enabled
523+
self.maybe_emit_state_changed_event()
524524

525525
@property
526526
def group_id(self) -> int | None:
@@ -562,6 +562,18 @@ def _disable(self, future: asyncio.Future) -> None:
562562
self._attr_enabled = False
563563
self.maybe_emit_state_changed_event()
564564

565+
def _update_attrs_from_entity_info(self) -> None:
566+
"""Update the entity attributes."""
567+
self._attr_enabled = self._entity_info.enabled
568+
self._attr_fallback_name = self._entity_info.fallback_name
569+
self._attr_translation_key = self._entity_info.translation_key
570+
self._attr_entity_category = self._entity_info.entity_category
571+
self._attr_entity_registry_enabled_default = (
572+
self._entity_info.entity_registry_enabled_default
573+
)
574+
self._attr_device_class = self._entity_info.device_class
575+
self._attr_state_class = self._entity_info.state_class
576+
565577
async def async_update(self) -> None:
566578
"""Retrieve latest state."""
567579
self.debug("polling current state")

zha/zigbee/device.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ def __init__(
11461146
self.unique_id: str = str(extended_device_info.ieee)
11471147
self._entities: dict[tuple[Platform, str], WebSocketClientEntity] = {}
11481148
if self._extended_device_info.entities:
1149-
self._build_entities()
1149+
self._build_or_update_entities()
11501150

11511151
@property
11521152
def quirk_id(self) -> str | None:
@@ -1172,7 +1172,7 @@ def extended_device_info(self) -> ExtendedDeviceInfo:
11721172
def extended_device_info(self, extended_device_info: ExtendedDeviceInfo) -> None:
11731173
"""Set extended device information."""
11741174
self._extended_device_info = extended_device_info
1175-
self._build_entities()
1175+
self._build_or_update_entities()
11761176

11771177
@property
11781178
def gateway(self) -> WebSocketClientGateway:
@@ -1300,19 +1300,18 @@ def platform_entities(self) -> dict[tuple[Platform, str], WebSocketClientEntity]
13001300
"""Return the platform entities for this device."""
13011301
return self._entities
13021302

1303-
def _build_entities(self):
1303+
def _build_or_update_entities(self):
13041304
"""Build the entities for this device or rebuild them from extended device info."""
1305-
self._entities.update(
1306-
{
1307-
(
1308-
entity_info.platform,
1309-
entity_info.unique_id,
1310-
): discovery.ENTITY_INFO_CLASS_TO_WEBSOCKET_CLIENT_ENTITY_CLASS[
1311-
entity_info.__class__
1312-
](entity_info, self)
1313-
for entity_info in self._extended_device_info.entities.values()
1314-
}
1315-
)
1305+
for entity_info in self._extended_device_info.entities.values():
1306+
entity_key = (entity_info.platform, entity_info.unique_id)
1307+
if entity_key in self._entities:
1308+
self._entities[entity_key].entity_info = entity_info
1309+
else:
1310+
self._entities[entity_key] = (
1311+
discovery.ENTITY_INFO_CLASS_TO_WEBSOCKET_CLIENT_ENTITY_CLASS[
1312+
entity_info.__class__
1313+
](entity_info, self)
1314+
)
13161315

13171316
def emit_platform_entity_event(self, event: EntityStateChangedEvent) -> None:
13181317
"""Proxy the firing of an entity event."""
@@ -1322,5 +1321,4 @@ def emit_platform_entity_event(self, event: EntityStateChangedEvent) -> None:
13221321
f"Entity not found: {event.platform}.{event.unique_id}",
13231322
)
13241323
entity.state = event.state
1325-
entity.maybe_emit_state_changed_event()
13261324
self.emit(f"{event.unique_id}_{event.event}", event)

0 commit comments

Comments
 (0)