Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensors for cloud based weatherflow."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import UTC, datetime
8 
9 from weatherflow4py.models.rest.observation import Observation
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15  SensorStateClass,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import UnitOfLength, UnitOfPressure, UnitOfTemperature
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import StateType
22 
23 from .const import DOMAIN
24 from .coordinator import WeatherFlowCloudDataUpdateCoordinator
25 from .entity import WeatherFlowCloudEntity
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  SensorEntityDescription,
31 ):
32  """Describes a weatherflow sensor."""
33 
34  value_fn: Callable[[Observation], StateType | datetime]
35 
36 
37 WF_SENSORS: tuple[WeatherFlowCloudSensorEntityDescription, ...] = (
38  # Air Sensors
40  key="air_density",
41  translation_key="air_density",
42  state_class=SensorStateClass.MEASUREMENT,
43  suggested_display_precision=5,
44  value_fn=lambda data: data.air_density,
45  native_unit_of_measurement="kg/m³",
46  ),
47  # Temp Sensors
49  key="air_temperature",
50  translation_key="air_temperature",
51  device_class=SensorDeviceClass.TEMPERATURE,
52  state_class=SensorStateClass.MEASUREMENT,
53  suggested_display_precision=1,
54  value_fn=lambda data: data.air_temperature,
55  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
56  ),
58  key="dew_point",
59  translation_key="dew_point",
60  value_fn=lambda data: data.dew_point,
61  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
62  device_class=SensorDeviceClass.TEMPERATURE,
63  state_class=SensorStateClass.MEASUREMENT,
64  suggested_display_precision=1,
65  ),
67  key="feels_like",
68  translation_key="feels_like",
69  device_class=SensorDeviceClass.TEMPERATURE,
70  state_class=SensorStateClass.MEASUREMENT,
71  suggested_display_precision=1,
72  value_fn=lambda data: data.feels_like,
73  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
74  ),
76  key="heat_index",
77  translation_key="heat_index",
78  device_class=SensorDeviceClass.TEMPERATURE,
79  state_class=SensorStateClass.MEASUREMENT,
80  suggested_display_precision=1,
81  value_fn=lambda data: data.heat_index,
82  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
83  ),
85  key="wind_chill",
86  translation_key="wind_chill",
87  device_class=SensorDeviceClass.TEMPERATURE,
88  state_class=SensorStateClass.MEASUREMENT,
89  suggested_display_precision=1,
90  value_fn=lambda data: data.wind_chill,
91  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
92  ),
94  key="wet_bulb_temperature",
95  translation_key="wet_bulb_temperature",
96  device_class=SensorDeviceClass.TEMPERATURE,
97  state_class=SensorStateClass.MEASUREMENT,
98  suggested_display_precision=1,
99  value_fn=lambda data: data.wet_bulb_temperature,
100  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
101  ),
103  key="wet_bulb_globe_temperature",
104  translation_key="wet_bulb_globe_temperature",
105  device_class=SensorDeviceClass.TEMPERATURE,
106  state_class=SensorStateClass.MEASUREMENT,
107  suggested_display_precision=1,
108  value_fn=lambda data: data.wet_bulb_globe_temperature,
109  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
110  ),
111  # Pressure Sensors
113  key="barometric_pressure",
114  translation_key="barometric_pressure",
115  value_fn=lambda data: data.barometric_pressure,
116  native_unit_of_measurement=UnitOfPressure.MBAR,
117  device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE,
118  state_class=SensorStateClass.MEASUREMENT,
119  suggested_display_precision=3,
120  ),
122  key="sea_level_pressure",
123  translation_key="sea_level_pressure",
124  value_fn=lambda data: data.sea_level_pressure,
125  native_unit_of_measurement=UnitOfPressure.MBAR,
126  device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE,
127  state_class=SensorStateClass.MEASUREMENT,
128  suggested_display_precision=3,
129  ),
130  # Lightning Sensors
132  key="lightning_strike_count",
133  translation_key="lightning_strike_count",
134  state_class=SensorStateClass.TOTAL,
135  value_fn=lambda data: data.lightning_strike_count,
136  ),
138  key="lightning_strike_count_last_1hr",
139  translation_key="lightning_strike_count_last_1hr",
140  state_class=SensorStateClass.TOTAL,
141  value_fn=lambda data: data.lightning_strike_count_last_1hr,
142  ),
144  key="lightning_strike_count_last_3hr",
145  translation_key="lightning_strike_count_last_3hr",
146  state_class=SensorStateClass.TOTAL,
147  value_fn=lambda data: data.lightning_strike_count_last_3hr,
148  ),
150  key="lightning_strike_last_distance",
151  translation_key="lightning_strike_last_distance",
152  state_class=SensorStateClass.MEASUREMENT,
153  device_class=SensorDeviceClass.DISTANCE,
154  native_unit_of_measurement=UnitOfLength.KILOMETERS,
155  value_fn=lambda data: data.lightning_strike_last_distance,
156  ),
158  key="lightning_strike_last_epoch",
159  translation_key="lightning_strike_last_epoch",
160  device_class=SensorDeviceClass.TIMESTAMP,
161  value_fn=(
162  lambda data: datetime.fromtimestamp(
163  data.lightning_strike_last_epoch, tz=UTC
164  )
165  if data.lightning_strike_last_epoch is not None
166  else None
167  ),
168  ),
169 )
170 
171 
173  hass: HomeAssistant,
174  entry: ConfigEntry,
175  async_add_entities: AddEntitiesCallback,
176 ) -> None:
177  """Set up WeatherFlow sensors based on a config entry."""
178 
179  coordinator: WeatherFlowCloudDataUpdateCoordinator = hass.data[DOMAIN][
180  entry.entry_id
181  ]
182 
184  WeatherFlowCloudSensor(coordinator, sensor_description, station_id)
185  for station_id in coordinator.data
186  for sensor_description in WF_SENSORS
187  )
188 
189 
191  """Implementation of a WeatherFlow sensor."""
192 
193  entity_description: WeatherFlowCloudSensorEntityDescription
194 
195  def __init__(
196  self,
197  coordinator: WeatherFlowCloudDataUpdateCoordinator,
198  description: WeatherFlowCloudSensorEntityDescription,
199  station_id: int,
200  ) -> None:
201  """Initialize the sensor."""
202  # Initialize the Entity Class
203  super().__init__(coordinator, station_id)
204  self.entity_descriptionentity_description = description
205  self._attr_unique_id_attr_unique_id = f"{station_id}_{description.key}"
206 
207  @property
208  def native_value(self) -> StateType | datetime:
209  """Return the state of the sensor."""
210  return self.entity_descriptionentity_description.value_fn(self.stationstation.observation.obs[0])
None __init__(self, WeatherFlowCloudDataUpdateCoordinator coordinator, WeatherFlowCloudSensorEntityDescription description, int station_id)
Definition: sensor.py:200
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:176