1 """Generic Hue Entity Model."""
3 from __future__
import annotations
5 from typing
import TYPE_CHECKING
7 from aiohue.v2.controllers.base
import BaseResourcesController
8 from aiohue.v2.controllers.events
import EventType
9 from aiohue.v2.models.resource
import ResourceTypes
10 from aiohue.v2.models.zigbee_connectivity
import ConnectivityServiceStatus
17 from ..bridge
import HueBridge
18 from ..const
import CONF_IGNORE_AVAILABILITY, DOMAIN
21 from aiohue.v2.models.device_power
import DevicePower
22 from aiohue.v2.models.grouped_light
import GroupedLight
23 from aiohue.v2.models.light
import Light
24 from aiohue.v2.models.light_level
import LightLevel
25 from aiohue.v2.models.motion
import Motion
27 type HueResource = Light | DevicePower | GroupedLight | LightLevel | Motion
30 RESOURCE_TYPE_NAMES = {
32 ResourceTypes.LIGHT_LEVEL:
"Illuminance",
33 ResourceTypes.DEVICE_POWER:
"Battery",
38 """Generic Entity Class for a Hue resource."""
40 _attr_should_poll =
False
45 controller: BaseResourcesController,
46 resource: HueResource,
48 """Initialize a generic Hue resource entity."""
52 self.
devicedevice = controller.get_device(resource.id)
53 self.
loggerlogger = bridge.logger.getChild(resource.type.value)
59 if self.
devicedevice
is None:
63 identifiers={(DOMAIN, bridge.api.config.bridge.bridge_id)},
67 identifiers={(DOMAIN, self.
devicedevice.id)},
74 """Call when entity is added."""
81 (EventType.RESOURCE_UPDATED, EventType.RESOURCE_DELETED),
85 if self.
devicedevice
is None:
88 self.
bridgebridge.api.devices.subscribe(
91 EventType.RESOURCE_UPDATED,
95 if zigbee := self.
bridgebridge.api.devices.get_zigbee_connectivity(self.
devicedevice.id):
97 self.
bridgebridge.api.sensors.zigbee_connectivity.subscribe(
100 EventType.RESOURCE_UPDATED,
106 """Return entity availability."""
108 if self.
devicedevice
is None:
111 if self.
resourceresource.type == ResourceTypes.ZIGBEE_CONNECTIVITY:
116 if zigbee := self.
bridgebridge.api.devices.get_zigbee_connectivity(self.
devicedevice.id):
117 return zigbee.status == ConnectivityServiceStatus.CONNECTED
122 """Call on update event."""
126 def _handle_event(self, event_type: EventType, resource: HueResource) ->
None:
127 """Handle status event for this resource (or it's parent)."""
128 if event_type == EventType.RESOURCE_DELETED:
130 if self.
devicedevice
is None and resource.id == self.
resourceresource.id:
131 ent_reg = er.async_get(self.
hasshass)
132 ent_reg.async_remove(self.
entity_identity_id)
135 self.
loggerlogger.debug(
"Received status update for %s", self.
entity_identity_id)
142 """Check availability of the device."""
147 if self.
devicedevice
is None or not hasattr(self.
resourceresource,
"on"):
151 if self.
devicedevice.id
in self.
bridgebridge.config_entry.options.get(
152 CONF_IGNORE_AVAILABILITY, []
156 "Device %s is configured to ignore availability status. ",
162 if self.
devicedevice.product_data.certified:
172 cur_state = self.
resourceresource.on.on
176 if zigbee := self.
bridgebridge.api.devices.get_zigbee_connectivity(self.
devicedevice.id):
179 and zigbee.status != ConnectivityServiceStatus.CONNECTED
183 self.
loggerlogger.warning(
185 "Device %s changed state while reported as disconnected. This"
186 " might be an indicator that routing is not working for this"
187 " device or the device is having connectivity issues. You can"
188 " disable availability reporting for this device in the Hue"
189 " options. Device details: %s - %s (%s) fw: %s"
192 self.
devicedevice.product_data.manufacturer_name,
193 self.
devicedevice.product_data.product_name,
194 self.
devicedevice.product_data.model_id,
195 self.
devicedevice.product_data.software_version,
None __init__(self, HueBridge bridge, BaseResourcesController controller, HueResource resource)
None _handle_event(self, EventType event_type, HueResource resource)
def _check_availability(self)
None async_added_to_hass(self)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
str|UndefinedType|None name(self)
Callable[[], None] subscribe(HomeAssistant hass, str topic, MessageCallbackType msg_callback, int qos=DEFAULT_QOS, str encoding="utf-8")