Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The WiLight integration."""
2 
3 from typing import Any
4 
5 from pywilight.wilight_device import PyWiLightDevice
6 
7 from homeassistant.core import callback
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.entity import Entity
10 
11 from .const import DOMAIN
12 
13 
15  """Representation of a WiLight device.
16 
17  Contains the common logic for WiLight entities.
18  """
19 
20  _attr_should_poll = False
21  _attr_has_entity_name = True
22 
23  def __init__(self, api_device: PyWiLightDevice, index: str, item_name: str) -> None:
24  """Initialize the device."""
25  # WiLight specific attributes for every component type
26  self._device_id_device_id = api_device.device_id
27  self._client_client = api_device.client
28  self._index_index = index
29  self._status_status: dict[str, Any] = {}
30 
31  self._attr_unique_id_attr_unique_id = f"{self._device_id}_{index}"
32  self._attr_device_info_attr_device_info = DeviceInfo(
33  name=item_name,
34  identifiers={(DOMAIN, self._attr_unique_id_attr_unique_id)},
35  model=api_device.model,
36  manufacturer="WiLight",
37  sw_version=api_device.swversion,
38  via_device=(DOMAIN, self._device_id_device_id),
39  )
40 
41  @property
42  def available(self) -> bool:
43  """Return True if entity is available."""
44  return bool(self._client_client.is_connected)
45 
46  @callback
47  def handle_event_callback(self, states: dict[str, Any]) -> None:
48  """Propagate changes through ha."""
49  self._status_status = states
50  self.async_write_ha_stateasync_write_ha_state()
51 
52  async def async_update(self) -> None:
53  """Synchronize state with api_device."""
54  await self._client_client.status(self._index_index)
55 
56  async def async_added_to_hass(self) -> None:
57  """Register update callback."""
58  self._client_client.register_status_callback(self.handle_event_callbackhandle_event_callback, self._index_index)
59  await self._client_client.status(self._index_index)
None __init__(self, PyWiLightDevice api_device, str index, str item_name)
Definition: entity.py:23
None handle_event_callback(self, dict[str, Any] states)
Definition: entity.py:47