Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ecowitt Weather Stations."""
2 
3 from __future__ import annotations
4 
5 import dataclasses
6 from datetime import datetime
7 from typing import Final
8 
9 from aioecowitt import EcoWittSensor, EcoWittSensorTypes
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.const import (
18  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
19  CONCENTRATION_PARTS_PER_MILLION,
20  DEGREE,
21  LIGHT_LUX,
22  PERCENTAGE,
23  UV_INDEX,
24  EntityCategory,
25  UnitOfElectricPotential,
26  UnitOfIrradiance,
27  UnitOfLength,
28  UnitOfPrecipitationDepth,
29  UnitOfPressure,
30  UnitOfSpeed,
31  UnitOfTemperature,
32  UnitOfVolumetricFlux,
33 )
34 from homeassistant.core import HomeAssistant
35 from homeassistant.helpers.entity_platform import AddEntitiesCallback
36 from homeassistant.helpers.typing import StateType
37 from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
38 
39 from . import EcowittConfigEntry
40 from .entity import EcowittEntity
41 
42 _METRIC: Final = (
43  EcoWittSensorTypes.TEMPERATURE_C,
44  EcoWittSensorTypes.RAIN_COUNT_MM,
45  EcoWittSensorTypes.RAIN_RATE_MM,
46  EcoWittSensorTypes.LIGHTNING_DISTANCE_KM,
47  EcoWittSensorTypes.SPEED_KPH,
48  EcoWittSensorTypes.PRESSURE_HPA,
49 )
50 _IMPERIAL: Final = (
51  EcoWittSensorTypes.TEMPERATURE_F,
52  EcoWittSensorTypes.RAIN_COUNT_INCHES,
53  EcoWittSensorTypes.RAIN_RATE_INCHES,
54  EcoWittSensorTypes.LIGHTNING_DISTANCE_MILES,
55  EcoWittSensorTypes.SPEED_MPH,
56  EcoWittSensorTypes.PRESSURE_INHG,
57 )
58 
59 
60 ECOWITT_SENSORS_MAPPING: Final = {
61  EcoWittSensorTypes.HUMIDITY: SensorEntityDescription(
62  key="HUMIDITY",
63  device_class=SensorDeviceClass.HUMIDITY,
64  native_unit_of_measurement=PERCENTAGE,
65  state_class=SensorStateClass.MEASUREMENT,
66  ),
67  EcoWittSensorTypes.DEGREE: SensorEntityDescription(
68  key="DEGREE", native_unit_of_measurement=DEGREE
69  ),
70  EcoWittSensorTypes.WATT_METERS_SQUARED: SensorEntityDescription(
71  key="WATT_METERS_SQUARED",
72  device_class=SensorDeviceClass.IRRADIANCE,
73  native_unit_of_measurement=UnitOfIrradiance.WATTS_PER_SQUARE_METER,
74  state_class=SensorStateClass.MEASUREMENT,
75  ),
76  EcoWittSensorTypes.UV_INDEX: SensorEntityDescription(
77  key="UV_INDEX",
78  native_unit_of_measurement=UV_INDEX,
79  state_class=SensorStateClass.MEASUREMENT,
80  ),
81  EcoWittSensorTypes.PM25: SensorEntityDescription(
82  key="PM25",
83  device_class=SensorDeviceClass.PM25,
84  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
85  state_class=SensorStateClass.MEASUREMENT,
86  ),
87  EcoWittSensorTypes.PM10: SensorEntityDescription(
88  key="PM10",
89  device_class=SensorDeviceClass.PM10,
90  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
91  state_class=SensorStateClass.MEASUREMENT,
92  ),
93  EcoWittSensorTypes.BATTERY_PERCENTAGE: SensorEntityDescription(
94  key="BATTERY_PERCENTAGE",
95  device_class=SensorDeviceClass.BATTERY,
96  native_unit_of_measurement=PERCENTAGE,
97  state_class=SensorStateClass.MEASUREMENT,
98  entity_category=EntityCategory.DIAGNOSTIC,
99  ),
100  EcoWittSensorTypes.BATTERY_VOLTAGE: SensorEntityDescription(
101  key="BATTERY_VOLTAGE",
102  device_class=SensorDeviceClass.VOLTAGE,
103  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
104  state_class=SensorStateClass.MEASUREMENT,
105  entity_category=EntityCategory.DIAGNOSTIC,
106  ),
107  EcoWittSensorTypes.CO2_PPM: SensorEntityDescription(
108  key="CO2_PPM",
109  device_class=SensorDeviceClass.CO2,
110  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
111  state_class=SensorStateClass.MEASUREMENT,
112  ),
113  EcoWittSensorTypes.LUX: SensorEntityDescription(
114  key="LUX",
115  device_class=SensorDeviceClass.ILLUMINANCE,
116  native_unit_of_measurement=LIGHT_LUX,
117  state_class=SensorStateClass.MEASUREMENT,
118  ),
119  EcoWittSensorTypes.TIMESTAMP: SensorEntityDescription(
120  key="TIMESTAMP", device_class=SensorDeviceClass.TIMESTAMP
121  ),
122  EcoWittSensorTypes.VOLTAGE: SensorEntityDescription(
123  key="VOLTAGE",
124  device_class=SensorDeviceClass.VOLTAGE,
125  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
126  state_class=SensorStateClass.MEASUREMENT,
127  entity_category=EntityCategory.DIAGNOSTIC,
128  ),
129  EcoWittSensorTypes.LIGHTNING_COUNT: SensorEntityDescription(
130  key="LIGHTNING_COUNT",
131  native_unit_of_measurement="strikes",
132  state_class=SensorStateClass.TOTAL_INCREASING,
133  ),
134  EcoWittSensorTypes.TEMPERATURE_C: SensorEntityDescription(
135  key="TEMPERATURE_C",
136  device_class=SensorDeviceClass.TEMPERATURE,
137  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
138  state_class=SensorStateClass.MEASUREMENT,
139  ),
140  EcoWittSensorTypes.TEMPERATURE_F: SensorEntityDescription(
141  key="TEMPERATURE_F",
142  device_class=SensorDeviceClass.TEMPERATURE,
143  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
144  state_class=SensorStateClass.MEASUREMENT,
145  ),
146  EcoWittSensorTypes.RAIN_COUNT_MM: SensorEntityDescription(
147  key="RAIN_COUNT_MM",
148  native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS,
149  device_class=SensorDeviceClass.PRECIPITATION,
150  state_class=SensorStateClass.TOTAL_INCREASING,
151  ),
152  EcoWittSensorTypes.RAIN_COUNT_INCHES: SensorEntityDescription(
153  key="RAIN_COUNT_INCHES",
154  native_unit_of_measurement=UnitOfPrecipitationDepth.INCHES,
155  device_class=SensorDeviceClass.PRECIPITATION,
156  state_class=SensorStateClass.TOTAL_INCREASING,
157  ),
158  EcoWittSensorTypes.RAIN_RATE_MM: SensorEntityDescription(
159  key="RAIN_RATE_MM",
160  native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
161  state_class=SensorStateClass.MEASUREMENT,
162  device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
163  ),
164  EcoWittSensorTypes.RAIN_RATE_INCHES: SensorEntityDescription(
165  key="RAIN_RATE_INCHES",
166  native_unit_of_measurement=UnitOfVolumetricFlux.INCHES_PER_HOUR,
167  state_class=SensorStateClass.MEASUREMENT,
168  device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
169  ),
170  EcoWittSensorTypes.LIGHTNING_DISTANCE_KM: SensorEntityDescription(
171  key="LIGHTNING_DISTANCE_KM",
172  native_unit_of_measurement=UnitOfLength.KILOMETERS,
173  state_class=SensorStateClass.MEASUREMENT,
174  ),
175  EcoWittSensorTypes.LIGHTNING_DISTANCE_MILES: SensorEntityDescription(
176  key="LIGHTNING_DISTANCE_MILES",
177  native_unit_of_measurement=UnitOfLength.MILES,
178  state_class=SensorStateClass.MEASUREMENT,
179  ),
180  EcoWittSensorTypes.SOIL_RAWADC: SensorEntityDescription(
181  key="SOIL_RAWADC",
182  entity_registry_enabled_default=False,
183  state_class=SensorStateClass.MEASUREMENT,
184  entity_category=EntityCategory.DIAGNOSTIC,
185  ),
186  EcoWittSensorTypes.SPEED_KPH: SensorEntityDescription(
187  key="SPEED_KPH",
188  device_class=SensorDeviceClass.WIND_SPEED,
189  native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
190  state_class=SensorStateClass.MEASUREMENT,
191  ),
192  EcoWittSensorTypes.SPEED_MPH: SensorEntityDescription(
193  key="SPEED_MPH",
194  device_class=SensorDeviceClass.WIND_SPEED,
195  native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
196  state_class=SensorStateClass.MEASUREMENT,
197  ),
198  EcoWittSensorTypes.PRESSURE_HPA: SensorEntityDescription(
199  key="PRESSURE_HPA",
200  device_class=SensorDeviceClass.PRESSURE,
201  native_unit_of_measurement=UnitOfPressure.HPA,
202  state_class=SensorStateClass.MEASUREMENT,
203  ),
204  EcoWittSensorTypes.PRESSURE_INHG: SensorEntityDescription(
205  key="PRESSURE_INHG",
206  device_class=SensorDeviceClass.PRESSURE,
207  native_unit_of_measurement=UnitOfPressure.INHG,
208  state_class=SensorStateClass.MEASUREMENT,
209  ),
210  EcoWittSensorTypes.PERCENTAGE: SensorEntityDescription(
211  key="PERCENTAGE",
212  native_unit_of_measurement=PERCENTAGE,
213  state_class=SensorStateClass.MEASUREMENT,
214  ),
215 }
216 
217 
219  hass: HomeAssistant,
220  entry: EcowittConfigEntry,
221  async_add_entities: AddEntitiesCallback,
222 ) -> None:
223  """Add sensors if new."""
224  ecowitt = entry.runtime_data
225 
226  def _new_sensor(sensor: EcoWittSensor) -> None:
227  """Add new sensor."""
228  if sensor.stype not in ECOWITT_SENSORS_MAPPING:
229  return
230 
231  # Ignore metrics that are not supported by the user's locale
232  if sensor.stype in _METRIC and hass.config.units is not METRIC_SYSTEM:
233  return
234  if sensor.stype in _IMPERIAL and hass.config.units is not US_CUSTOMARY_SYSTEM:
235  return
236  mapping = ECOWITT_SENSORS_MAPPING[sensor.stype]
237 
238  # Setup sensor description
239  description = dataclasses.replace(
240  mapping,
241  key=sensor.key,
242  name=sensor.name,
243  )
244 
245  # Hourly rain doesn't reset to fixed hours, it must be measurement state classes
246  if sensor.key in (
247  "hrain_piezomm",
248  "hrain_piezo",
249  "hourlyrainmm",
250  "hourlyrainin",
251  ):
252  description = dataclasses.replace(
253  description,
254  state_class=SensorStateClass.MEASUREMENT,
255  )
256 
257  async_add_entities([EcowittSensorEntity(sensor, description)])
258 
259  ecowitt.new_sensor_cb.append(_new_sensor)
260  entry.async_on_unload(lambda: ecowitt.new_sensor_cb.remove(_new_sensor))
261 
262  # Add all sensors that are already known
263  for sensor in ecowitt.sensors.values():
264  _new_sensor(sensor)
265 
266 
268  """Representation of a Ecowitt Sensor."""
269 
270  def __init__(
271  self, sensor: EcoWittSensor, description: SensorEntityDescription
272  ) -> None:
273  """Initialize the sensor."""
274  super().__init__(sensor)
275  self.entity_descriptionentity_description = description
276 
277  @property
278  def native_value(self) -> StateType | datetime:
279  """Return the state of the sensor."""
280  return self.ecowitt.value
None __init__(self, EcoWittSensor sensor, SensorEntityDescription description)
Definition: sensor.py:272
None async_setup_entry(HomeAssistant hass, EcowittConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:222
None _new_sensor(EcoWittSensor sensor)
Definition: sensor.py:226