Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """The Wolf SmartSet sensors."""
2 
3 from __future__ import annotations
4 
5 from wolf_comm.models import (
6  HoursParameter,
7  ListItemParameter,
8  Parameter,
9  PercentageParameter,
10  Pressure,
11  SimpleParameter,
12  Temperature,
13 )
14 
15 from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import UnitOfPressure, UnitOfTemperature, UnitOfTime
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.device_registry import DeviceInfo
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.update_coordinator import CoordinatorEntity
22 
23 from .const import COORDINATOR, DEVICE_ID, DOMAIN, MANUFACTURER, PARAMETERS, STATES
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up all entries for Wolf Platform."""
32 
33  coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
34  parameters = hass.data[DOMAIN][config_entry.entry_id][PARAMETERS]
35  device_id = hass.data[DOMAIN][config_entry.entry_id][DEVICE_ID]
36 
37  entities: list[WolfLinkSensor] = []
38  for parameter in parameters:
39  if isinstance(parameter, Temperature):
40  entities.append(WolfLinkTemperature(coordinator, parameter, device_id))
41  if isinstance(parameter, Pressure):
42  entities.append(WolfLinkPressure(coordinator, parameter, device_id))
43  if isinstance(parameter, PercentageParameter):
44  entities.append(WolfLinkPercentage(coordinator, parameter, device_id))
45  if isinstance(parameter, ListItemParameter):
46  entities.append(WolfLinkState(coordinator, parameter, device_id))
47  if isinstance(parameter, HoursParameter):
48  entities.append(WolfLinkHours(coordinator, parameter, device_id))
49  if isinstance(parameter, SimpleParameter):
50  entities.append(WolfLinkSensor(coordinator, parameter, device_id))
51 
52  async_add_entities(entities, True)
53 
54 
56  """Base class for all Wolf entities."""
57 
58  def __init__(self, coordinator, wolf_object: Parameter, device_id) -> None:
59  """Initialize."""
60  super().__init__(coordinator)
61  self.wolf_objectwolf_object = wolf_object
62  self._attr_name_attr_name = wolf_object.name
63  self._attr_unique_id_attr_unique_id = f"{device_id}:{wolf_object.parameter_id}"
64  self._state_state = None
65  self._attr_device_info_attr_device_info = DeviceInfo(
66  identifiers={(DOMAIN, str(device_id))},
67  configuration_url="https://www.wolf-smartset.com/",
68  manufacturer=MANUFACTURER,
69  )
70 
71  @property
72  def native_value(self):
73  """Return the state. Wolf Client is returning only changed values so we need to store old value here."""
74  if self.wolf_objectwolf_object.parameter_id in self.coordinator.data:
75  new_state = self.coordinator.data[self.wolf_objectwolf_object.parameter_id]
76  self.wolf_objectwolf_object.value_id = new_state[0]
77  self._state_state = new_state[1]
78  return self._state_state
79 
80  @property
82  """Return the state attributes."""
83  return {
84  "parameter_id": self.wolf_objectwolf_object.parameter_id,
85  "value_id": self.wolf_objectwolf_object.value_id,
86  "parent": self.wolf_objectwolf_object.parent,
87  }
88 
89 
91  """Class for hour based entities."""
92 
93  _attr_icon = "mdi:clock"
94  _attr_native_unit_of_measurement = UnitOfTime.HOURS
95 
96 
98  """Class for temperature based entities."""
99 
100  _attr_device_class = SensorDeviceClass.TEMPERATURE
101  _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
102 
103 
105  """Class for pressure based entities."""
106 
107  _attr_device_class = SensorDeviceClass.PRESSURE
108  _attr_native_unit_of_measurement = UnitOfPressure.BAR
109 
110 
112  """Class for percentage based entities."""
113 
114  @property
116  """Return the unit the value is expressed in."""
117  return self.wolf_objectwolf_object.unit
118 
119 
121  """Class for entities which has defined list of state."""
122 
123  _attr_translation_key = "state"
124 
125  @property
126  def native_value(self):
127  """Return the state converting with supported values."""
128  state = super().native_value
129  if state is not None:
130  resolved_state = [
131  item for item in self.wolf_objectwolf_object.items if item.value == int(state)
132  ]
133  if resolved_state:
134  resolved_name = resolved_state[0].name
135  return STATES.get(resolved_name, resolved_name)
136  return state