Check early for empty passages in delijn ()

* 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 <marhje52@gmail.com>
pull/59667/head
ericvb 2021-11-14 03:22:36 +01:00 committed by GitHub
parent 5860932635
commit 570f80a73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions
homeassistant/components/delijn

View File

@ -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