8
8
ServiceCall ,
9
9
ServiceResponse ,
10
10
SupportsResponse ,
11
+ callback ,
11
12
)
12
13
from homeassistant .exceptions import HomeAssistantError , ServiceValidationError
13
14
from homeassistant .helpers .selector import (
39
40
)
40
41
41
42
42
- def async_get_entry (
43
+ def _async_get_entry (
43
44
hass : HomeAssistant , config_entry_id : str
44
45
) -> SwissPublicTransportConfigEntry :
45
46
"""Get the Swiss public transport config entry."""
@@ -58,34 +59,36 @@ def async_get_entry(
58
59
return entry
59
60
60
61
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 }
63
82
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 ])
69
83
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."""
84
87
85
88
hass .services .async_register (
86
89
DOMAIN ,
87
90
SERVICE_FETCH_CONNECTIONS ,
88
- async_fetch_connections ,
91
+ _async_fetch_connections ,
89
92
schema = SERVICE_FETCH_CONNECTIONS_SCHEMA ,
90
93
supports_response = SupportsResponse .ONLY ,
91
94
)
0 commit comments