1 """Weather information for air and road temperature (by Trafikverket)."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
9 from pytrafikverket.models
import WeatherStationInfoModel
14 SensorEntityDescription,
32 from .
import TVWeatherConfigEntry
33 from .const
import ATTRIBUTION, CONF_STATION, DOMAIN
34 from .coordinator
import TVDataUpdateCoordinator
36 PRECIPITATION_TYPE = [
46 @dataclass(frozen=True, kw_only=True)
48 """Describes Trafikverket sensor entity."""
50 value_fn: Callable[[WeatherStationInfoModel], StateType | datetime]
54 """Add UTC timezone if datetime."""
56 return date_time.replace(tzinfo=dt_util.UTC)
60 SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
63 translation_key=
"air_temperature",
64 value_fn=
lambda data: data.air_temp,
65 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66 device_class=SensorDeviceClass.TEMPERATURE,
67 state_class=SensorStateClass.MEASUREMENT,
71 translation_key=
"road_temperature",
72 value_fn=
lambda data: data.road_temp,
73 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
74 device_class=SensorDeviceClass.TEMPERATURE,
75 state_class=SensorStateClass.MEASUREMENT,
79 translation_key=
"precipitation",
80 value_fn=
lambda data: data.precipitationtype,
81 entity_registry_enabled_default=
False,
82 options=PRECIPITATION_TYPE,
83 device_class=SensorDeviceClass.ENUM,
87 translation_key=
"wind_direction",
88 value_fn=
lambda data: data.winddirection,
89 native_unit_of_measurement=DEGREE,
90 state_class=SensorStateClass.MEASUREMENT,
94 value_fn=
lambda data: data.windforce,
95 native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
96 device_class=SensorDeviceClass.WIND_SPEED,
97 state_class=SensorStateClass.MEASUREMENT,
100 key=
"wind_speed_max",
101 translation_key=
"wind_speed_max",
102 value_fn=
lambda data: data.windforcemax,
103 native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
104 device_class=SensorDeviceClass.WIND_SPEED,
105 entity_registry_enabled_default=
False,
106 state_class=SensorStateClass.MEASUREMENT,
110 value_fn=
lambda data: data.humidity,
111 native_unit_of_measurement=PERCENTAGE,
112 device_class=SensorDeviceClass.HUMIDITY,
113 entity_registry_enabled_default=
False,
114 state_class=SensorStateClass.MEASUREMENT,
117 key=
"precipitation_amount",
118 value_fn=
lambda data: data.precipitation_amount,
119 native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
120 device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
121 state_class=SensorStateClass.MEASUREMENT,
125 translation_key=
"measure_time",
126 value_fn=
lambda data: data.measure_time,
127 entity_registry_enabled_default=
False,
128 device_class=SensorDeviceClass.TIMESTAMP,
132 translation_key=
"dew_point",
133 value_fn=
lambda data: data.dew_point,
134 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
135 device_class=SensorDeviceClass.TEMPERATURE,
136 state_class=SensorStateClass.MEASUREMENT,
139 key=
"visible_distance",
140 translation_key=
"visible_distance",
141 value_fn=
lambda data: data.visible_distance,
142 native_unit_of_measurement=UnitOfLength.METERS,
143 device_class=SensorDeviceClass.DISTANCE,
144 state_class=SensorStateClass.MEASUREMENT,
145 entity_registry_enabled_default=
False,
148 key=
"road_ice_depth",
149 translation_key=
"road_ice_depth",
150 value_fn=
lambda data: data.road_ice_depth,
151 native_unit_of_measurement=UnitOfLength.MILLIMETERS,
152 device_class=SensorDeviceClass.DISTANCE,
153 state_class=SensorStateClass.MEASUREMENT,
154 entity_registry_enabled_default=
False,
157 key=
"road_snow_depth",
158 translation_key=
"road_snow_depth",
159 value_fn=
lambda data: data.road_snow_depth,
160 native_unit_of_measurement=UnitOfLength.MILLIMETERS,
161 device_class=SensorDeviceClass.DISTANCE,
162 state_class=SensorStateClass.MEASUREMENT,
163 entity_registry_enabled_default=
False,
166 key=
"road_water_depth",
167 translation_key=
"road_water_depth",
168 value_fn=
lambda data: data.road_water_depth,
169 native_unit_of_measurement=UnitOfLength.MILLIMETERS,
170 device_class=SensorDeviceClass.DISTANCE,
171 state_class=SensorStateClass.MEASUREMENT,
172 entity_registry_enabled_default=
False,
175 key=
"road_water_equivalent_depth",
176 translation_key=
"road_water_equivalent_depth",
177 value_fn=
lambda data: data.road_water_equivalent_depth,
178 native_unit_of_measurement=UnitOfLength.MILLIMETERS,
179 device_class=SensorDeviceClass.DISTANCE,
180 state_class=SensorStateClass.MEASUREMENT,
181 entity_registry_enabled_default=
False,
185 translation_key=
"wind_height",
186 value_fn=
lambda data: data.wind_height,
187 native_unit_of_measurement=UnitOfLength.METERS,
188 device_class=SensorDeviceClass.DISTANCE,
189 state_class=SensorStateClass.MEASUREMENT,
190 entity_registry_enabled_default=
False,
194 translation_key=
"modified_time",
196 entity_registry_enabled_default=
False,
197 device_class=SensorDeviceClass.TIMESTAMP,
204 entry: TVWeatherConfigEntry,
205 async_add_entities: AddEntitiesCallback,
207 """Set up the Trafikverket sensor entry."""
209 coordinator = entry.runtime_data
213 coordinator, entry.entry_id, entry.data[CONF_STATION], description
215 for description
in SENSOR_TYPES
220 CoordinatorEntity[TVDataUpdateCoordinator], SensorEntity
222 """Representation of a Trafikverket sensor."""
224 entity_description: TrafikverketSensorEntityDescription
225 _attr_attribution = ATTRIBUTION
226 _attr_has_entity_name =
True
230 coordinator: TVDataUpdateCoordinator,
233 description: TrafikverketSensorEntityDescription,
235 """Initialize the sensor."""
240 entry_type=DeviceEntryType.SERVICE,
241 identifiers={(DOMAIN, entry_id)},
242 manufacturer=
"Trafikverket",
245 configuration_url=
"https://api.trafikinfo.trafikverket.se/",
250 """Return state of sensor."""
StateType|datetime native_value(self)
None __init__(self, TVDataUpdateCoordinator coordinator, str entry_id, str sensor_station, TrafikverketSensorEntityDescription description)
datetime|None add_utc_timezone(datetime|None date_time)
None async_setup_entry(HomeAssistant hass, TVWeatherConfigEntry entry, AddEntitiesCallback async_add_entities)