Improve meteo_france
generic typing (#84685)
This commit is contained in:
parent
059ecd10a2
commit
8827d9e88f
2 changed files with 27 additions and 12 deletions
|
@ -2,11 +2,15 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, TypeVar, Union
|
||||
|
||||
from meteofrance_api.helpers import (
|
||||
get_warning_text_status_from_indice_color,
|
||||
readeable_phenomenoms_dict,
|
||||
)
|
||||
from meteofrance_api.model.forecast import Forecast
|
||||
from meteofrance_api.model.rain import Rain
|
||||
from meteofrance_api.model.warning import CurrentPhenomenons
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -45,6 +49,8 @@ from .const import (
|
|||
MODEL,
|
||||
)
|
||||
|
||||
_DataT = TypeVar("_DataT", bound=Union[Rain, Forecast, CurrentPhenomenons])
|
||||
|
||||
|
||||
@dataclass
|
||||
class MeteoFranceRequiredKeysMixin:
|
||||
|
@ -180,11 +186,14 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Meteo-France sensor platform."""
|
||||
coordinator_forecast = hass.data[DOMAIN][entry.entry_id][COORDINATOR_FORECAST]
|
||||
coordinator_rain = hass.data[DOMAIN][entry.entry_id][COORDINATOR_RAIN]
|
||||
coordinator_alert = hass.data[DOMAIN][entry.entry_id][COORDINATOR_ALERT]
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator_forecast: DataUpdateCoordinator[Forecast] = data[COORDINATOR_FORECAST]
|
||||
coordinator_rain: DataUpdateCoordinator[Rain] | None = data[COORDINATOR_RAIN]
|
||||
coordinator_alert: DataUpdateCoordinator[CurrentPhenomenons] | None = data[
|
||||
COORDINATOR_ALERT
|
||||
]
|
||||
|
||||
entities = [
|
||||
entities: list[MeteoFranceSensor[Any]] = [
|
||||
MeteoFranceSensor(coordinator_forecast, description)
|
||||
for description in SENSOR_TYPES
|
||||
]
|
||||
|
@ -216,7 +225,7 @@ async def async_setup_entry(
|
|||
async_add_entities(entities, False)
|
||||
|
||||
|
||||
class MeteoFranceSensor(CoordinatorEntity, SensorEntity):
|
||||
class MeteoFranceSensor(CoordinatorEntity[DataUpdateCoordinator[_DataT]], SensorEntity):
|
||||
"""Representation of a Meteo-France sensor."""
|
||||
|
||||
entity_description: MeteoFranceSensorEntityDescription
|
||||
|
@ -224,7 +233,7 @@ class MeteoFranceSensor(CoordinatorEntity, SensorEntity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
coordinator: DataUpdateCoordinator[_DataT],
|
||||
description: MeteoFranceSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the Meteo-France sensor."""
|
||||
|
@ -278,7 +287,7 @@ class MeteoFranceSensor(CoordinatorEntity, SensorEntity):
|
|||
return value
|
||||
|
||||
|
||||
class MeteoFranceRainSensor(MeteoFranceSensor):
|
||||
class MeteoFranceRainSensor(MeteoFranceSensor[Rain]):
|
||||
"""Representation of a Meteo-France rain sensor."""
|
||||
|
||||
@property
|
||||
|
@ -304,12 +313,12 @@ class MeteoFranceRainSensor(MeteoFranceSensor):
|
|||
}
|
||||
|
||||
|
||||
class MeteoFranceAlertSensor(MeteoFranceSensor):
|
||||
class MeteoFranceAlertSensor(MeteoFranceSensor[CurrentPhenomenons]):
|
||||
"""Representation of a Meteo-France alert sensor."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
coordinator: DataUpdateCoordinator[CurrentPhenomenons],
|
||||
description: MeteoFranceSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the Meteo-France sensor."""
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
import logging
|
||||
import time
|
||||
|
||||
from meteofrance_api.model.forecast import Forecast
|
||||
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_FORECAST_CONDITION,
|
||||
ATTR_FORECAST_NATIVE_PRECIPITATION,
|
||||
|
@ -56,7 +58,9 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Meteo-France weather platform."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR_FORECAST]
|
||||
coordinator: DataUpdateCoordinator[Forecast] = hass.data[DOMAIN][entry.entry_id][
|
||||
COORDINATOR_FORECAST
|
||||
]
|
||||
|
||||
async_add_entities(
|
||||
[
|
||||
|
@ -74,7 +78,9 @@ async def async_setup_entry(
|
|||
)
|
||||
|
||||
|
||||
class MeteoFranceWeather(CoordinatorEntity, WeatherEntity):
|
||||
class MeteoFranceWeather(
|
||||
CoordinatorEntity[DataUpdateCoordinator[Forecast]], WeatherEntity
|
||||
):
|
||||
"""Representation of a weather condition."""
|
||||
|
||||
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS
|
||||
|
@ -82,7 +88,7 @@ class MeteoFranceWeather(CoordinatorEntity, WeatherEntity):
|
|||
_attr_native_pressure_unit = UnitOfPressure.HPA
|
||||
_attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
|
||||
|
||||
def __init__(self, coordinator: DataUpdateCoordinator, mode: str) -> None:
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Forecast], mode: str) -> None:
|
||||
"""Initialise the platform with a data instance and station name."""
|
||||
super().__init__(coordinator)
|
||||
self._city_name = self.coordinator.data.position["name"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue