Skip to content

Commit f6fb4c8

Browse files
authored
Add unique id to nederlandse spoorwegen (home-assistant#154013)
1 parent a6e575e commit f6fb4c8

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

homeassistant/components/nederlandse_spoorwegen/sensor.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ async def async_setup_entry(
129129
subentry.data[CONF_NAME],
130130
subentry.data[CONF_FROM],
131131
subentry.data[CONF_TO],
132+
subentry.subentry_id,
132133
subentry.data.get(CONF_VIA),
133-
parse_time(subentry.data[CONF_TIME])
134-
if CONF_TIME in subentry.data
135-
else None,
134+
(
135+
parse_time(subentry.data[CONF_TIME])
136+
if CONF_TIME in subentry.data
137+
else None
138+
),
136139
)
137140
],
138141
config_subentry_id=subentry.subentry_id,
@@ -153,6 +156,7 @@ def __init__(
153156
name: str,
154157
departure: str,
155158
heading: str,
159+
subentry_id: str,
156160
via: str | None,
157161
time: dt.time | None,
158162
) -> None:
@@ -166,6 +170,7 @@ def __init__(
166170
self._trips: list[Trip] | None = None
167171
self._first_trip: Trip | None = None
168172
self._next_trip: Trip | None = None
173+
self._attr_unique_id = f"{subentry_id}-actual_departure"
169174

170175
@property
171176
def name(self) -> str:

tests/components/nederlandse_spoorwegen/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
CONF_VIA,
1313
DOMAIN,
1414
)
15-
from homeassistant.config_entries import ConfigSubentryData
15+
from homeassistant.config_entries import ConfigSubentryDataWithId
1616
from homeassistant.const import CONF_API_KEY, CONF_NAME
1717

1818
from .const import API_KEY
@@ -61,7 +61,7 @@ def mock_config_entry() -> MockConfigEntry:
6161
data={CONF_API_KEY: API_KEY},
6262
domain=DOMAIN,
6363
subentries_data=[
64-
ConfigSubentryData(
64+
ConfigSubentryDataWithId(
6565
data={
6666
CONF_NAME: "To work",
6767
CONF_FROM: "Ams",
@@ -71,6 +71,7 @@ def mock_config_entry() -> MockConfigEntry:
7171
subentry_type="route",
7272
title="Test Route",
7373
unique_id=None,
74+
subentry_id="01K721DZPMEN39R5DK0ATBMSY8",
7475
),
7576
],
7677
)

tests/components/nederlandse_spoorwegen/snapshots/test_sensor.ambr

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
# serializer version: 1
2-
# name: test_sensor
2+
# name: test_sensor[sensor.to_work-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': None,
8+
'config_entry_id': <ANY>,
9+
'config_subentry_id': <ANY>,
10+
'device_class': None,
11+
'device_id': <ANY>,
12+
'disabled_by': None,
13+
'domain': 'sensor',
14+
'entity_category': None,
15+
'entity_id': 'sensor.to_work',
16+
'has_entity_name': False,
17+
'hidden_by': None,
18+
'icon': None,
19+
'id': <ANY>,
20+
'labels': set({
21+
}),
22+
'name': None,
23+
'options': dict({
24+
}),
25+
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
26+
'original_icon': 'mdi:train',
27+
'original_name': 'To work',
28+
'platform': 'nederlandse_spoorwegen',
29+
'previous_unique_id': None,
30+
'suggested_object_id': None,
31+
'supported_features': 0,
32+
'translation_key': None,
33+
'unique_id': '01K721DZPMEN39R5DK0ATBMSY8-actual_departure',
34+
'unit_of_measurement': None,
35+
})
36+
# ---
37+
# name: test_sensor[sensor.to_work-state]
338
StateSnapshot({
439
'attributes': ReadOnlyDict({
540
'arrival_delay': False,

tests/components/nederlandse_spoorwegen/test_sensor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
1616
from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_PLATFORM
1717
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
18+
import homeassistant.helpers.entity_registry as er
1819
import homeassistant.helpers.issue_registry as ir
1920
from homeassistant.setup import async_setup_component
2021

2122
from . import setup_integration
2223
from .const import API_KEY
2324

24-
from tests.common import MockConfigEntry
25+
from tests.common import MockConfigEntry, snapshot_platform
2526

2627

2728
async def test_config_import(
@@ -65,8 +66,9 @@ async def test_sensor(
6566
mock_nsapi,
6667
mock_config_entry: MockConfigEntry,
6768
snapshot: SnapshotAssertion,
69+
entity_registry: er.EntityRegistry,
6870
) -> None:
6971
"""Test sensor initialization."""
7072
await setup_integration(hass, mock_config_entry)
7173

72-
assert hass.states.get("sensor.to_work") == snapshot
74+
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)

0 commit comments

Comments
 (0)