mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Improve here_travel_time
generic typing (#84631)
This commit is contained in:
parent
2049993941
commit
b90a51490f
@ -1,4 +1,6 @@
|
|||||||
"""Constants for the HERE Travel Time integration."""
|
"""Constants for the HERE Travel Time integration."""
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
DOMAIN = "here_travel_time"
|
DOMAIN = "here_travel_time"
|
||||||
DEFAULT_SCAN_INTERVAL = 300
|
DEFAULT_SCAN_INTERVAL = 300
|
||||||
|
|
||||||
@ -51,11 +53,11 @@ ICONS = {
|
|||||||
TRAVEL_MODE_TRUCK: ICON_TRUCK,
|
TRAVEL_MODE_TRUCK: ICON_TRUCK,
|
||||||
}
|
}
|
||||||
|
|
||||||
ATTR_DURATION = "duration"
|
ATTR_DURATION: Final = "duration"
|
||||||
ATTR_DISTANCE = "distance"
|
ATTR_DISTANCE: Final = "distance"
|
||||||
ATTR_ORIGIN = "origin"
|
ATTR_ORIGIN: Final = "origin"
|
||||||
ATTR_DESTINATION = "destination"
|
ATTR_DESTINATION: Final = "destination"
|
||||||
|
|
||||||
ATTR_DURATION_IN_TRAFFIC = "duration_in_traffic"
|
ATTR_DURATION_IN_TRAFFIC: Final = "duration_in_traffic"
|
||||||
ATTR_ORIGIN_NAME = "origin_name"
|
ATTR_ORIGIN_NAME: Final = "origin_name"
|
||||||
ATTR_DESTINATION_NAME = "destination_name"
|
ATTR_DESTINATION_NAME: Final = "destination_name"
|
||||||
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import datetime, time, timedelta
|
from datetime import datetime, time, timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any, Optional
|
||||||
|
|
||||||
import here_routing
|
import here_routing
|
||||||
from here_routing import (
|
from here_routing import (
|
||||||
@ -40,7 +40,7 @@ BACKOFF_MULTIPLIER = 1.1
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator):
|
class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator[HERETravelTimeData]):
|
||||||
"""here_routing DataUpdateCoordinator."""
|
"""here_routing DataUpdateCoordinator."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -59,7 +59,7 @@ class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
self._api = HERERoutingApi(api_key)
|
self._api = HERERoutingApi(api_key)
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
async def _async_update_data(self) -> HERETravelTimeData | None:
|
async def _async_update_data(self) -> HERETravelTimeData:
|
||||||
"""Get the latest data from the HERE Routing API."""
|
"""Get the latest data from the HERE Routing API."""
|
||||||
origin, destination, arrival, departure = prepare_parameters(
|
origin, destination, arrival, departure = prepare_parameters(
|
||||||
self.hass, self.config
|
self.hass, self.config
|
||||||
@ -144,7 +144,9 @@ class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class HERETransitDataUpdateCoordinator(DataUpdateCoordinator):
|
class HERETransitDataUpdateCoordinator(
|
||||||
|
DataUpdateCoordinator[Optional[HERETravelTimeData]]
|
||||||
|
):
|
||||||
"""HERETravelTime DataUpdateCoordinator."""
|
"""HERETravelTime DataUpdateCoordinator."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any, Union
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
RestoreSensor,
|
RestoreSensor,
|
||||||
@ -102,7 +102,12 @@ async def async_setup_entry(
|
|||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
class HERETravelTimeSensor(CoordinatorEntity, RestoreSensor):
|
class HERETravelTimeSensor(
|
||||||
|
CoordinatorEntity[
|
||||||
|
Union[HERERoutingDataUpdateCoordinator, HERETransitDataUpdateCoordinator]
|
||||||
|
],
|
||||||
|
RestoreSensor,
|
||||||
|
):
|
||||||
"""Representation of a HERE travel time sensor."""
|
"""Representation of a HERE travel time sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -144,7 +149,7 @@ class HERETravelTimeSensor(CoordinatorEntity, RestoreSensor):
|
|||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
if self.coordinator.data is not None:
|
if self.coordinator.data is not None:
|
||||||
self._attr_native_value = self.coordinator.data.get(
|
self._attr_native_value = self.coordinator.data.get( # type: ignore[assignment]
|
||||||
self.entity_description.key
|
self.entity_description.key
|
||||||
)
|
)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user