mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Fix rmvtransport breaking when destinations don't match (#38401)
This commit is contained in:
parent
89cbcdb9dc
commit
d02c432e4d
@ -100,7 +100,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
tasks = [sensor.async_update() for sensor in sensors]
|
tasks = [sensor.async_update() for sensor in sensors]
|
||||||
if tasks:
|
if tasks:
|
||||||
await asyncio.wait(tasks)
|
await asyncio.wait(tasks)
|
||||||
if not all(sensor.data.departures for sensor in sensors):
|
|
||||||
|
if not any(sensor.data for sensor in sensors):
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
@ -165,6 +166,7 @@ class RMVDepartureSensor(Entity):
|
|||||||
"minutes": self.data.departures[0].get("minutes"),
|
"minutes": self.data.departures[0].get("minutes"),
|
||||||
"departure_time": self.data.departures[0].get("departure_time"),
|
"departure_time": self.data.departures[0].get("departure_time"),
|
||||||
"product": self.data.departures[0].get("product"),
|
"product": self.data.departures[0].get("product"),
|
||||||
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
}
|
}
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return {}
|
return {}
|
||||||
@ -183,13 +185,16 @@ class RMVDepartureSensor(Entity):
|
|||||||
"""Get the latest data and update the state."""
|
"""Get the latest data and update the state."""
|
||||||
await self.data.async_update()
|
await self.data.async_update()
|
||||||
|
|
||||||
|
if self._name == DEFAULT_NAME:
|
||||||
|
self._name = self.data.station
|
||||||
|
|
||||||
|
self._station = self.data.station
|
||||||
|
|
||||||
if not self.data.departures:
|
if not self.data.departures:
|
||||||
self._state = None
|
self._state = None
|
||||||
self._icon = ICONS[None]
|
self._icon = ICONS[None]
|
||||||
return
|
return
|
||||||
if self._name == DEFAULT_NAME:
|
|
||||||
self._name = self.data.station
|
|
||||||
self._station = self.data.station
|
|
||||||
self._state = self.data.departures[0].get("minutes")
|
self._state = self.data.departures[0].get("minutes")
|
||||||
self._icon = ICONS[self.data.departures[0].get("product")]
|
self._icon = ICONS[self.data.departures[0].get("product")]
|
||||||
|
|
||||||
@ -220,6 +225,7 @@ class RMVDepartureData:
|
|||||||
self._max_journeys = max_journeys
|
self._max_journeys = max_journeys
|
||||||
self.rmv = RMVtransport(session, timeout)
|
self.rmv = RMVtransport(session, timeout)
|
||||||
self.departures = []
|
self.departures = []
|
||||||
|
self._error_notification = False
|
||||||
|
|
||||||
@Throttle(SCAN_INTERVAL)
|
@Throttle(SCAN_INTERVAL)
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
@ -231,31 +237,49 @@ class RMVDepartureData:
|
|||||||
direction_id=self._direction,
|
direction_id=self._direction,
|
||||||
max_journeys=50,
|
max_journeys=50,
|
||||||
)
|
)
|
||||||
|
|
||||||
except RMVtransportApiConnectionError:
|
except RMVtransportApiConnectionError:
|
||||||
self.departures = []
|
self.departures = []
|
||||||
_LOGGER.warning("Could not retrieve data from rmv.de")
|
_LOGGER.warning("Could not retrieve data from rmv.de")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.station = _data.get("station")
|
self.station = _data.get("station")
|
||||||
|
|
||||||
_deps = []
|
_deps = []
|
||||||
|
_deps_not_found = set(self._destinations)
|
||||||
|
|
||||||
for journey in _data["journeys"]:
|
for journey in _data["journeys"]:
|
||||||
# find the first departure meeting the criteria
|
# find the first departure meeting the criteria
|
||||||
_nextdep = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
_nextdep = {}
|
||||||
if self._destinations:
|
if self._destinations:
|
||||||
dest_found = False
|
dest_found = False
|
||||||
for dest in self._destinations:
|
for dest in self._destinations:
|
||||||
if dest in journey["stops"]:
|
if dest in journey["stops"]:
|
||||||
dest_found = True
|
dest_found = True
|
||||||
|
if dest in _deps_not_found:
|
||||||
|
_deps_not_found.remove(dest)
|
||||||
_nextdep["destination"] = dest
|
_nextdep["destination"] = dest
|
||||||
|
|
||||||
if not dest_found:
|
if not dest_found:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif self._lines and journey["number"] not in self._lines:
|
elif self._lines and journey["number"] not in self._lines:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif journey["minutes"] < self._time_offset:
|
elif journey["minutes"] < self._time_offset:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for attr in ["direction", "departure_time", "product", "minutes"]:
|
for attr in ["direction", "departure_time", "product", "minutes"]:
|
||||||
_nextdep[attr] = journey.get(attr, "")
|
_nextdep[attr] = journey.get(attr, "")
|
||||||
|
|
||||||
_nextdep["line"] = journey.get("number", "")
|
_nextdep["line"] = journey.get("number", "")
|
||||||
_deps.append(_nextdep)
|
_deps.append(_nextdep)
|
||||||
|
|
||||||
if len(_deps) > self._max_journeys:
|
if len(_deps) > self._max_journeys:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if not self._error_notification and _deps_not_found:
|
||||||
|
self._error_notification = True
|
||||||
|
_LOGGER.info("Destination(s) %s not found", ", ".join(_deps_not_found))
|
||||||
|
|
||||||
self.departures = _deps
|
self.departures = _deps
|
||||||
|
@ -48,7 +48,7 @@ VALID_CONFIG_DEST = {
|
|||||||
|
|
||||||
def get_departures_mock():
|
def get_departures_mock():
|
||||||
"""Mock rmvtransport departures loading."""
|
"""Mock rmvtransport departures loading."""
|
||||||
data = {
|
return {
|
||||||
"station": "Frankfurt (Main) Hauptbahnhof",
|
"station": "Frankfurt (Main) Hauptbahnhof",
|
||||||
"stationId": "3000010",
|
"stationId": "3000010",
|
||||||
"filter": "11111111111",
|
"filter": "11111111111",
|
||||||
@ -145,18 +145,16 @@ def get_departures_mock():
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def get_no_departures_mock():
|
def get_no_departures_mock():
|
||||||
"""Mock no departures in results."""
|
"""Mock no departures in results."""
|
||||||
data = {
|
return {
|
||||||
"station": "Frankfurt (Main) Hauptbahnhof",
|
"station": "Frankfurt (Main) Hauptbahnhof",
|
||||||
"stationId": "3000010",
|
"stationId": "3000010",
|
||||||
"filter": "11111111111",
|
"filter": "11111111111",
|
||||||
"journeys": [],
|
"journeys": [],
|
||||||
}
|
}
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
async def test_rmvtransport_min_config(hass):
|
async def test_rmvtransport_min_config(hass):
|
||||||
@ -232,4 +230,4 @@ async def test_rmvtransport_no_departures(hass):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("sensor.frankfurt_main_hauptbahnhof")
|
state = hass.states.get("sensor.frankfurt_main_hauptbahnhof")
|
||||||
assert not state
|
assert state.state == "unavailable"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user