1 """Entity for Zigbee Home Automation."""
3 from __future__
import annotations
6 from collections.abc
import Callable
7 from functools
import partial
11 from propcache
import cached_property
12 from zha.mixins
import LogMixin
22 from .const
import DOMAIN
23 from .helpers
import SIGNAL_REMOVE_ENTITIES, EntityData, convert_zha_error_to_ha_error
25 _LOGGER = logging.getLogger(__name__)
31 _attr_has_entity_name =
True
32 _attr_should_poll =
False
33 remove_future: asyncio.Future[Any]
35 def __init__(self, entity_data: EntityData, *args, **kwargs) ->
None:
36 """Init ZHA entity."""
38 self.entity_data: EntityData = entity_data
39 self._unsubs: list[Callable[[],
None]] = []
41 if self.entity_data.entity.icon
is not None:
45 meta = self.entity_data.entity.info_object
48 if meta.entity_category
is not None:
52 meta.entity_registry_enabled_default
55 if meta.translation_key
is not None:
59 def name(self) -> str | UndefinedType | None:
60 """Return the name of the entity."""
61 meta = self.entity_data.entity.info_object
62 original_name = super().name
64 if original_name
not in (UNDEFINED,
None)
or meta.fallback_name
is None:
74 """Return entity availability."""
75 return self.entity_data.entity.available
79 """Return a device description for device registry."""
80 zha_device_info = self.entity_data.device_proxy.device_info
81 ieee = zha_device_info[
"ieee"]
82 zha_gateway = self.entity_data.device_proxy.gateway_proxy.gateway
85 connections={(CONNECTION_ZIGBEE, ieee)},
86 identifiers={(DOMAIN, ieee)},
87 manufacturer=zha_device_info[ATTR_MANUFACTURER],
88 model=zha_device_info[ATTR_MODEL],
89 name=zha_device_info[ATTR_NAME],
90 via_device=(DOMAIN, zha_gateway.state.node_info.ieee),
95 """Entity state changed."""
96 self.debug(
"Handling event from entity: %s", event)
100 """Run when about to be added to hass."""
106 f
"{SIGNAL_REMOVE_ENTITIES}_group_{self.entity_data.group_proxy.group.group_id}"
107 if self.entity_data.is_group_entity
108 and self.entity_data.group_proxy
is not None
109 else f
"{SIGNAL_REMOVE_ENTITIES}_{self.entity_data.device_proxy.device.ieee}"
115 partial(self.
async_removeasync_remove, force_remove=
True),
118 self.entity_data.device_proxy.gateway_proxy.register_entity_reference(
132 """Restore ephemeral external state from Home Assistant back into ZHA."""
140 """Disconnect entity object when removed."""
141 for unsub
in self._unsubs[:]:
143 self._unsubs.
remove(unsub)
147 @convert_zha_error_to_ha_error
149 """Update the entity."""
150 await self.entity_data.entity.async_update()
153 def log(self, level: int, msg: str, *args, **kwargs):
157 _LOGGER.log(level, msg, *args, **kwargs)
None async_will_remove_from_hass(self)
None __init__(self, EntityData entity_data, *args, **kwargs)
str|UndefinedType|None name(self)
None async_added_to_hass(self)
None _handle_entity_events(self, Any event)
def log(self, int level, str msg, *args, **kwargs)
DeviceInfo device_info(self)
None restore_external_state_attributes(self, State state)
_attr_entity_registry_enabled_default
None async_write_ha_state(self)
None async_remove(self, *bool force_remove=False)
DeviceInfo|None device_info(self)
State|None async_get_last_state(self)
bool remove(self, _T matcher)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)