Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The HERE Travel Time integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_API_KEY, CONF_MODE, Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.start import async_at_started
9 from homeassistant.util import dt as dt_util
10 
11 from .const import (
12  CONF_ARRIVAL_TIME,
13  CONF_DEPARTURE_TIME,
14  CONF_DESTINATION_ENTITY_ID,
15  CONF_DESTINATION_LATITUDE,
16  CONF_DESTINATION_LONGITUDE,
17  CONF_ORIGIN_ENTITY_ID,
18  CONF_ORIGIN_LATITUDE,
19  CONF_ORIGIN_LONGITUDE,
20  CONF_ROUTE_MODE,
21  DOMAIN,
22  TRAVEL_MODE_PUBLIC,
23 )
24 from .coordinator import (
25  HERERoutingDataUpdateCoordinator,
26  HERETransitDataUpdateCoordinator,
27 )
28 from .model import HERETravelTimeConfig
29 
30 PLATFORMS = [Platform.SENSOR]
31 
32 
33 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
34  """Set up HERE Travel Time from a config entry."""
35  api_key = config_entry.data[CONF_API_KEY]
36 
37  arrival = dt_util.parse_time(config_entry.options.get(CONF_ARRIVAL_TIME, ""))
38  departure = dt_util.parse_time(config_entry.options.get(CONF_DEPARTURE_TIME, ""))
39 
40  here_travel_time_config = HERETravelTimeConfig(
41  destination_latitude=config_entry.data.get(CONF_DESTINATION_LATITUDE),
42  destination_longitude=config_entry.data.get(CONF_DESTINATION_LONGITUDE),
43  destination_entity_id=config_entry.data.get(CONF_DESTINATION_ENTITY_ID),
44  origin_latitude=config_entry.data.get(CONF_ORIGIN_LATITUDE),
45  origin_longitude=config_entry.data.get(CONF_ORIGIN_LONGITUDE),
46  origin_entity_id=config_entry.data.get(CONF_ORIGIN_ENTITY_ID),
47  travel_mode=config_entry.data[CONF_MODE],
48  route_mode=config_entry.options[CONF_ROUTE_MODE],
49  arrival=arrival,
50  departure=departure,
51  )
52 
53  cls: type[HERETransitDataUpdateCoordinator | HERERoutingDataUpdateCoordinator]
54  if config_entry.data[CONF_MODE] in {TRAVEL_MODE_PUBLIC, "publicTransportTimeTable"}:
55  cls = HERETransitDataUpdateCoordinator
56  else:
57  cls = HERERoutingDataUpdateCoordinator
58 
59  data_coordinator = cls(hass, api_key, here_travel_time_config)
60  hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data_coordinator
61 
62  async def _async_update_at_start(_: HomeAssistant) -> None:
63  await data_coordinator.async_refresh()
64 
65  config_entry.async_on_unload(async_at_started(hass, _async_update_at_start))
66  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
67 
68  return True
69 
70 
71 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
72  """Unload a config entry."""
73  unload_ok = await hass.config_entries.async_unload_platforms(
74  config_entry, PLATFORMS
75  )
76  if unload_ok:
77  hass.data[DOMAIN].pop(config_entry.entry_id)
78 
79  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:71
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:33
CALLBACK_TYPE async_at_started(HomeAssistant hass, Callable[[HomeAssistant], Coroutine[Any, Any, None]|None] at_start_cb)
Definition: start.py:80