Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for voltage, power & energy sensors for VeSync outlets."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 
9 from pyvesync.vesyncfan import VeSyncAirBypass
10 from pyvesync.vesyncoutlet import VeSyncOutlet
11 from pyvesync.vesyncswitch import VeSyncSwitch
12 
14  SensorDeviceClass,
15  SensorEntity,
16  SensorEntityDescription,
17  SensorStateClass,
18 )
19 from homeassistant.config_entries import ConfigEntry
20 from homeassistant.const import (
21  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
22  PERCENTAGE,
23  EntityCategory,
24  UnitOfElectricPotential,
25  UnitOfEnergy,
26  UnitOfPower,
27 )
28 from homeassistant.core import HomeAssistant, callback
29 from homeassistant.helpers.dispatcher import async_dispatcher_connect
30 from homeassistant.helpers.entity_platform import AddEntitiesCallback
31 from homeassistant.helpers.typing import StateType
32 
33 from .const import DEV_TYPE_TO_HA, DOMAIN, SKU_TO_BASE_DEVICE, VS_DISCOVERY, VS_SENSORS
34 from .entity import VeSyncBaseEntity
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
39 @dataclass(frozen=True, kw_only=True)
41  """Describe VeSync sensor entity."""
42 
43  value_fn: Callable[[VeSyncAirBypass | VeSyncOutlet | VeSyncSwitch], StateType]
44 
45  exists_fn: Callable[[VeSyncAirBypass | VeSyncOutlet | VeSyncSwitch], bool] = (
46  lambda _: True
47  )
48  update_fn: Callable[[VeSyncAirBypass | VeSyncOutlet | VeSyncSwitch], None] = (
49  lambda _: None
50  )
51 
52 
53 def update_energy(device):
54  """Update outlet details and energy usage."""
55  device.update()
56  device.update_energy()
57 
58 
59 def sku_supported(device, supported):
60  """Get the base device of which a device is an instance."""
61  return SKU_TO_BASE_DEVICE.get(device.device_type) in supported
62 
63 
64 def ha_dev_type(device):
65  """Get the homeassistant device_type for a given device."""
66  return DEV_TYPE_TO_HA.get(device.device_type)
67 
68 
69 FILTER_LIFE_SUPPORTED = [
70  "LV-PUR131S",
71  "Core200S",
72  "Core300S",
73  "Core400S",
74  "Core600S",
75  "EverestAir",
76  "Vital100S",
77  "Vital200S",
78 ]
79 AIR_QUALITY_SUPPORTED = [
80  "LV-PUR131S",
81  "Core300S",
82  "Core400S",
83  "Core600S",
84  "Vital100S",
85  "Vital200S",
86 ]
87 PM25_SUPPORTED = [
88  "Core300S",
89  "Core400S",
90  "Core600S",
91  "EverestAir",
92  "Vital100S",
93  "Vital200S",
94 ]
95 
96 SENSORS: tuple[VeSyncSensorEntityDescription, ...] = (
98  key="filter-life",
99  translation_key="filter_life",
100  native_unit_of_measurement=PERCENTAGE,
101  state_class=SensorStateClass.MEASUREMENT,
102  entity_category=EntityCategory.DIAGNOSTIC,
103  value_fn=lambda device: device.filter_life,
104  exists_fn=lambda device: sku_supported(device, FILTER_LIFE_SUPPORTED),
105  ),
107  key="air-quality",
108  translation_key="air_quality",
109  value_fn=lambda device: device.details["air_quality"],
110  exists_fn=lambda device: sku_supported(device, AIR_QUALITY_SUPPORTED),
111  ),
113  key="pm25",
114  device_class=SensorDeviceClass.PM25,
115  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
116  state_class=SensorStateClass.MEASUREMENT,
117  value_fn=lambda device: device.details["air_quality_value"],
118  exists_fn=lambda device: sku_supported(device, PM25_SUPPORTED),
119  ),
121  key="power",
122  translation_key="current_power",
123  device_class=SensorDeviceClass.POWER,
124  native_unit_of_measurement=UnitOfPower.WATT,
125  state_class=SensorStateClass.MEASUREMENT,
126  value_fn=lambda device: device.details["power"],
127  update_fn=update_energy,
128  exists_fn=lambda device: ha_dev_type(device) == "outlet",
129  ),
131  key="energy",
132  translation_key="energy_today",
133  device_class=SensorDeviceClass.ENERGY,
134  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
135  state_class=SensorStateClass.TOTAL_INCREASING,
136  value_fn=lambda device: device.energy_today,
137  update_fn=update_energy,
138  exists_fn=lambda device: ha_dev_type(device) == "outlet",
139  ),
141  key="energy-weekly",
142  translation_key="energy_week",
143  device_class=SensorDeviceClass.ENERGY,
144  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
145  state_class=SensorStateClass.TOTAL_INCREASING,
146  value_fn=lambda device: device.weekly_energy_total,
147  update_fn=update_energy,
148  exists_fn=lambda device: ha_dev_type(device) == "outlet",
149  ),
151  key="energy-monthly",
152  translation_key="energy_month",
153  device_class=SensorDeviceClass.ENERGY,
154  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
155  state_class=SensorStateClass.TOTAL_INCREASING,
156  value_fn=lambda device: device.monthly_energy_total,
157  update_fn=update_energy,
158  exists_fn=lambda device: ha_dev_type(device) == "outlet",
159  ),
161  key="energy-yearly",
162  translation_key="energy_year",
163  device_class=SensorDeviceClass.ENERGY,
164  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
165  state_class=SensorStateClass.TOTAL_INCREASING,
166  value_fn=lambda device: device.yearly_energy_total,
167  update_fn=update_energy,
168  exists_fn=lambda device: ha_dev_type(device) == "outlet",
169  ),
171  key="voltage",
172  translation_key="current_voltage",
173  device_class=SensorDeviceClass.VOLTAGE,
174  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
175  state_class=SensorStateClass.MEASUREMENT,
176  value_fn=lambda device: device.details["voltage"],
177  update_fn=update_energy,
178  exists_fn=lambda device: ha_dev_type(device) == "outlet",
179  ),
180 )
181 
182 
184  hass: HomeAssistant,
185  config_entry: ConfigEntry,
186  async_add_entities: AddEntitiesCallback,
187 ) -> None:
188  """Set up switches."""
189 
190  @callback
191  def discover(devices):
192  """Add new devices to platform."""
193  _setup_entities(devices, async_add_entities)
194 
195  config_entry.async_on_unload(
196  async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SENSORS), discover)
197  )
198 
199  _setup_entities(hass.data[DOMAIN][VS_SENSORS], async_add_entities)
200 
201 
202 @callback
203 def _setup_entities(devices, async_add_entities):
204  """Check if device is online and add entity."""
206  (
207  VeSyncSensorEntity(dev, description)
208  for dev in devices
209  for description in SENSORS
210  if description.exists_fn(dev)
211  ),
212  update_before_add=True,
213  )
214 
215 
217  """Representation of a sensor describing a VeSync device."""
218 
219  entity_description: VeSyncSensorEntityDescription
220 
221  def __init__(
222  self,
223  device: VeSyncAirBypass | VeSyncOutlet | VeSyncSwitch,
224  description: VeSyncSensorEntityDescription,
225  ) -> None:
226  """Initialize the VeSync outlet device."""
227  super().__init__(device)
228  self.entity_descriptionentity_description = description
229  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}-{description.key}"
230 
231  @property
232  def native_value(self) -> StateType:
233  """Return the state of the sensor."""
234  return self.entity_descriptionentity_description.value_fn(self.devicedevice)
235 
236  def update(self) -> None:
237  """Run the update function defined for the sensor."""
238  return self.entity_descriptionentity_description.update_fn(self.devicedevice)
None __init__(self, VeSyncAirBypass|VeSyncOutlet|VeSyncSwitch device, VeSyncSensorEntityDescription description)
Definition: sensor.py:225
list[tuple[str, int]] discover(HomeAssistant hass)
Definition: config_flow.py:97
def _setup_entities(devices, async_add_entities)
Definition: sensor.py:203
def sku_supported(device, supported)
Definition: sensor.py:59
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:187
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103