mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Use runtime_data in here_travel_time (#144340)
This commit is contained in:
parent
121e9e4e7f
commit
a673bd7a91
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_MODE, Platform
|
from homeassistant.const import CONF_API_KEY, CONF_MODE, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.start import async_at_started
|
from homeassistant.helpers.start import async_at_started
|
||||||
@ -18,10 +17,10 @@ from .const import (
|
|||||||
CONF_ORIGIN_LATITUDE,
|
CONF_ORIGIN_LATITUDE,
|
||||||
CONF_ORIGIN_LONGITUDE,
|
CONF_ORIGIN_LONGITUDE,
|
||||||
CONF_ROUTE_MODE,
|
CONF_ROUTE_MODE,
|
||||||
DOMAIN,
|
|
||||||
TRAVEL_MODE_PUBLIC,
|
TRAVEL_MODE_PUBLIC,
|
||||||
)
|
)
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
|
HereConfigEntry,
|
||||||
HERERoutingDataUpdateCoordinator,
|
HERERoutingDataUpdateCoordinator,
|
||||||
HERETransitDataUpdateCoordinator,
|
HERETransitDataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
@ -30,7 +29,7 @@ from .model import HERETravelTimeConfig
|
|||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, config_entry: HereConfigEntry) -> bool:
|
||||||
"""Set up HERE Travel Time from a config entry."""
|
"""Set up HERE Travel Time from a config entry."""
|
||||||
api_key = config_entry.data[CONF_API_KEY]
|
api_key = config_entry.data[CONF_API_KEY]
|
||||||
|
|
||||||
@ -57,7 +56,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
cls = HERERoutingDataUpdateCoordinator
|
cls = HERERoutingDataUpdateCoordinator
|
||||||
|
|
||||||
data_coordinator = cls(hass, config_entry, api_key, here_travel_time_config)
|
data_coordinator = cls(hass, config_entry, api_key, here_travel_time_config)
|
||||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data_coordinator
|
config_entry.runtime_data = data_coordinator
|
||||||
|
|
||||||
async def _async_update_at_start(_: HomeAssistant) -> None:
|
async def _async_update_at_start(_: HomeAssistant) -> None:
|
||||||
await data_coordinator.async_refresh()
|
await data_coordinator.async_refresh()
|
||||||
@ -68,12 +67,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_unload_entry(
|
||||||
|
hass: HomeAssistant, config_entry: HereConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||||
config_entry, PLATFORMS
|
|
||||||
)
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -41,16 +41,20 @@ BACKOFF_MULTIPLIER = 1.1
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type HereConfigEntry = ConfigEntry[
|
||||||
|
HERETransitDataUpdateCoordinator | HERERoutingDataUpdateCoordinator
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator[HERETravelTimeData]):
|
class HERERoutingDataUpdateCoordinator(DataUpdateCoordinator[HERETravelTimeData]):
|
||||||
"""here_routing DataUpdateCoordinator."""
|
"""here_routing DataUpdateCoordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: HereConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: HereConfigEntry,
|
||||||
api_key: str,
|
api_key: str,
|
||||||
config: HERETravelTimeConfig,
|
config: HERETravelTimeConfig,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -173,12 +177,12 @@ class HERETransitDataUpdateCoordinator(
|
|||||||
):
|
):
|
||||||
"""HERETravelTime DataUpdateCoordinator."""
|
"""HERETravelTime DataUpdateCoordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: HereConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: HereConfigEntry,
|
||||||
api_key: str,
|
api_key: str,
|
||||||
config: HERETravelTimeConfig,
|
config: HERETravelTimeConfig,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -12,7 +12,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
ATTR_LATITUDE,
|
ATTR_LATITUDE,
|
||||||
@ -40,6 +39,7 @@ from .const import (
|
|||||||
ICONS,
|
ICONS,
|
||||||
)
|
)
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
|
HereConfigEntry,
|
||||||
HERERoutingDataUpdateCoordinator,
|
HERERoutingDataUpdateCoordinator,
|
||||||
HERETransitDataUpdateCoordinator,
|
HERETransitDataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
@ -77,14 +77,14 @@ def sensor_descriptions(travel_mode: str) -> tuple[SensorEntityDescription, ...]
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: HereConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add HERE travel time entities from a config_entry."""
|
"""Add HERE travel time entities from a config_entry."""
|
||||||
|
|
||||||
entry_id = config_entry.entry_id
|
entry_id = config_entry.entry_id
|
||||||
name = config_entry.data[CONF_NAME]
|
name = config_entry.data[CONF_NAME]
|
||||||
coordinator = hass.data[DOMAIN][entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
sensors: list[HERETravelTimeSensor] = [
|
sensors: list[HERETravelTimeSensor] = [
|
||||||
HERETravelTimeSensor(
|
HERETravelTimeSensor(
|
||||||
@ -164,7 +164,8 @@ class OriginSensor(HERETravelTimeSensor):
|
|||||||
self,
|
self,
|
||||||
unique_id_prefix: str,
|
unique_id_prefix: str,
|
||||||
name: str,
|
name: str,
|
||||||
coordinator: HERERoutingDataUpdateCoordinator,
|
coordinator: HERERoutingDataUpdateCoordinator
|
||||||
|
| HERETransitDataUpdateCoordinator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
sensor_description = SensorEntityDescription(
|
sensor_description = SensorEntityDescription(
|
||||||
@ -192,7 +193,8 @@ class DestinationSensor(HERETravelTimeSensor):
|
|||||||
self,
|
self,
|
||||||
unique_id_prefix: str,
|
unique_id_prefix: str,
|
||||||
name: str,
|
name: str,
|
||||||
coordinator: HERERoutingDataUpdateCoordinator,
|
coordinator: HERERoutingDataUpdateCoordinator
|
||||||
|
| HERETransitDataUpdateCoordinator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
sensor_description = SensorEntityDescription(
|
sensor_description = SensorEntityDescription(
|
||||||
|
@ -50,4 +50,3 @@ async def test_unload_entry(hass: HomeAssistant, options) -> None:
|
|||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
assert not hass.data[DOMAIN]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user