Skip to content

Commit b0cf974

Browse files
authored
Simplify swiss public transport service actions (home-assistant#146611)
1 parent 171f7c5 commit b0cf974

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

homeassistant/components/swiss_public_transport/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
SwissPublicTransportDataUpdateCoordinator,
3636
)
3737
from .helper import offset_opendata, unique_id_from_config
38-
from .services import setup_services
38+
from .services import async_setup_services
3939

4040
_LOGGER = logging.getLogger(__name__)
4141

@@ -47,7 +47,7 @@
4747

4848
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4949
"""Set up the Swiss public transport component."""
50-
setup_services(hass)
50+
async_setup_services(hass)
5151
return True
5252

5353

homeassistant/components/swiss_public_transport/services.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ServiceCall,
99
ServiceResponse,
1010
SupportsResponse,
11+
callback,
1112
)
1213
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
1314
from homeassistant.helpers.selector import (
@@ -39,7 +40,7 @@
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

Comments
 (0)