1 """Support for UK Met Office weather service."""
3 from __future__
import annotations
5 from typing
import Any, cast
7 from datapoint.Timestep
import Timestep
10 ATTR_FORECAST_CONDITION,
11 ATTR_FORECAST_NATIVE_TEMP,
12 ATTR_FORECAST_NATIVE_WIND_SPEED,
13 ATTR_FORECAST_PRECIPITATION_PROBABILITY,
14 ATTR_FORECAST_WIND_BEARING,
15 DOMAIN
as WEATHER_DOMAIN,
16 CoordinatorWeatherEntity,
27 from .
import get_device_info
32 METOFFICE_COORDINATES,
33 METOFFICE_DAILY_COORDINATOR,
34 METOFFICE_HOURLY_COORDINATOR,
38 from .data
import MetOfficeData
42 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
44 """Set up the Met Office weather sensor platform."""
45 entity_registry = er.async_get(hass)
46 hass_data = hass.data[DOMAIN][entry.entry_id]
49 if entity_id := entity_registry.async_get_entity_id(
54 entity_registry.async_remove(entity_id)
59 hass_data[METOFFICE_DAILY_COORDINATOR],
60 hass_data[METOFFICE_HOURLY_COORDINATOR],
69 data =
Forecast(datetime=timestep.date.isoformat())
71 data[ATTR_FORECAST_CONDITION] = CONDITION_MAP.get(timestep.weather.value)
72 if timestep.precipitation:
73 data[ATTR_FORECAST_PRECIPITATION_PROBABILITY] = timestep.precipitation.value
74 if timestep.temperature:
75 data[ATTR_FORECAST_NATIVE_TEMP] = timestep.temperature.value
76 if timestep.wind_direction:
77 data[ATTR_FORECAST_WIND_BEARING] = timestep.wind_direction.value
78 if timestep.wind_speed:
79 data[ATTR_FORECAST_NATIVE_WIND_SPEED] = timestep.wind_speed.value
84 """Calculate unique ID."""
87 return f
"{coordinates}_{MODE_DAILY}"
91 CoordinatorWeatherEntity[
92 TimestampDataUpdateCoordinator[MetOfficeData],
93 TimestampDataUpdateCoordinator[MetOfficeData],
96 """Implementation of a Met Office weather condition."""
98 _attr_attribution = ATTRIBUTION
99 _attr_has_entity_name =
True
101 _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
102 _attr_native_pressure_unit = UnitOfPressure.HPA
103 _attr_native_wind_speed_unit = UnitOfSpeed.MILES_PER_HOUR
104 _attr_supported_features = (
105 WeatherEntityFeature.FORECAST_HOURLY | WeatherEntityFeature.FORECAST_DAILY
110 coordinator_daily: TimestampDataUpdateCoordinator[MetOfficeData],
111 coordinator_hourly: TimestampDataUpdateCoordinator[MetOfficeData],
112 hass_data: dict[str, Any],
114 """Initialise the platform with a data instance."""
115 observation_coordinator = coordinator_daily
117 observation_coordinator,
118 daily_coordinator=coordinator_daily,
119 hourly_coordinator=coordinator_hourly,
123 coordinates=hass_data[METOFFICE_COORDINATES], name=hass_data[METOFFICE_NAME]
127 hass_data[METOFFICE_COORDINATES],
False
132 """Return the current condition."""
133 if self.coordinator.data.now:
134 return CONDITION_MAP.get(self.coordinator.data.now.weather.value)
139 """Return the platform temperature."""
140 weather_now = self.coordinator.data.now
141 if weather_now.temperature:
142 value = weather_now.temperature.value
143 return float(value)
if value
is not None else None
148 """Return the mean sea-level pressure."""
149 weather_now = self.coordinator.data.now
150 if weather_now
and weather_now.pressure:
151 value = weather_now.pressure.value
152 return float(value)
if value
is not None else None
157 """Return the relative humidity."""
158 weather_now = self.coordinator.data.now
159 if weather_now
and weather_now.humidity:
160 value = weather_now.humidity.value
161 return float(value)
if value
is not None else None
166 """Return the wind speed."""
167 weather_now = self.coordinator.data.now
168 if weather_now
and weather_now.wind_speed:
169 value = weather_now.wind_speed.value
170 return float(value)
if value
is not None else None
175 """Return the wind bearing."""
176 weather_now = self.coordinator.data.now
177 if weather_now
and weather_now.wind_direction:
178 value = weather_now.wind_direction.value
179 return str(value)
if value
is not None else None
184 """Return the twice daily forecast in native units."""
186 TimestampDataUpdateCoordinator[MetOfficeData],
195 """Return the hourly forecast in native units."""
197 TimestampDataUpdateCoordinator[MetOfficeData],
float|None native_temperature(self)
float|None native_wind_speed(self)
str|None wind_bearing(self)
list[Forecast]|None _async_forecast_daily(self)
list[Forecast]|None _async_forecast_hourly(self)
float|None humidity(self)
float|None native_pressure(self)
None __init__(self, TimestampDataUpdateCoordinator[MetOfficeData] coordinator_daily, TimestampDataUpdateCoordinator[MetOfficeData] coordinator_hourly, dict[str, Any] hass_data)
str _calculate_unique_id(str coordinates, bool use_3hourly)
Forecast _build_forecast_data(Timestep timestep)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo get_device_info(str coordinates, str name)