Typehints and cleanup for metoffice (#74338)
* Typehints and cleanup for metoffice * add myself as owner
This commit is contained in:
parent
b082764e30
commit
b3fec4c401
10 changed files with 115 additions and 63 deletions
|
@ -1,18 +1,27 @@
|
|||
"""Support for UK Met Office weather service."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from datapoint.Timestep import Timestep
|
||||
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_FORECAST_CONDITION,
|
||||
ATTR_FORECAST_NATIVE_TEMP,
|
||||
ATTR_FORECAST_NATIVE_WIND_SPEED,
|
||||
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
|
||||
ATTR_FORECAST_TIME,
|
||||
ATTR_FORECAST_WIND_BEARING,
|
||||
Forecast,
|
||||
WeatherEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import PRESSURE_HPA, SPEED_MILES_PER_HOUR, TEMP_CELSIUS
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from . import get_device_info
|
||||
from .const import (
|
||||
|
@ -28,6 +37,7 @@ from .const import (
|
|||
MODE_DAILY,
|
||||
MODE_DAILY_LABEL,
|
||||
)
|
||||
from .data import MetOfficeData
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
@ -45,9 +55,8 @@ async def async_setup_entry(
|
|||
)
|
||||
|
||||
|
||||
def _build_forecast_data(timestep):
|
||||
data = {}
|
||||
data[ATTR_FORECAST_TIME] = timestep.date.isoformat()
|
||||
def _build_forecast_data(timestep: Timestep) -> Forecast:
|
||||
data = Forecast(datetime=timestep.date.isoformat())
|
||||
if timestep.weather:
|
||||
data[ATTR_FORECAST_CONDITION] = _get_weather_condition(timestep.weather.value)
|
||||
if timestep.precipitation:
|
||||
|
@ -61,21 +70,30 @@ def _build_forecast_data(timestep):
|
|||
return data
|
||||
|
||||
|
||||
def _get_weather_condition(metoffice_code):
|
||||
def _get_weather_condition(metoffice_code: str) -> str | None:
|
||||
for hass_name, metoffice_codes in CONDITION_CLASSES.items():
|
||||
if metoffice_code in metoffice_codes:
|
||||
return hass_name
|
||||
return None
|
||||
|
||||
|
||||
class MetOfficeWeather(CoordinatorEntity, WeatherEntity):
|
||||
class MetOfficeWeather(
|
||||
CoordinatorEntity[DataUpdateCoordinator[MetOfficeData]], WeatherEntity
|
||||
):
|
||||
"""Implementation of a Met Office weather condition."""
|
||||
|
||||
_attr_attribution = ATTRIBUTION
|
||||
|
||||
_attr_native_temperature_unit = TEMP_CELSIUS
|
||||
_attr_native_pressure_unit = PRESSURE_HPA
|
||||
_attr_native_wind_speed_unit = SPEED_MILES_PER_HOUR
|
||||
|
||||
def __init__(self, coordinator, hass_data, use_3hourly):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator[MetOfficeData],
|
||||
hass_data: dict[str, Any],
|
||||
use_3hourly: bool,
|
||||
) -> None:
|
||||
"""Initialise the platform with a data instance."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
|
@ -89,62 +107,61 @@ class MetOfficeWeather(CoordinatorEntity, WeatherEntity):
|
|||
self._attr_unique_id = f"{self._attr_unique_id}_{MODE_DAILY}"
|
||||
|
||||
@property
|
||||
def condition(self):
|
||||
def condition(self) -> str | None:
|
||||
"""Return the current condition."""
|
||||
if self.coordinator.data.now:
|
||||
return _get_weather_condition(self.coordinator.data.now.weather.value)
|
||||
return None
|
||||
|
||||
@property
|
||||
def native_temperature(self):
|
||||
def native_temperature(self) -> float | None:
|
||||
"""Return the platform temperature."""
|
||||
if self.coordinator.data.now.temperature:
|
||||
return self.coordinator.data.now.temperature.value
|
||||
weather_now = self.coordinator.data.now
|
||||
if weather_now.temperature:
|
||||
value = weather_now.temperature.value
|
||||
return float(value) if value is not None else None
|
||||
return None
|
||||
|
||||
@property
|
||||
def native_pressure(self):
|
||||
def native_pressure(self) -> float | None:
|
||||
"""Return the mean sea-level pressure."""
|
||||
weather_now = self.coordinator.data.now
|
||||
if weather_now and weather_now.pressure:
|
||||
return weather_now.pressure.value
|
||||
value = weather_now.pressure.value
|
||||
return float(value) if value is not None else None
|
||||
return None
|
||||
|
||||
@property
|
||||
def humidity(self):
|
||||
def humidity(self) -> float | None:
|
||||
"""Return the relative humidity."""
|
||||
weather_now = self.coordinator.data.now
|
||||
if weather_now and weather_now.humidity:
|
||||
return weather_now.humidity.value
|
||||
value = weather_now.humidity.value
|
||||
return float(value) if value is not None else None
|
||||
return None
|
||||
|
||||
@property
|
||||
def native_wind_speed(self):
|
||||
def native_wind_speed(self) -> float | None:
|
||||
"""Return the wind speed."""
|
||||
weather_now = self.coordinator.data.now
|
||||
if weather_now and weather_now.wind_speed:
|
||||
return weather_now.wind_speed.value
|
||||
value = weather_now.wind_speed.value
|
||||
return float(value) if value is not None else None
|
||||
return None
|
||||
|
||||
@property
|
||||
def wind_bearing(self):
|
||||
def wind_bearing(self) -> str | None:
|
||||
"""Return the wind bearing."""
|
||||
weather_now = self.coordinator.data.now
|
||||
if weather_now and weather_now.wind_direction:
|
||||
return weather_now.wind_direction.value
|
||||
value = weather_now.wind_direction.value
|
||||
return str(value) if value is not None else None
|
||||
return None
|
||||
|
||||
@property
|
||||
def forecast(self):
|
||||
def forecast(self) -> list[Forecast] | None:
|
||||
"""Return the forecast array."""
|
||||
if self.coordinator.data.forecast is None:
|
||||
return None
|
||||
return [
|
||||
_build_forecast_data(timestep)
|
||||
for timestep in self.coordinator.data.forecast
|
||||
]
|
||||
|
||||
@property
|
||||
def attribution(self):
|
||||
"""Return the attribution."""
|
||||
return ATTRIBUTION
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue