diff --git a/homeassistant/components/swiss_public_transport/__init__.py b/homeassistant/components/swiss_public_transport/__init__.py index 0d0c4dc6169..49fe9949772 100644 --- a/homeassistant/components/swiss_public_transport/__init__.py +++ b/homeassistant/components/swiss_public_transport/__init__.py @@ -35,7 +35,7 @@ from .coordinator import ( SwissPublicTransportDataUpdateCoordinator, ) from .helper import offset_opendata, unique_id_from_config -from .services import setup_services +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -47,7 +47,7 @@ CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Swiss public transport component.""" - setup_services(hass) + async_setup_services(hass) return True diff --git a/homeassistant/components/swiss_public_transport/services.py b/homeassistant/components/swiss_public_transport/services.py index 3abf1a14b9f..1ac116b4ca9 100644 --- a/homeassistant/components/swiss_public_transport/services.py +++ b/homeassistant/components/swiss_public_transport/services.py @@ -8,6 +8,7 @@ from homeassistant.core import ( ServiceCall, ServiceResponse, SupportsResponse, + callback, ) from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers.selector import ( @@ -39,7 +40,7 @@ SERVICE_FETCH_CONNECTIONS_SCHEMA = vol.Schema( ) -def async_get_entry( +def _async_get_entry( hass: HomeAssistant, config_entry_id: str ) -> SwissPublicTransportConfigEntry: """Get the Swiss public transport config entry.""" @@ -58,34 +59,36 @@ def async_get_entry( return entry -def setup_services(hass: HomeAssistant) -> None: +async def _async_fetch_connections( + call: ServiceCall, +) -> ServiceResponse: + """Fetch a set of connections.""" + config_entry = _async_get_entry(call.hass, call.data[ATTR_CONFIG_ENTRY_ID]) + + limit = call.data.get(ATTR_LIMIT) or CONNECTIONS_COUNT + try: + connections = await config_entry.runtime_data.fetch_connections_as_json( + limit=int(limit) + ) + except UpdateFailed as e: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="cannot_connect", + translation_placeholders={ + "error": str(e), + }, + ) from e + return {"connections": connections} + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: """Set up the services for the Swiss public transport integration.""" - async def async_fetch_connections( - call: ServiceCall, - ) -> ServiceResponse: - """Fetch a set of connections.""" - config_entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) - - limit = call.data.get(ATTR_LIMIT) or CONNECTIONS_COUNT - try: - connections = await config_entry.runtime_data.fetch_connections_as_json( - limit=int(limit) - ) - except UpdateFailed as e: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="cannot_connect", - translation_placeholders={ - "error": str(e), - }, - ) from e - return {"connections": connections} - hass.services.async_register( DOMAIN, SERVICE_FETCH_CONNECTIONS, - async_fetch_connections, + _async_fetch_connections, schema=SERVICE_FETCH_CONNECTIONS_SCHEMA, supports_response=SupportsResponse.ONLY, )