From 3a0822e03b939119c9f10a87cefab58ea42a5c2a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 7 Aug 2023 14:06:16 +0200 Subject: [PATCH] Modernize met.no weather (#97952) --- homeassistant/components/met/config_flow.py | 2 +- homeassistant/components/met/weather.py | 33 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/met/config_flow.py b/homeassistant/components/met/config_flow.py index d8cb31077c2..d36a9e58eb7 100644 --- a/homeassistant/components/met/config_flow.py +++ b/homeassistant/components/met/config_flow.py @@ -33,7 +33,7 @@ from .const import ( @callback def configured_instances(hass: HomeAssistant) -> set[str]: - """Return a set of configured SimpliSafe instances.""" + """Return a set of configured met.no instances.""" entries = [] for entry in hass.config_entries.async_entries(DOMAIN): if entry.data.get("track_home"): diff --git a/homeassistant/components/met/weather.py b/homeassistant/components/met/weather.py index 20822dc9973..d364066ae61 100644 --- a/homeassistant/components/met/weather.py +++ b/homeassistant/components/met/weather.py @@ -16,6 +16,7 @@ from homeassistant.components.weather import ( ATTR_WEATHER_WIND_SPEED, Forecast, WeatherEntity, + WeatherEntityFeature, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -27,7 +28,7 @@ from homeassistant.const import ( UnitOfSpeed, UnitOfTemperature, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -82,6 +83,9 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity): _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS _attr_native_pressure_unit = UnitOfPressure.HPA _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR + _attr_supported_features = ( + WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY + ) def __init__( self, @@ -133,6 +137,15 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity): """Return if the entity should be enabled when first added to the entity registry.""" return not self._hourly + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + super()._handle_coordinator_update() + assert self.platform.config_entry + self.platform.config_entry.async_create_task( + self.hass, self.async_update_listeners(("daily", "hourly")) + ) + @property def condition(self) -> str | None: """Return the current condition.""" @@ -190,10 +203,9 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity): ATTR_MAP[ATTR_WEATHER_CLOUD_COVERAGE] ) - @property - def forecast(self) -> list[Forecast] | None: + def _forecast(self, hourly: bool) -> list[Forecast] | None: """Return the forecast array.""" - if self._hourly: + if hourly: met_forecast = self.coordinator.data.hourly_forecast else: met_forecast = self.coordinator.data.daily_forecast @@ -214,6 +226,19 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity): ha_forecast.append(ha_item) # type: ignore[arg-type] return ha_forecast + @property + def forecast(self) -> list[Forecast] | None: + """Return the forecast array.""" + return self._forecast(self._hourly) + + async def async_forecast_daily(self) -> list[Forecast] | None: + """Return the daily forecast in native units.""" + return self._forecast(False) + + async def async_forecast_hourly(self) -> list[Forecast] | None: + """Return the hourly forecast in native units.""" + return self._forecast(True) + @property def device_info(self) -> DeviceInfo: """Device info."""