Take exclude vias in unique ids for nmbs (#136590)

This commit is contained in:
Simon Lamon 2025-01-29 14:25:28 +01:00 committed by GitHub
parent 32829596eb
commit d9deba3916
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 8 deletions

View File

@ -79,8 +79,9 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN):
for station in self.stations for station in self.stations
if station["id"] == user_input[CONF_STATION_TO] 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( 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() self._abort_if_unique_id_configured()
@ -154,12 +155,13 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN):
user_input[CONF_STATION_LIVE] = station_live["id"] user_input[CONF_STATION_LIVE] = station_live["id"]
entity_registry = er.async_get(self.hass) entity_registry = er.async_get(self.hass)
prefix = "live" prefix = "live"
vias = "_excl_vias" if user_input.get(CONF_EXCLUDE_VIAS, False) else ""
if entity_id := entity_registry.async_get_entity_id( if entity_id := entity_registry.async_get_entity_id(
Platform.SENSOR, Platform.SENSOR,
DOMAIN, DOMAIN,
f"{prefix}_{station_live['standardname']}_{station_from['standardname']}_{station_to['standardname']}", 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_registry.async_update_entity(
entity_id, new_unique_id=new_unique_id entity_id, new_unique_id=new_unique_id
) )
@ -168,7 +170,7 @@ class NMBSConfigFlow(ConfigFlow, domain=DOMAIN):
DOMAIN, DOMAIN,
f"{prefix}_{station_live['name']}_{station_from['name']}_{station_to['name']}", 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_registry.async_update_entity(
entity_id, new_unique_id=new_unique_id entity_id, new_unique_id=new_unique_id
) )

View File

@ -170,8 +170,10 @@ async def async_setup_entry(
NMBSSensor( NMBSSensor(
api_client, name, show_on_map, station_from, station_to, excl_vias api_client, name, show_on_map, station_from, station_to, excl_vias
), ),
NMBSLiveBoard(api_client, station_from, station_from, station_to), NMBSLiveBoard(
NMBSLiveBoard(api_client, station_to, station_from, station_to), 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], live_station: dict[str, Any],
station_from: dict[str, Any], station_from: dict[str, Any],
station_to: dict[str, Any], station_to: dict[str, Any],
excl_vias: bool,
) -> None: ) -> None:
"""Initialize the sensor for getting liveboard data.""" """Initialize the sensor for getting liveboard data."""
self._station = live_station self._station = live_station
self._api_client = api_client self._api_client = api_client
self._station_from = station_from self._station_from = station_from
self._station_to = station_to self._station_to = station_to
self._excl_vias = excl_vias
self._attrs: dict[str, Any] | None = {} self._attrs: dict[str, Any] | None = {}
self._state: str | None = None self._state: str | None = None
@ -210,7 +215,8 @@ class NMBSLiveBoard(SensorEntity):
unique_id = ( unique_id = (
f"{self._station['id']}_{self._station_from['id']}_{self._station_to['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 @property
def icon(self) -> str: def icon(self) -> str:
@ -303,7 +309,8 @@ class NMBSSensor(SensorEntity):
"""Return the unique ID.""" """Return the unique ID."""
unique_id = f"{self._station_from['id']}_{self._station_to['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 @property
def name(self) -> str: def name(self) -> str:

View File

@ -6,6 +6,7 @@ from unittest.mock import AsyncMock
import pytest import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.nmbs.config_flow import CONF_EXCLUDE_VIAS
from homeassistant.components.nmbs.const import ( from homeassistant.components.nmbs.const import (
CONF_STATION_FROM, CONF_STATION_FROM,
CONF_STATION_LIVE, CONF_STATION_LIVE,
@ -120,6 +121,23 @@ async def test_abort_if_exists(
assert result["reason"] == "already_configured" 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( async def test_unavailable_api(
hass: HomeAssistant, mock_nmbs_client: AsyncMock hass: HomeAssistant, mock_nmbs_client: AsyncMock
) -> None: ) -> None:
@ -158,7 +176,10 @@ async def test_import(
CONF_STATION_LIVE: "BE.NMBS.008813003", CONF_STATION_LIVE: "BE.NMBS.008813003",
CONF_STATION_TO: "BE.NMBS.008814001", 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( async def test_step_import_abort_if_already_setup(