88 ServiceCall ,
99 ServiceResponse ,
1010 SupportsResponse ,
11+ callback ,
1112)
1213from homeassistant .exceptions import HomeAssistantError , ServiceValidationError
1314from homeassistant .helpers .selector import (
3940)
4041
4142
42- def async_get_entry (
43+ def _async_get_entry (
4344 hass : HomeAssistant , config_entry_id : str
4445) -> SwissPublicTransportConfigEntry :
4546 """Get the Swiss public transport config entry."""
@@ -58,34 +59,36 @@ def async_get_entry(
5859 return entry
5960
6061
61- def setup_services (hass : HomeAssistant ) -> None :
62- """Set up the services for the Swiss public transport integration."""
62+ async def _async_fetch_connections (
63+ call : ServiceCall ,
64+ ) -> ServiceResponse :
65+ """Fetch a set of connections."""
66+ config_entry = _async_get_entry (call .hass , call .data [ATTR_CONFIG_ENTRY_ID ])
67+
68+ limit = call .data .get (ATTR_LIMIT ) or CONNECTIONS_COUNT
69+ try :
70+ connections = await config_entry .runtime_data .fetch_connections_as_json (
71+ limit = int (limit )
72+ )
73+ except UpdateFailed as e :
74+ raise HomeAssistantError (
75+ translation_domain = DOMAIN ,
76+ translation_key = "cannot_connect" ,
77+ translation_placeholders = {
78+ "error" : str (e ),
79+ },
80+ ) from e
81+ return {"connections" : connections }
6382
64- async def async_fetch_connections (
65- call : ServiceCall ,
66- ) -> ServiceResponse :
67- """Fetch a set of connections."""
68- config_entry = async_get_entry (hass , call .data [ATTR_CONFIG_ENTRY_ID ])
6983
70- limit = call .data .get (ATTR_LIMIT ) or CONNECTIONS_COUNT
71- try :
72- connections = await config_entry .runtime_data .fetch_connections_as_json (
73- limit = int (limit )
74- )
75- except UpdateFailed as e :
76- raise HomeAssistantError (
77- translation_domain = DOMAIN ,
78- translation_key = "cannot_connect" ,
79- translation_placeholders = {
80- "error" : str (e ),
81- },
82- ) from e
83- return {"connections" : connections }
84+ @callback
85+ def async_setup_services (hass : HomeAssistant ) -> None :
86+ """Set up the services for the Swiss public transport integration."""
8487
8588 hass .services .async_register (
8689 DOMAIN ,
8790 SERVICE_FETCH_CONNECTIONS ,
88- async_fetch_connections ,
91+ _async_fetch_connections ,
8992 schema = SERVICE_FETCH_CONNECTIONS_SCHEMA ,
9093 supports_response = SupportsResponse .ONLY ,
9194 )
0 commit comments