1 """Define services for the Swiss public transport integration."""
3 import voluptuous
as vol
26 SERVICE_FETCH_CONNECTIONS,
28 from .coordinator
import SwissPublicTransportConfigEntry
30 SERVICE_FETCH_CONNECTIONS_SCHEMA = vol.Schema(
32 vol.Required(ATTR_CONFIG_ENTRY_ID): str,
33 vol.Optional(ATTR_LIMIT, default=CONNECTIONS_COUNT):
NumberSelector(
35 min=1, max=CONNECTIONS_MAX, mode=NumberSelectorMode.BOX
43 hass: HomeAssistant, config_entry_id: str
44 ) -> SwissPublicTransportConfigEntry:
45 """Get the Swiss public transport config entry."""
46 if not (entry := hass.config_entries.async_get_entry(config_entry_id)):
48 translation_domain=DOMAIN,
49 translation_key=
"config_entry_not_found",
50 translation_placeholders={
"target": config_entry_id},
52 if entry.state
is not ConfigEntryState.LOADED:
54 translation_domain=DOMAIN,
55 translation_key=
"not_loaded",
56 translation_placeholders={
"target": entry.title},
62 """Set up the services for the Swiss public transport integration."""
64 async
def async_fetch_connections(
67 """Fetch a set of connections."""
70 limit = call.data.get(ATTR_LIMIT)
or CONNECTIONS_COUNT
72 connections = await config_entry.runtime_data.fetch_connections_as_json(
75 except UpdateFailed
as e:
77 translation_domain=DOMAIN,
78 translation_key=
"cannot_connect",
79 translation_placeholders={
83 return {
"connections": connections}
85 hass.services.async_register(
87 SERVICE_FETCH_CONNECTIONS,
88 async_fetch_connections,
89 schema=SERVICE_FETCH_CONNECTIONS_SCHEMA,
90 supports_response=SupportsResponse.ONLY,
None setup_services(HomeAssistant hass)
SwissPublicTransportConfigEntry async_get_entry(HomeAssistant hass, str config_entry_id)