Create truly live unique id (#31078)

* Create truly live unique id

* Do not generate unique id on basis of name

* Add the station to the default station live name
This commit is contained in:
Thibault Maekelbergh 2020-01-29 12:12:40 +01:00 committed by GitHub
parent 080827fb62
commit 64edf2fe33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,7 +80,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
] ]
if station_live is not None: if station_live is not None:
sensors.append(NMBSLiveBoard(api_client, station_live)) sensors.append(
NMBSLiveBoard(api_client, station_live, station_from, station_to)
)
add_entities(sensors, True) add_entities(sensors, True)
@ -88,23 +90,26 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class NMBSLiveBoard(Entity): class NMBSLiveBoard(Entity):
"""Get the next train from a station's liveboard.""" """Get the next train from a station's liveboard."""
def __init__(self, api_client, live_station): def __init__(self, api_client, live_station, station_from, station_to):
"""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._unique_id = f"nmbs_live_{self._station}" self._station_from = station_from
self._station_to = station_to
self._attrs = {} self._attrs = {}
self._state = None self._state = None
@property @property
def name(self): def name(self):
"""Return the sensor default name.""" """Return the sensor default name."""
return "NMBS Live" return f"NMBS Live ({self._station})"
@property @property
def unique_id(self): def unique_id(self):
"""Return a unique ID.""" """Return a unique ID."""
return self._unique_id unique_id = f"{self._station}_{self._station_from}_{self._station_to}"
return f"nmbs_live_{unique_id}"
@property @property
def icon(self): def icon(self):