From d9deba3916f535af3b109465cb48a166ccf7ebba Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Wed, 29 Jan 2025 14:25:28 +0100 Subject: [PATCH] Take exclude vias in unique ids for nmbs (#136590) --- homeassistant/components/nmbs/config_flow.py | 8 ++++--- homeassistant/components/nmbs/sensor.py | 15 +++++++++---- tests/components/nmbs/test_config_flow.py | 23 +++++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/nmbs/config_flow.py b/homeassistant/components/nmbs/config_flow.py index 24ef8cd4995..e45b2d9adeb 100644 --- a/homeassistant/components/nmbs/config_flow.py +++ b/homeassistant/components/nmbs/config_flow.py @@ -79,8 +79,9 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN): for station in self.stations if station["id"] == user_input[CONF_STATION_TO] ] + vias = "_excl_vias" if user_input.get(CONF_EXCLUDE_VIAS) else "" await self.async_set_unique_id( - f"{user_input[CONF_STATION_FROM]}_{user_input[CONF_STATION_TO]}" + f"{user_input[CONF_STATION_FROM]}_{user_input[CONF_STATION_TO]}{vias}" ) self._abort_if_unique_id_configured() @@ -154,12 +155,13 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN): user_input[CONF_STATION_LIVE] = station_live["id"] entity_registry = er.async_get(self.hass) prefix = "live" + vias = "_excl_vias" if user_input.get(CONF_EXCLUDE_VIAS, False) else "" if entity_id := entity_registry.async_get_entity_id( Platform.SENSOR, DOMAIN, f"{prefix}_{station_live['standardname']}_{station_from['standardname']}_{station_to['standardname']}", ): - new_unique_id = f"{DOMAIN}_{prefix}_{station_live['id']}_{station_from['id']}_{station_to['id']}" + new_unique_id = f"{DOMAIN}_{prefix}_{station_live['id']}_{station_from['id']}_{station_to['id']}{vias}" entity_registry.async_update_entity( entity_id, new_unique_id=new_unique_id ) @@ -168,7 +170,7 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN): DOMAIN, f"{prefix}_{station_live['name']}_{station_from['name']}_{station_to['name']}", ): - new_unique_id = f"{DOMAIN}_{prefix}_{station_live['id']}_{station_from['id']}_{station_to['id']}" + new_unique_id = f"{DOMAIN}_{prefix}_{station_live['id']}_{station_from['id']}_{station_to['id']}{vias}" entity_registry.async_update_entity( entity_id, new_unique_id=new_unique_id ) diff --git a/homeassistant/components/nmbs/sensor.py b/homeassistant/components/nmbs/sensor.py index ca18d3b1bbd..6d13777e10a 100644 --- a/homeassistant/components/nmbs/sensor.py +++ b/homeassistant/components/nmbs/sensor.py @@ -170,8 +170,10 @@ async def async_setup_entry( NMBSSensor( api_client, name, show_on_map, station_from, station_to, excl_vias ), - NMBSLiveBoard(api_client, station_from, station_from, station_to), - NMBSLiveBoard(api_client, station_to, station_from, station_to), + NMBSLiveBoard( + api_client, station_from, station_from, station_to, excl_vias + ), + NMBSLiveBoard(api_client, station_to, station_from, station_to, excl_vias), ] ) @@ -187,12 +189,15 @@ class NMBSLiveBoard(SensorEntity): live_station: dict[str, Any], station_from: dict[str, Any], station_to: dict[str, Any], + excl_vias: bool, ) -> None: """Initialize the sensor for getting liveboard data.""" self._station = live_station self._api_client = api_client self._station_from = station_from self._station_to = station_to + + self._excl_vias = excl_vias self._attrs: dict[str, Any] | None = {} self._state: str | None = None @@ -210,7 +215,8 @@ class NMBSLiveBoard(SensorEntity): unique_id = ( f"{self._station['id']}_{self._station_from['id']}_{self._station_to['id']}" ) - return f"nmbs_live_{unique_id}" + vias = "_excl_vias" if self._excl_vias else "" + return f"nmbs_live_{unique_id}{vias}" @property def icon(self) -> str: @@ -303,7 +309,8 @@ class NMBSSensor(SensorEntity): """Return the unique ID.""" unique_id = f"{self._station_from['id']}_{self._station_to['id']}" - return f"nmbs_connection_{unique_id}" + vias = "_excl_vias" if self._excl_vias else "" + return f"nmbs_connection_{unique_id}{vias}" @property def name(self) -> str: diff --git a/tests/components/nmbs/test_config_flow.py b/tests/components/nmbs/test_config_flow.py index 6e55f89e54a..ff4c5bdf72a 100644 --- a/tests/components/nmbs/test_config_flow.py +++ b/tests/components/nmbs/test_config_flow.py @@ -6,6 +6,7 @@ from unittest.mock import AsyncMock import pytest from homeassistant import config_entries +from homeassistant.components.nmbs.config_flow import CONF_EXCLUDE_VIAS from homeassistant.components.nmbs.const import ( CONF_STATION_FROM, CONF_STATION_LIVE, @@ -120,6 +121,23 @@ async def test_abort_if_exists( assert result["reason"] == "already_configured" +async def test_dont_abort_if_exists_when_vias_differs( + hass: HomeAssistant, mock_nmbs_client: AsyncMock, mock_config_entry: MockConfigEntry +) -> None: + """Test aborting the flow if the entry already exists.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={ + CONF_STATION_FROM: DUMMY_DATA["STAT_BRUSSELS_NORTH"], + CONF_STATION_TO: DUMMY_DATA["STAT_BRUSSELS_SOUTH"], + CONF_EXCLUDE_VIAS: True, + }, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + + async def test_unavailable_api( hass: HomeAssistant, mock_nmbs_client: AsyncMock ) -> None: @@ -158,7 +176,10 @@ async def test_import( CONF_STATION_LIVE: "BE.NMBS.008813003", CONF_STATION_TO: "BE.NMBS.008814001", } - assert result["result"].unique_id == "BE.NMBS.008812005_BE.NMBS.008814001" + assert ( + result["result"].unique_id + == f"{DUMMY_DATA['STAT_BRUSSELS_NORTH']}_{DUMMY_DATA['STAT_BRUSSELS_SOUTH']}" + ) async def test_step_import_abort_if_already_setup(