From 08160a41a680a359f37d83cfe91b1d4018f1ebcd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 11 Feb 2026 15:59:50 +0100 Subject: [PATCH] Use service helper to extract swiss public transport config entry (#162810) --- .../swiss_public_transport/services.py | 27 ++++--------------- .../swiss_public_transport/strings.json | 6 ----- .../swiss_public_transport/test_services.py | 4 +-- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/swiss_public_transport/services.py b/homeassistant/components/swiss_public_transport/services.py index 9297bd4b409..575d17047f4 100644 --- a/homeassistant/components/swiss_public_transport/services.py +++ b/homeassistant/components/swiss_public_transport/services.py @@ -2,7 +2,6 @@ import voluptuous as vol -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_CONFIG_ENTRY_ID from homeassistant.core import ( HomeAssistant, @@ -11,7 +10,8 @@ from homeassistant.core import ( SupportsResponse, callback, ) -from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import service from homeassistant.helpers.selector import ( NumberSelector, NumberSelectorConfig, @@ -40,30 +40,13 @@ SERVICE_FETCH_CONNECTIONS_SCHEMA = vol.Schema( ) -def _async_get_entry( - hass: HomeAssistant, config_entry_id: str -) -> SwissPublicTransportConfigEntry: - """Get the Swiss public transport config entry.""" - if not (entry := hass.config_entries.async_get_entry(config_entry_id)): - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="config_entry_not_found", - translation_placeholders={"target": config_entry_id}, - ) - if entry.state is not ConfigEntryState.LOADED: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="not_loaded", - translation_placeholders={"target": entry.title}, - ) - return entry - - 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]) + config_entry: SwissPublicTransportConfigEntry = service.async_get_config_entry( + call.hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID] + ) limit = call.data.get(ATTR_LIMIT) or CONNECTIONS_COUNT try: diff --git a/homeassistant/components/swiss_public_transport/strings.json b/homeassistant/components/swiss_public_transport/strings.json index cd5bc1a5916..f5495354b7f 100644 --- a/homeassistant/components/swiss_public_transport/strings.json +++ b/homeassistant/components/swiss_public_transport/strings.json @@ -85,15 +85,9 @@ "cannot_connect": { "message": "Cannot connect to server.\n{error}" }, - "config_entry_not_found": { - "message": "Swiss public transport integration instance \"{target}\" not found." - }, "invalid_data": { "message": "Setup failed for entry {config_title} with invalid data, check at the [stationboard]({stationboard_url}) if your station names are valid.\n{error}" }, - "not_loaded": { - "message": "{target} is not loaded." - }, "request_timeout": { "message": "Timeout while connecting for entry {config_title}.\n{error}" } diff --git a/tests/components/swiss_public_transport/test_services.py b/tests/components/swiss_public_transport/test_services.py index b65ffc12de1..779ca183938 100644 --- a/tests/components/swiss_public_transport/test_services.py +++ b/tests/components/swiss_public_transport/test_services.py @@ -201,7 +201,7 @@ async def test_service_call_load_unload( await hass.async_block_till_done() with pytest.raises( - ServiceValidationError, match=f"{config_entry.title} is not loaded" + ServiceValidationError, match="service_config_entry_not_loaded" ): await hass.services.async_call( domain=DOMAIN, @@ -215,7 +215,7 @@ async def test_service_call_load_unload( with pytest.raises( ServiceValidationError, - match=f'Swiss public transport integration instance "{bad_entry_id}" not found', + match="service_config_entry_not_found", ): await hass.services.async_call( domain=DOMAIN,