Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Oncue sensors."""
2 
3 from __future__ import annotations
4 
5 from aiooncue import OncueDevice, OncueSensor
6 
7 from homeassistant.const import ATTR_CONNECTIONS
8 from homeassistant.helpers import device_registry as dr
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity import Entity, EntityDescription
12  CoordinatorEntity,
13  DataUpdateCoordinator,
14 )
15 
16 from .const import CONNECTION_ESTABLISHED_KEY, DOMAIN, VALUE_UNAVAILABLE
17 
18 
20  CoordinatorEntity[DataUpdateCoordinator[dict[str, OncueDevice]]], Entity
21 ):
22  """Representation of an Oncue entity."""
23 
24  _attr_has_entity_name = True
25 
26  def __init__(
27  self,
28  coordinator: DataUpdateCoordinator[dict[str, OncueDevice]],
29  device_id: str,
30  device: OncueDevice,
31  sensor: OncueSensor,
32  description: EntityDescription,
33  ) -> None:
34  """Initialize the sensor."""
35  super().__init__(coordinator)
36  self.entity_descriptionentity_description = description
37  self._device_id_device_id = device_id
38  self._attr_unique_id_attr_unique_id = f"{device_id}_{description.key}"
39  self._attr_name_attr_name = sensor.display_name
40  self._attr_device_info_attr_device_info = DeviceInfo(
41  identifiers={(DOMAIN, device_id)},
42  name=device.name,
43  hw_version=device.hardware_version,
44  sw_version=device.sensors["FirmwareVersion"].display_value,
45  model=device.sensors["GensetModelNumberSelect"].display_value,
46  manufacturer="Kohler",
47  )
48  try:
49  mac_address_hex = hex(int(device.sensors["MacAddress"].value))[2:]
50  except ValueError: # MacAddress may be invalid if the gateway is offline
51  return
52  self._attr_device_info_attr_device_info[ATTR_CONNECTIONS] = {
53  (dr.CONNECTION_NETWORK_MAC, mac_address_hex)
54  }
55 
56  @property
57  def _oncue_value(self) -> str:
58  """Return the sensor value."""
59  device: OncueDevice = self.coordinator.data[self._device_id_device_id]
60  sensor: OncueSensor = device.sensors[self.entity_descriptionentity_description.key]
61  return sensor.value
62 
63  @property
64  def available(self) -> bool:
65  """Return if entity is available."""
66  # The binary sensor that tracks the connection should not go unavailable.
67  if self.entity_descriptionentity_description.key != CONNECTION_ESTABLISHED_KEY:
68  # If Kohler returns -- the entity is unavailable.
69  if self._oncue_value_oncue_value_oncue_value == VALUE_UNAVAILABLE:
70  return False
71  # If the cloud is reporting that the generator is not connected
72  # this also indicates the data is not available.
73  # The battery voltage sensor reports 0.0 rather than
74  # -- hence the purpose of this check.
75  device: OncueDevice = self.coordinator.data[self._device_id_device_id]
76  conn_established: OncueSensor = device.sensors[CONNECTION_ESTABLISHED_KEY]
77  if (
78  conn_established is not None
79  and conn_established.value == VALUE_UNAVAILABLE
80  ):
81  return False
82  return super().available
None __init__(self, DataUpdateCoordinator[dict[str, OncueDevice]] coordinator, str device_id, OncueDevice device, OncueSensor sensor, EntityDescription description)
Definition: entity.py:33