Modernize met.no weather (#97952)

This commit is contained in:
Erik Montnemery 2023-08-07 14:06:16 +02:00 committed by GitHub
parent 683c2f8d22
commit 3a0822e03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -33,7 +33,7 @@ from .const import (
@callback @callback
def configured_instances(hass: HomeAssistant) -> set[str]: def configured_instances(hass: HomeAssistant) -> set[str]:
"""Return a set of configured SimpliSafe instances.""" """Return a set of configured met.no instances."""
entries = [] entries = []
for entry in hass.config_entries.async_entries(DOMAIN): for entry in hass.config_entries.async_entries(DOMAIN):
if entry.data.get("track_home"): if entry.data.get("track_home"):

View File

@ -16,6 +16,7 @@ from homeassistant.components.weather import (
ATTR_WEATHER_WIND_SPEED, ATTR_WEATHER_WIND_SPEED,
Forecast, Forecast,
WeatherEntity, WeatherEntity,
WeatherEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -27,7 +28,7 @@ from homeassistant.const import (
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -82,6 +83,9 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity):
_attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
_attr_native_pressure_unit = UnitOfPressure.HPA _attr_native_pressure_unit = UnitOfPressure.HPA
_attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
_attr_supported_features = (
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
)
def __init__( def __init__(
self, 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 if the entity should be enabled when first added to the entity registry."""
return not self._hourly 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 @property
def condition(self) -> str | None: def condition(self) -> str | None:
"""Return the current condition.""" """Return the current condition."""
@ -190,10 +203,9 @@ class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity):
ATTR_MAP[ATTR_WEATHER_CLOUD_COVERAGE] ATTR_MAP[ATTR_WEATHER_CLOUD_COVERAGE]
) )
@property def _forecast(self, hourly: bool) -> list[Forecast] | None:
def forecast(self) -> list[Forecast] | None:
"""Return the forecast array.""" """Return the forecast array."""
if self._hourly: if hourly:
met_forecast = self.coordinator.data.hourly_forecast met_forecast = self.coordinator.data.hourly_forecast
else: else:
met_forecast = self.coordinator.data.daily_forecast 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] ha_forecast.append(ha_item) # type: ignore[arg-type]
return ha_forecast 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 @property
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:
"""Device info.""" """Device info."""