1 """Sensors for cloud based weatherflow."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import UTC, datetime
9 from weatherflow4py.models.rest.observation
import Observation
14 SensorEntityDescription,
23 from .const
import DOMAIN
24 from .coordinator
import WeatherFlowCloudDataUpdateCoordinator
25 from .entity
import WeatherFlowCloudEntity
28 @dataclass(frozen=True, kw_only=True)
30 SensorEntityDescription,
32 """Describes a weatherflow sensor."""
34 value_fn: Callable[[Observation], StateType | datetime]
37 WF_SENSORS: tuple[WeatherFlowCloudSensorEntityDescription, ...] = (
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³",
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
158 key=
"lightning_strike_last_epoch",
159 translation_key=
"lightning_strike_last_epoch",
160 device_class=SensorDeviceClass.TIMESTAMP,
162 lambda data: datetime.fromtimestamp(
163 data.lightning_strike_last_epoch, tz=UTC
165 if data.lightning_strike_last_epoch
is not None
175 async_add_entities: AddEntitiesCallback,
177 """Set up WeatherFlow sensors based on a config entry."""
179 coordinator: WeatherFlowCloudDataUpdateCoordinator = hass.data[DOMAIN][
185 for station_id
in coordinator.data
186 for sensor_description
in WF_SENSORS
191 """Implementation of a WeatherFlow sensor."""
193 entity_description: WeatherFlowCloudSensorEntityDescription
197 coordinator: WeatherFlowCloudDataUpdateCoordinator,
198 description: WeatherFlowCloudSensorEntityDescription,
201 """Initialize the sensor."""
203 super().
__init__(coordinator, station_id)
209 """Return the state of the sensor."""
WeatherFlowDataREST station(self)
None __init__(self, WeatherFlowCloudDataUpdateCoordinator coordinator, WeatherFlowCloudSensorEntityDescription description, int station_id)
StateType|datetime native_value(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)