Simplify swiss public transport service actions (#146611)

This commit is contained in:
epenet 2025-06-12 16:27:20 +02:00 committed by GitHub
parent 171f7c5f81
commit b0cf974b34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 26 deletions

View File

@ -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

View File

@ -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,14 +59,11 @@ def async_get_entry(
return entry
def setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Swiss public transport integration."""
async def async_fetch_connections(
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])
config_entry = _async_get_entry(call.hass, call.data[ATTR_CONFIG_ENTRY_ID])
limit = call.data.get(ATTR_LIMIT) or CONNECTIONS_COUNT
try:
@ -82,10 +80,15 @@ def setup_services(hass: HomeAssistant) -> None:
) from e
return {"connections": connections}
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Swiss public transport integration."""
hass.services.async_register(
DOMAIN,
SERVICE_FETCH_CONNECTIONS,
async_fetch_connections,
_async_fetch_connections,
schema=SERVICE_FETCH_CONNECTIONS_SCHEMA,
supports_response=SupportsResponse.ONLY,
)