From 570f80a73c28914472661a5cd086d108c465e282 Mon Sep 17 00:00:00 2001 From: ericvb Date: Sun, 14 Nov 2021 03:22:36 +0100 Subject: [PATCH] Check early for empty passages in delijn (#59612) * Add a check to verify if there is a passage Late in the evening and at night, there can be no passages anymore, so check it to avoid an unnecessary exception * One passage is enough! Requesting minimum 2 passages was an error due to counting from 1 and not zero * Invert check and put it out of the try-catch code Adding also the KeyError in the log message * Clean up * Putting comment in the correct python syntax * Clean up Co-authored-by: Martin Hjelmare --- homeassistant/components/delijn/sensor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/delijn/sensor.py b/homeassistant/components/delijn/sensor.py index 395c2d93dff..310e6a7f7fc 100644 --- a/homeassistant/components/delijn/sensor.py +++ b/homeassistant/components/delijn/sensor.py @@ -82,6 +82,10 @@ class DeLijnPublicTransportSensor(SensorEntity): self._attributes["stopname"] = self._name + if not self.line.passages: + self._available = False + return + try: first = self.line.passages[0] if first["due_at_realtime"] is not None: @@ -97,8 +101,8 @@ class DeLijnPublicTransportSensor(SensorEntity): self._attributes["is_realtime"] = first["is_realtime"] self._attributes["next_passages"] = self.line.passages self._available = True - except (KeyError, IndexError): - _LOGGER.error("Invalid data received from De Lijn") + except (KeyError) as error: + _LOGGER.error("Invalid data received from De Lijn: %s", error) self._available = False @property