mirror of
https://github.com/home-assistant/core.git
synced 2025-07-04 11:57:05 +00:00
Simplify swiss public transport service actions (#146611)
This commit is contained in:
parent
171f7c5f81
commit
b0cf974b34
@ -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
|
||||
|
||||
|
||||
|
@ -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,
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user