Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity for the Chacon Dio entity."""
2 
3 import logging
4 from typing import Any
5 
6 from dio_chacon_wifi_api import DIOChaconAPIClient
7 
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.entity import Entity
10 
11 from .const import DOMAIN, MANUFACTURER
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 
17  """Implements a common class elements representing the Chacon Dio entity."""
18 
19  _attr_should_poll = False
20  _attr_has_entity_name = True
21 
22  def __init__(self, client: DIOChaconAPIClient, device: dict[str, Any]) -> None:
23  """Initialize Chacon Dio entity."""
24 
25  self.clientclient = client
26 
27  self.target_id: str = device["id"]
28  self._attr_unique_id_attr_unique_id = self.target_id
29  self._attr_device_info: DeviceInfo | None = DeviceInfo(
30  identifiers={(DOMAIN, self.target_id)},
31  manufacturer=MANUFACTURER,
32  name=device["name"],
33  model=device["model"],
34  )
35 
36  self._update_attr_update_attr(device)
37 
38  def _update_attr(self, data: dict[str, Any]) -> None:
39  """Recomputes the attributes values."""
40 
41  async def async_added_to_hass(self) -> None:
42  """Register the callback for server side events."""
43  await super().async_added_to_hass()
44  self.clientclient.set_callback_device_state_by_device(
45  self.target_id, self.callback_device_statecallback_device_state
46  )
47 
48  def callback_device_state(self, data: dict[str, Any]) -> None:
49  """Receive callback for device state notification pushed from the server."""
50 
51  _LOGGER.debug("Data received from server %s", data)
52  self._update_attr_update_attr(data)
53  self.async_write_ha_stateasync_write_ha_state()
54 
55  async def async_update(self) -> None:
56  """Update the state when the entity is requested to."""
57 
58  _LOGGER.debug("Update called for %s, %s", self, self.target_id)
59  data = await self.clientclient.get_status_details([self.target_id])
60  _LOGGER.debug("Received data from server %s", data)
61  self._update_attr_update_attr(data[self.target_id])
None __init__(self, DIOChaconAPIClient client, dict[str, Any] device)
Definition: entity.py:22
None _update_attr(self, dict[str, Any] data)
Definition: entity.py:38
None callback_device_state(self, dict[str, Any] data)
Definition: entity.py:48