Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Israel Rail component."""
2 
3 import logging
4 
5 from israelrailapi import TrainSchedule
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 
12 from .const import CONF_DESTINATION, CONF_START, DOMAIN
13 from .coordinator import IsraelRailDataUpdateCoordinator
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
18 PLATFORMS: list[Platform] = [Platform.SENSOR]
19 
20 
21 type IsraelRailConfigEntry = ConfigEntry[IsraelRailDataUpdateCoordinator]
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: IsraelRailConfigEntry) -> bool:
25  """Set up Israel rail from a config entry."""
26  config = entry.data
27 
28  start = config[CONF_START]
29  destination = config[CONF_DESTINATION]
30 
31  train_schedule = TrainSchedule()
32 
33  try:
34  await hass.async_add_executor_job(train_schedule.query, start, destination)
35  except Exception as e:
36  raise ConfigEntryNotReady(
37  translation_domain=DOMAIN,
38  translation_key="request_timeout",
39  translation_placeholders={
40  "config_title": entry.title,
41  "error": str(e),
42  },
43  ) from e
44 
45  israel_rail_coordinator = IsraelRailDataUpdateCoordinator(
46  hass, train_schedule, start, destination
47  )
48  await israel_rail_coordinator.async_config_entry_first_refresh()
49  entry.runtime_data = israel_rail_coordinator
50 
51  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
52 
53  return True
54 
55 
56 async def async_unload_entry(hass: HomeAssistant, entry: IsraelRailConfigEntry) -> bool:
57  """Unload a config entry."""
58  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, IsraelRailConfigEntry entry)
Definition: __init__.py:24
bool async_unload_entry(HomeAssistant hass, IsraelRailConfigEntry entry)
Definition: __init__.py:56