Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Code to handle a Livisi switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiolivisi.const import CAPABILITY_MAP
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import callback
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.update_coordinator import CoordinatorEntity
15 
16 from .const import DOMAIN, LIVISI_REACHABILITY_CHANGE
17 from .coordinator import LivisiDataUpdateCoordinator
18 
19 
20 class LivisiEntity(CoordinatorEntity[LivisiDataUpdateCoordinator]):
21  """Represents a base livisi entity."""
22 
23  _attr_has_entity_name = True
24 
25  def __init__(
26  self,
27  config_entry: ConfigEntry,
28  coordinator: LivisiDataUpdateCoordinator,
29  device: dict[str, Any],
30  *,
31  use_room_as_device_name: bool = False,
32  ) -> None:
33  """Initialize the common properties of a Livisi device."""
34  self.aio_livisiaio_livisi = coordinator.aiolivisi
35  self.capabilities: Mapping[str, Any] = device[CAPABILITY_MAP]
36 
37  name = device["config"]["name"]
38  unique_id = device["id"]
39 
40  room_id: str | None = device.get("location")
41  room_name: str | None = None
42  if room_id is not None:
43  room_name = coordinator.rooms.get(room_id)
44 
45  self._attr_available_attr_available = False
46  self._attr_unique_id_attr_unique_id = unique_id
47 
48  device_name = name
49 
50  # For livisi climate entities, the device should have the room name from
51  # the livisi setup, as each livisi room gets exactly one VRCC device. The entity
52  # name will always be some localized value of "Climate", so the full element name
53  # in homeassistent will be in the form of "Bedroom Climate"
54  if use_room_as_device_name and room_name is not None:
55  self._attr_name_attr_name = name
56  device_name = room_name
57 
58  self._attr_device_info_attr_device_info = DeviceInfo(
59  identifiers={(DOMAIN, unique_id)},
60  manufacturer=device["manufacturer"],
61  model=device["type"],
62  name=device_name,
63  suggested_area=room_name,
64  via_device=(DOMAIN, config_entry.entry_id),
65  )
66  super().__init__(coordinator)
67 
68  async def async_added_to_hass(self) -> None:
69  """Register callback for reachability."""
70  await super().async_added_to_hass()
71  self.async_on_remove(
73  self.hasshasshass,
74  f"{LIVISI_REACHABILITY_CHANGE}_{self.unique_id}",
75  self.update_reachabilityupdate_reachability,
76  )
77  )
78 
79  @callback
80  def update_reachability(self, is_reachable: bool) -> None:
81  """Update the reachability of the device."""
82  self._attr_available_attr_available = is_reachable
83  self.async_write_ha_state()
None update_reachability(self, bool is_reachable)
Definition: entity.py:80
None __init__(self, ConfigEntry config_entry, LivisiDataUpdateCoordinator coordinator, dict[str, Any] device, *bool use_room_as_device_name=False)
Definition: entity.py:32
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103