Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for a device entity integrated in devolo Home Control."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from urllib.parse import urlparse
7 
8 from devolo_home_control_api.devices.zwave import Zwave
9 from devolo_home_control_api.homecontrol import HomeControl
10 
11 from homeassistant.components.sensor import SensorDeviceClass
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity import Entity
14 
15 from .const import DOMAIN
16 from .subscriber import Subscriber
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
22  """Abstract representation of a device within devolo Home Control."""
23 
24  _attr_has_entity_name = True
25 
26  def __init__(
27  self, homecontrol: HomeControl, device_instance: Zwave, element_uid: str
28  ) -> None:
29  """Initialize a devolo device entity."""
30  self._device_instance_device_instance = device_instance
31  self._homecontrol_homecontrol = homecontrol
32 
33  self._attr_available_attr_available = (
34  device_instance.is_online()
35  ) # This is not doing I/O. It fetches an internal state of the API
36  self._attr_should_poll_attr_should_poll = False
37  self._attr_unique_id_attr_unique_id = element_uid
38  self._attr_device_info_attr_device_info = DeviceInfo(
39  configuration_url=f"https://{urlparse(device_instance.href).netloc}",
40  identifiers={(DOMAIN, self._device_instance_device_instance.uid)},
41  manufacturer=device_instance.brand,
42  model=device_instance.name,
43  model_id=device_instance.identifier,
44  name=device_instance.settings_property["general_device_settings"].name,
45  suggested_area=device_instance.settings_property[
46  "general_device_settings"
47  ].zone,
48  )
49 
50  self.subscribersubscriber: Subscriber | None = None
51  self.sync_callbacksync_callback = self._sync_sync
52 
53  self._value_value: float
54 
55  async def async_added_to_hass(self) -> None:
56  """Call when entity is added to hass."""
57  assert self.device_infodevice_info
58  assert self.device_infodevice_info["name"] # The name was set on entity creation
59  self.subscribersubscriber = Subscriber(
60  self.device_infodevice_info["name"], callback=self.sync_callbacksync_callback
61  )
62  self._homecontrol_homecontrol.publisher.register(
63  self._device_instance_device_instance.uid, self.subscribersubscriber, self.sync_callbacksync_callback
64  )
65 
66  async def async_will_remove_from_hass(self) -> None:
67  """Call when entity is removed or disabled."""
68  self._homecontrol_homecontrol.publisher.unregister(
69  self._device_instance_device_instance.uid, self.subscribersubscriber
70  )
71 
72  def _sync(self, message: tuple) -> None:
73  """Update the state."""
74  if message[0] == self._attr_unique_id_attr_unique_id:
75  self._value_value = message[1]
76  else:
77  self._generic_message_generic_message(message)
78  self.schedule_update_ha_stateschedule_update_ha_state()
79 
80  def _generic_message(self, message: tuple) -> None:
81  """Handle generic messages."""
82  if (
83  len(message) == 3
84  and message[2] == "battery_level"
85  and self.device_classdevice_classdevice_class == SensorDeviceClass.BATTERY
86  ):
87  self._value_value = message[1]
88  elif len(message) == 3 and message[2] == "status":
89  # Maybe the API wants to tell us, that the device went on- or offline.
90  self._attr_available_attr_available = self._device_instance_device_instance.is_online()
91  else:
92  _LOGGER.debug("No valid message received: %s", message)
None __init__(self, HomeControl homecontrol, Zwave device_instance, str element_uid)
Definition: entity.py:28
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
DeviceInfo|None device_info(self)
Definition: entity.py:798