From 4ec619b7b44ac82f3608aeca83b7659c247dcda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20M=C3=B6ritz?= Date: Sat, 10 Oct 2026 17:02:39 +0200 Subject: [PATCH] Fix selecting HVV stations that share a name (#185625) --- .../components/hvv_departures/config_flow.py | 16 +++- .../hvv_departures/test_config_flow.py | 80 ++++++++++++++++++- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/hvv_departures/config_flow.py b/homeassistant/components/hvv_departures/config_flow.py index 63beebffc2a3..038b3906fb33 100644 --- a/homeassistant/components/hvv_departures/config_flow.py +++ b/homeassistant/components/hvv_departures/config_flow.py @@ -101,9 +101,10 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN): ) self.stations = { - station.name: station + station.id: station for station in (check_name.results or []) if station.type == RegionalSDNameType.STATION + and station.id is not None and station.name is not None } @@ -126,7 +127,18 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN): """Handle the step where the user inputs his/her station.""" schema = probatio.Schema( - {probatio.Required(CONF_STATION): probatio.In(list(self.stations))} + { + probatio.Required(CONF_STATION): probatio.In( + { + station_id: ( + f"{station.name} ({station.city})" + if station.city + else station.name + ) + for station_id, station in self.stations.items() + } + ) + } ) if user_input is None: diff --git a/tests/components/hvv_departures/test_config_flow.py b/tests/components/hvv_departures/test_config_flow.py index 46609dfdd3d6..1e25c753fff8 100644 --- a/tests/components/hvv_departures/test_config_flow.py +++ b/tests/components/hvv_departures/test_config_flow.py @@ -83,7 +83,7 @@ async def test_user_flow(hass: HomeAssistant) -> None: # step: station_select result_station_select = await hass.config_entries.flow.async_configure( result_user["flow_id"], - {CONF_STATION: "Wartenau"}, + {CONF_STATION: "Master:10901"}, ) assert result_station_select["type"] is FlowResultType.CREATE_ENTRY @@ -105,6 +105,78 @@ async def test_user_flow(hass: HomeAssistant) -> None: } +async def test_user_flow_stations_with_same_name(hass: HomeAssistant) -> None: + """Test that stations with the same name in different cities can be selected.""" + + with ( + patch( + "homeassistant.components.hvv_departures.hub.GTI.init", + return_value=FIXTURE_INIT, + ), + patch( + "homeassistant.components.hvv_departures.hub.GTI.checkName", + return_value=CNResponse.model_validate( + { + "returnCode": "OK", + "results": [ + { + "name": "Rathaus", + "city": "Hamburg", + "id": "Master:1", + "type": "STATION", + }, + { + "name": "Rathaus", + "city": "Barsbüttel", + "id": "Master:2", + "type": "STATION", + }, + ], + } + ), + ), + patch( + "homeassistant.components.hvv_departures.async_setup_entry", + return_value=True, + ), + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_HOST: "api-test.geofox.de", + CONF_USERNAME: "test-username", + CONF_PASSWORD: "test-password", + }, + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_STATION: "Rathaus"}, + ) + + assert result["step_id"] == "station_select" + assert result["data_schema"].schema[CONF_STATION].container == { + "Master:1": "Rathaus (Hamburg)", + "Master:2": "Rathaus (Barsbüttel)", + } + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_STATION: "Master:1"}, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Rathaus" + assert result["data"][CONF_STATION] == { + "name": "Rathaus", + "city": "Hamburg", + "id": "Master:1", + "type": "STATION", + } + + async def test_user_flow_no_results(hass: HomeAssistant) -> None: """Test that config flow works when there are no results.""" @@ -164,7 +236,7 @@ async def test_user_flow_no_results(hass: HomeAssistant) -> None: result_station_select = await hass.config_entries.flow.async_configure( result_user["flow_id"], - {CONF_STATION: "Wartenau"}, + {CONF_STATION: "Master:10901"}, ) assert result_station_select["type"] is FlowResultType.CREATE_ENTRY @@ -231,7 +303,7 @@ async def test_user_flow_invalid_auth(hass: HomeAssistant) -> None: result_station_select = await hass.config_entries.flow.async_configure( result_user["flow_id"], - {CONF_STATION: "Wartenau"}, + {CONF_STATION: "Master:10901"}, ) assert result_station_select["type"] is FlowResultType.CREATE_ENTRY @@ -298,7 +370,7 @@ async def test_user_flow_cannot_connect(hass: HomeAssistant) -> None: result_station_select = await hass.config_entries.flow.async_configure( result_user["flow_id"], - {CONF_STATION: "Wartenau"}, + {CONF_STATION: "Master:10901"}, ) assert result_station_select["type"] is FlowResultType.CREATE_ENTRY