1 """The swiss_public_transport component."""
5 from opendata_transport
import OpendataTransport
6 from opendata_transport.exceptions
import (
7 OpendataTransportConnectionError,
8 OpendataTransportError,
15 config_validation
as cv,
16 device_registry
as dr,
17 entity_registry
as er,
33 from .coordinator
import (
34 SwissPublicTransportConfigEntry,
35 SwissPublicTransportDataUpdateCoordinator,
37 from .helper
import offset_opendata, unique_id_from_config
38 from .services
import setup_services
40 _LOGGER = logging.getLogger(__name__)
43 PLATFORMS: list[Platform] = [Platform.SENSOR]
45 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
48 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
49 """Set up the Swiss public transport component."""
55 hass: HomeAssistant, entry: SwissPublicTransportConfigEntry
57 """Set up Swiss public transport from a config entry."""
60 start = config[CONF_START]
61 destination = config[CONF_DESTINATION]
63 time_offset: dict[str, int] |
None = config.get(CONF_TIME_OFFSET)
66 opendata = OpendataTransport(
70 via=config.get(CONF_VIA),
71 time=config.get(CONF_TIME_FIXED),
72 isArrivalTime=config.get(CONF_TIME_STATION, DEFAULT_TIME_STATION) ==
"arrival",
78 await opendata.async_get_data()
79 except OpendataTransportConnectionError
as e:
81 translation_domain=DOMAIN,
82 translation_key=
"request_timeout",
83 translation_placeholders={
84 "config_title": entry.title,
88 except OpendataTransportError
as e:
90 translation_domain=DOMAIN,
91 translation_key=
"invalid_data",
92 translation_placeholders={
94 "config_title": entry.title,
100 await coordinator.async_config_entry_first_refresh()
101 entry.runtime_data = coordinator
103 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
108 hass: HomeAssistant, entry: SwissPublicTransportConfigEntry
110 """Unload a config entry."""
111 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
115 hass: HomeAssistant, config_entry: SwissPublicTransportConfigEntry
117 """Migrate config entry."""
118 _LOGGER.debug(
"Migrating from version %s", config_entry.version)
120 if config_entry.version > 3:
124 if config_entry.version == 1
and config_entry.minor_version == 1:
127 entity_registry = er.async_get(hass)
128 device_registry = dr.async_get(hass)
129 device_entries = dr.async_entries_for_config_entry(
130 device_registry, config_entry_id=config_entry.entry_id
132 for dev
in device_entries:
133 device_registry.async_update_device(
134 dev.id, remove_config_entry_id=config_entry.entry_id
137 entity_id = entity_registry.async_get_entity_id(
138 Platform.SENSOR, DOMAIN,
"None_departure"
141 entity_registry.async_update_entity(
143 new_unique_id=f
"{new_unique_id}_departure",
146 "Faulty entity with unique_id 'None_departure' migrated to new unique_id '%s'",
147 f
"{new_unique_id}_departure",
151 hass.config_entries.async_update_entry(
152 config_entry, unique_id=new_unique_id, minor_version=2
155 if config_entry.version < 3:
157 hass.config_entries.async_update_entry(config_entry, version=3, minor_version=1)
160 "Migration to version %s.%s successful",
161 config_entry.version,
162 config_entry.minor_version,
None setup_services(HomeAssistant hass)
None offset_opendata(OpendataTransport opendata, dict[str, int] offset)
str unique_id_from_config(MappingProxyType[str, Any]|dict[str, Any] config)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_migrate_entry(HomeAssistant hass, SwissPublicTransportConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, SwissPublicTransportConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, SwissPublicTransportConfigEntry entry)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)