Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Parent class for every Overkiz device."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from pyoverkiz.enums import OverkizAttribute, OverkizState
8 from pyoverkiz.models import Device
9 
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity import EntityDescription
12 from homeassistant.helpers.update_coordinator import CoordinatorEntity
13 
14 from .const import DOMAIN
15 from .coordinator import OverkizDataUpdateCoordinator
16 from .executor import OverkizExecutor
17 
18 
19 class OverkizEntity(CoordinatorEntity[OverkizDataUpdateCoordinator]):
20  """Representation of an Overkiz device entity."""
21 
22  _attr_has_entity_name = True
23  _attr_name: str | None = None
24 
25  def __init__(
26  self, device_url: str, coordinator: OverkizDataUpdateCoordinator
27  ) -> None:
28  """Initialize the device."""
29  super().__init__(coordinator)
30  self.device_urldevice_url = device_url
31  split_device_url = self.device_urldevice_url.split("#")
32  self.base_device_urlbase_device_url = split_device_url[0]
33  if len(split_device_url) == 2:
34  self.index_device_urlindex_device_url = split_device_url[1]
35  self.executorexecutor = OverkizExecutor(device_url, coordinator)
36 
37  self._attr_assumed_state_attr_assumed_state = not self.devicedevice.states
38  self._attr_available_attr_available = self.devicedevice.available
39  self._attr_unique_id_attr_unique_id = self.devicedevice.device_url
40 
41  if self.is_sub_deviceis_sub_device:
42  # In case of sub entity, use the provided label as name
43  self._attr_name_attr_name = self.devicedevice.label
44 
45  self._attr_device_info_attr_device_info = self.generate_device_infogenerate_device_info()
46 
47  @property
48  def is_sub_device(self) -> bool:
49  """Return True if device is a sub device."""
50  return "#" in self.device_urldevice_url and not self.device_urldevice_url.endswith("#1")
51 
52  @property
53  def device(self) -> Device:
54  """Return Overkiz device linked to this entity."""
55  return self.coordinator.data[self.device_urldevice_url]
56 
57  def generate_device_info(self) -> DeviceInfo:
58  """Return device registry information for this entity."""
59  # Some devices, such as the Smart Thermostat have several devices
60  # in one physical device, with same device url, terminated by '#' and a number.
61  # In this case, we use the base device url as the device identifier.
62  if self.is_sub_deviceis_sub_device:
63  # Only return the url of the base device, to inherit device name
64  # and model from parent device.
65  return DeviceInfo(
66  identifiers={(DOMAIN, self.executorexecutor.base_device_url)},
67  )
68 
69  manufacturer = (
70  self.executorexecutor.select_attribute(OverkizAttribute.CORE_MANUFACTURER)
71  or self.executorexecutor.select_state(OverkizState.CORE_MANUFACTURER_NAME)
72  or self.coordinator.client.server.manufacturer
73  )
74 
75  model = (
76  self.executorexecutor.select_state(
77  OverkizState.CORE_MODEL,
78  OverkizState.CORE_PRODUCT_MODEL_NAME,
79  OverkizState.IO_MODEL,
80  )
81  or self.devicedevice.widget.value
82  )
83 
84  suggested_area = (
85  self.coordinator.areas[self.devicedevice.place_oid]
86  if self.coordinator.areas and self.devicedevice.place_oid
87  else None
88  )
89 
90  return DeviceInfo(
91  identifiers={(DOMAIN, self.executorexecutor.base_device_url)},
92  name=self.devicedevice.label,
93  manufacturer=str(manufacturer),
94  model=str(model),
95  sw_version=cast(
96  str,
97  self.executorexecutor.select_attribute(OverkizAttribute.CORE_FIRMWARE_REVISION),
98  ),
99  hw_version=self.devicedevice.controllable_name,
100  suggested_area=suggested_area,
101  via_device=(DOMAIN, self.executorexecutor.get_gateway_id()),
102  configuration_url=self.coordinator.client.server.configuration_url,
103  )
104 
105 
107  """Representation of a Overkiz device entity based on a description."""
108 
109  def __init__(
110  self,
111  device_url: str,
112  coordinator: OverkizDataUpdateCoordinator,
113  description: EntityDescription,
114  ) -> None:
115  """Initialize the device."""
116  super().__init__(device_url, coordinator)
117  self.entity_descriptionentity_description = description
118  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}-{self.entity_description.key}"
119 
120  if self.is_sub_deviceis_sub_device:
121  # In case of sub device, use the provided label
122  # and append the name of the type of entity
123  self._attr_name_attr_name_attr_name = f"{self.device.label} {description.name}"
124  elif isinstance(description.name, str):
125  self._attr_name_attr_name_attr_name = description.name
None __init__(self, str device_url, OverkizDataUpdateCoordinator coordinator, EntityDescription description)
Definition: entity.py:114
None __init__(self, str device_url, OverkizDataUpdateCoordinator coordinator)
Definition: entity.py:27