mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Improve type hints in environment_canada sensors (#136813)
* Use TypeVar * Use bound for TypeVar * Remove PEP 695 syntax * Add type alias to use new TypeVar syntax --------- Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
parent
5e6f4a374e
commit
bfa7eaa221
@ -19,6 +19,7 @@ from .const import DOMAIN
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type ECConfigEntry = ConfigEntry[ECRuntimeData]
|
||||
type ECDataType = ECAirQuality | ECRadar | ECWeather
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -30,16 +31,16 @@ class ECRuntimeData:
|
||||
weather_coordinator: ECDataUpdateCoordinator[ECWeather]
|
||||
|
||||
|
||||
class ECDataUpdateCoordinator[_ECDataTypeT: ECAirQuality | ECRadar | ECWeather](
|
||||
DataUpdateCoordinator[_ECDataTypeT]
|
||||
):
|
||||
class ECDataUpdateCoordinator[DataT: ECDataType](DataUpdateCoordinator[DataT]):
|
||||
"""Class to manage fetching EC data."""
|
||||
|
||||
config_entry: ECConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: ECConfigEntry,
|
||||
ec_data: _ECDataTypeT,
|
||||
ec_data: DataT,
|
||||
name: str,
|
||||
update_interval: timedelta,
|
||||
) -> None:
|
||||
@ -60,7 +61,7 @@ class ECDataUpdateCoordinator[_ECDataTypeT: ECAirQuality | ECRadar | ECWeather](
|
||||
configuration_url="https://weather.gc.ca/",
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> _ECDataTypeT:
|
||||
async def _async_update_data(self) -> DataT:
|
||||
"""Fetch data from EC."""
|
||||
try:
|
||||
await self.ec_data.update()
|
||||
|
@ -6,6 +6,8 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from env_canada import ECWeather
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
@ -27,7 +29,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import ATTR_STATION
|
||||
from .coordinator import ECConfigEntry
|
||||
from .coordinator import ECConfigEntry, ECDataType, ECDataUpdateCoordinator
|
||||
|
||||
ATTR_TIME = "alert time"
|
||||
|
||||
@ -268,13 +270,19 @@ async def async_setup_entry(
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class ECBaseSensorEntity(CoordinatorEntity, SensorEntity):
|
||||
class ECBaseSensorEntity[DataT: ECDataType](
|
||||
CoordinatorEntity[ECDataUpdateCoordinator[DataT]], SensorEntity
|
||||
):
|
||||
"""Environment Canada sensor base."""
|
||||
|
||||
entity_description: ECSensorEntityDescription
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(self, coordinator, description):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ECDataUpdateCoordinator[DataT],
|
||||
description: ECSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the base sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
@ -292,10 +300,14 @@ class ECBaseSensorEntity(CoordinatorEntity, SensorEntity):
|
||||
return value
|
||||
|
||||
|
||||
class ECSensorEntity(ECBaseSensorEntity):
|
||||
class ECSensorEntity[DataT: ECDataType](ECBaseSensorEntity[DataT]):
|
||||
"""Environment Canada sensor for conditions."""
|
||||
|
||||
def __init__(self, coordinator, description):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ECDataUpdateCoordinator[DataT],
|
||||
description: ECSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator, description)
|
||||
self._attr_extra_state_attributes = {
|
||||
@ -304,7 +316,7 @@ class ECSensorEntity(ECBaseSensorEntity):
|
||||
}
|
||||
|
||||
|
||||
class ECAlertSensorEntity(ECBaseSensorEntity):
|
||||
class ECAlertSensorEntity(ECBaseSensorEntity[ECWeather]):
|
||||
"""Environment Canada sensor for alerts."""
|
||||
|
||||
@property
|
||||
|
Loading…
x
Reference in New Issue
Block a user