Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensors for the Elexa Guardian integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  EntityCategory,
18  UnitOfElectricCurrent,
19  UnitOfElectricPotential,
20  UnitOfTemperature,
21  UnitOfTime,
22 )
23 from homeassistant.core import HomeAssistant, callback
24 from homeassistant.helpers.dispatcher import async_dispatcher_connect
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import StateType
27 
28 from . import GuardianData
29 from .const import (
30  API_SYSTEM_DIAGNOSTICS,
31  API_SYSTEM_ONBOARD_SENSOR_STATUS,
32  API_VALVE_STATUS,
33  CONF_UID,
34  DOMAIN,
35  SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
36 )
37 from .entity import (
38  PairedSensorEntity,
39  ValveControllerEntity,
40  ValveControllerEntityDescription,
41 )
42 
43 SENSOR_KIND_AVG_CURRENT = "average_current"
44 SENSOR_KIND_BATTERY = "battery"
45 SENSOR_KIND_INST_CURRENT = "instantaneous_current"
46 SENSOR_KIND_INST_CURRENT_DDT = "instantaneous_current_ddt"
47 SENSOR_KIND_TEMPERATURE = "temperature"
48 SENSOR_KIND_TRAVEL_COUNT = "travel_count"
49 SENSOR_KIND_UPTIME = "uptime"
50 
51 
52 @dataclass(frozen=True, kw_only=True)
54  """Describe a Guardian paired sensor."""
55 
56  value_fn: Callable[[dict[str, Any]], StateType]
57 
58 
59 @dataclass(frozen=True, kw_only=True)
61  SensorEntityDescription, ValveControllerEntityDescription
62 ):
63  """Describe a Guardian valve controller sensor."""
64 
65  value_fn: Callable[[dict[str, Any]], StateType]
66 
67 
68 PAIRED_SENSOR_DESCRIPTIONS = (
70  key=SENSOR_KIND_BATTERY,
71  device_class=SensorDeviceClass.VOLTAGE,
72  entity_category=EntityCategory.DIAGNOSTIC,
73  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
74  value_fn=lambda data: data["battery"],
75  ),
77  key=SENSOR_KIND_TEMPERATURE,
78  device_class=SensorDeviceClass.TEMPERATURE,
79  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
80  state_class=SensorStateClass.MEASUREMENT,
81  value_fn=lambda data: data["temperature"],
82  ),
83 )
84 VALVE_CONTROLLER_DESCRIPTIONS = (
86  key=SENSOR_KIND_AVG_CURRENT,
87  translation_key="current",
88  device_class=SensorDeviceClass.CURRENT,
89  entity_category=EntityCategory.DIAGNOSTIC,
90  native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
91  api_category=API_VALVE_STATUS,
92  value_fn=lambda data: data["average_current"],
93  ),
95  key=SENSOR_KIND_INST_CURRENT,
96  translation_key="instantaneous_current",
97  device_class=SensorDeviceClass.CURRENT,
98  entity_category=EntityCategory.DIAGNOSTIC,
99  native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
100  api_category=API_VALVE_STATUS,
101  value_fn=lambda data: data["instantaneous_current"],
102  ),
104  key=SENSOR_KIND_INST_CURRENT_DDT,
105  translation_key="instantaneous_current_ddt",
106  device_class=SensorDeviceClass.CURRENT,
107  entity_category=EntityCategory.DIAGNOSTIC,
108  native_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
109  api_category=API_VALVE_STATUS,
110  value_fn=lambda data: data["instantaneous_current_ddt"],
111  ),
113  key=SENSOR_KIND_TEMPERATURE,
114  device_class=SensorDeviceClass.TEMPERATURE,
115  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
116  state_class=SensorStateClass.MEASUREMENT,
117  api_category=API_SYSTEM_ONBOARD_SENSOR_STATUS,
118  value_fn=lambda data: data["temperature"],
119  ),
121  key=SENSOR_KIND_UPTIME,
122  translation_key="uptime",
123  entity_category=EntityCategory.DIAGNOSTIC,
124  native_unit_of_measurement=UnitOfTime.MINUTES,
125  api_category=API_SYSTEM_DIAGNOSTICS,
126  value_fn=lambda data: data["uptime"],
127  ),
129  key=SENSOR_KIND_TRAVEL_COUNT,
130  translation_key="travel_count",
131  entity_category=EntityCategory.DIAGNOSTIC,
132  native_unit_of_measurement="revolutions",
133  api_category=API_VALVE_STATUS,
134  value_fn=lambda data: data["travel_count"],
135  ),
136 )
137 
138 
140  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
141 ) -> None:
142  """Set up Guardian switches based on a config entry."""
143  data: GuardianData = hass.data[DOMAIN][entry.entry_id]
144 
145  @callback
146  def add_new_paired_sensor(uid: str) -> None:
147  """Add a new paired sensor."""
150  entry, data.paired_sensor_manager.coordinators[uid], description
151  )
152  for description in PAIRED_SENSOR_DESCRIPTIONS
153  )
154 
155  # Handle adding paired sensors after HASS startup:
156  entry.async_on_unload(
158  hass,
159  SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
160  add_new_paired_sensor,
161  )
162  )
163 
164  # Add all valve controller-specific binary sensors:
165  sensors: list[PairedSensorSensor | ValveControllerSensor] = [
166  ValveControllerSensor(entry, data.valve_controller_coordinators, description)
167  for description in VALVE_CONTROLLER_DESCRIPTIONS
168  ]
169 
170  # Add all paired sensor-specific binary sensors:
171  sensors.extend(
172  [
173  PairedSensorSensor(entry, coordinator, description)
174  for coordinator in data.paired_sensor_manager.coordinators.values()
175  for description in PAIRED_SENSOR_DESCRIPTIONS
176  ]
177  )
178 
179  async_add_entities(sensors)
180 
181 
183  """Define a binary sensor related to a Guardian valve controller."""
184 
185  entity_description: PairedSensorDescription
186 
187  @property
188  def native_value(self) -> StateType:
189  """Return the value reported by the sensor."""
190  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
191 
192 
194  """Define a generic Guardian sensor."""
195 
196  entity_description: ValveControllerSensorDescription
197 
198  @property
199  def native_value(self) -> StateType:
200  """Return the value reported by the sensor."""
201  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:141
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103