Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """PlaatoEntity class."""
2 
3 from typing import Any
4 
5 from pyplaato.models.device import PlaatoDevice
6 
7 from homeassistant.helpers import entity
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 
11 from .const import (
12  DEVICE,
13  DEVICE_ID,
14  DEVICE_NAME,
15  DEVICE_TYPE,
16  DOMAIN,
17  EXTRA_STATE_ATTRIBUTES,
18  SENSOR_DATA,
19  SENSOR_SIGNAL,
20 )
21 
22 
23 class PlaatoEntity(entity.Entity):
24  """Representation of a Plaato Entity."""
25 
26  _attr_should_poll = False
27 
28  def __init__(self, data, sensor_type, coordinator=None):
29  """Initialize the sensor."""
30  self._coordinator_coordinator = coordinator
31  self._entry_data_entry_data = data
32  self._sensor_type_sensor_type = sensor_type
33  self._device_id_device_id = data[DEVICE][DEVICE_ID]
34  self._device_type_device_type = data[DEVICE][DEVICE_TYPE]
35  self._device_name_device_name = data[DEVICE][DEVICE_NAME]
36  self._attr_unique_id_attr_unique_id = f"{self._device_id}_{self._sensor_type}"
37  self._attr_name_attr_name = f"{DOMAIN} {self._device_type} {self._device_name} {self._sensor_name}".title()
38  sw_version = None
39  if firmware := self._sensor_data_sensor_data.firmware_version:
40  sw_version = firmware
41  self._attr_device_info_attr_device_info = DeviceInfo(
42  identifiers={(DOMAIN, self._device_id_device_id)},
43  manufacturer="Plaato",
44  model=self._device_type_device_type,
45  name=self._device_name_device_name,
46  sw_version=sw_version,
47  )
48 
49  @property
50  def _attributes(self) -> dict:
51  return PlaatoEntity._to_snake_case(self._sensor_data_sensor_data.attributes)
52 
53  @property
54  def _sensor_name(self) -> str:
55  return self._sensor_data_sensor_data.get_sensor_name(self._sensor_type_sensor_type)
56 
57  @property
58  def _sensor_data(self) -> PlaatoDevice:
59  if self._coordinator_coordinator:
60  return self._coordinator_coordinator.data
61  return self._entry_data_entry_data[SENSOR_DATA]
62 
63  @property
64  def extra_state_attributes(self) -> dict[str, Any] | None:
65  """Return the state attributes of the monitored installation."""
66  if self._attributes_attributes:
67  return {
68  attr_key: self._attributes_attributes[plaato_key]
69  for attr_key, plaato_key in EXTRA_STATE_ATTRIBUTES.items()
70  if plaato_key in self._attributes_attributes
71  and self._attributes_attributes[plaato_key] is not None
72  }
73  return None
74 
75  @property
76  def available(self):
77  """Return if sensor is available."""
78  if self._coordinator_coordinator is not None:
79  return self._coordinator_coordinator.last_update_success
80  return True
81 
82  async def async_added_to_hass(self):
83  """When entity is added to hass."""
84  if self._coordinator_coordinator is not None:
85  self.async_on_remove(
86  self._coordinator_coordinator.async_add_listener(self.async_write_ha_state)
87  )
88  else:
89  self.async_on_remove(
91  self.hass,
92  SENSOR_SIGNAL % (self._device_id_device_id, self._sensor_type_sensor_type),
93  self.async_write_ha_state,
94  )
95  )
96 
97  @staticmethod
98  def _to_snake_case(dictionary: dict):
99  return {k.lower().replace(" ", "_"): v for k, v in dictionary.items()}
dict[str, Any]|None extra_state_attributes(self)
Definition: entity.py:64
def __init__(self, data, sensor_type, coordinator=None)
Definition: entity.py:28
None async_add_listener(HomeAssistant hass, Callable[[], None] listener)
Definition: __init__.py:82
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103