From 551dc8f2e1c58b2953334c7b42c05cafc375df32 Mon Sep 17 00:00:00 2001 From: Martin Troels Eberhardt Date: Wed, 31 Jul 2019 21:19:46 +0200 Subject: [PATCH 1/4] Improve handling of empty results from Rejseplanen (Fixes #25566) --- .../components/rejseplanen/sensor.py | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/rejseplanen/sensor.py b/homeassistant/components/rejseplanen/sensor.py index 3ba2b06eb02f6b..b1f47e536fd68d 100755 --- a/homeassistant/components/rejseplanen/sensor.py +++ b/homeassistant/components/rejseplanen/sensor.py @@ -111,14 +111,24 @@ def state(self): def device_state_attributes(self): """Return the state attributes.""" if not self._times: - return None + return { + ATTR_DUE_IN: None, + ATTR_DUE_AT: None, + ATTR_TYPE: None, + ATTR_ROUTE: None, + ATTR_DIRECTION: None, + ATTR_STOP_NAME: None, + ATTR_STOP_ID: self._stop_id, + ATTR_ATTRIBUTION: ATTRIBUTION, + ATTR_NEXT_UP: None + } - next_up = [] + next_up = None if len(self._times) > 1: next_up = self._times[1:] - params = { - ATTR_DUE_IN: str(self._times[0][ATTR_DUE_IN]), + return { + ATTR_DUE_IN: self._times[0][ATTR_DUE_IN], ATTR_DUE_AT: self._times[0][ATTR_DUE_AT], ATTR_TYPE: self._times[0][ATTR_TYPE], ATTR_ROUTE: self._times[0][ATTR_ROUTE], @@ -128,7 +138,6 @@ def device_state_attributes(self): ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_NEXT_UP: next_up, } - return {k: v for k, v in params.items() if v} @property def unit_of_measurement(self): @@ -144,10 +153,14 @@ def update(self): """Get the latest data from rejseplanen.dk and update the states.""" self.data.update() self._times = self.data.info - try: - self._state = self._times[0][ATTR_DUE_IN] - except TypeError: - pass + + if not self._times: + self._state = None + else: + try: + self._state = self._times[0][ATTR_DUE_IN] + except TypeError: + pass class PublicTransportData: @@ -159,20 +172,7 @@ def __init__(self, stop_id, route, direction, departure_type): self.route = route self.direction = direction self.departure_type = departure_type - self.info = self.empty_result() - - def empty_result(self): - """Object returned when no departures are found.""" - return [ - { - ATTR_DUE_IN: "n/a", - ATTR_DUE_AT: "n/a", - ATTR_TYPE: "n/a", - ATTR_ROUTE: self.route, - ATTR_DIRECTION: "n/a", - ATTR_STOP_NAME: "n/a", - } - ] + self.info = [] def update(self): """Get the latest data from rejseplanen.""" @@ -200,11 +200,9 @@ def intersection(lst1, lst2): ) except rjpl.rjplAPIError as error: _LOGGER.debug("API returned error: %s", error) - self.info = self.empty_result() return except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): _LOGGER.debug("Error occured while connecting to the API") - self.info = self.empty_result() return # Filter result @@ -246,7 +244,6 @@ def intersection(lst1, lst2): if not self.info: _LOGGER.debug("No departures with given parameters") - self.info = self.empty_result() # Sort the data by time self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN)) From 4c4d7c7b5515ad6e5539dcd443656cbd47078979 Mon Sep 17 00:00:00 2001 From: Martin Troels Eberhardt Date: Wed, 31 Jul 2019 22:28:41 +0200 Subject: [PATCH 2/4] Exclude attributes with null value --- homeassistant/components/rejseplanen/sensor.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/rejseplanen/sensor.py b/homeassistant/components/rejseplanen/sensor.py index b1f47e536fd68d..fdab0c8d17df20 100755 --- a/homeassistant/components/rejseplanen/sensor.py +++ b/homeassistant/components/rejseplanen/sensor.py @@ -112,18 +112,11 @@ def device_state_attributes(self): """Return the state attributes.""" if not self._times: return { - ATTR_DUE_IN: None, - ATTR_DUE_AT: None, - ATTR_TYPE: None, - ATTR_ROUTE: None, - ATTR_DIRECTION: None, - ATTR_STOP_NAME: None, ATTR_STOP_ID: self._stop_id, - ATTR_ATTRIBUTION: ATTRIBUTION, - ATTR_NEXT_UP: None + ATTR_ATTRIBUTION: ATTRIBUTION } - next_up = None + next_up = [] if len(self._times) > 1: next_up = self._times[1:] @@ -181,7 +174,7 @@ def update(self): self.info = [] def intersection(lst1, lst2): - """Return items contained in both lists.""" + """Return items contained in both lists""" return list(set(lst1) & set(lst2)) # Limit search to selected types, to get more results From 1dc5fb201630df607fed16fcabd56c230d8f122e Mon Sep 17 00:00:00 2001 From: Martin Troels Eberhardt Date: Wed, 31 Jul 2019 23:10:08 +0200 Subject: [PATCH 3/4] Add period back into docstring --- homeassistant/components/rejseplanen/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/rejseplanen/sensor.py b/homeassistant/components/rejseplanen/sensor.py index fdab0c8d17df20..a112e364fcda3f 100755 --- a/homeassistant/components/rejseplanen/sensor.py +++ b/homeassistant/components/rejseplanen/sensor.py @@ -174,7 +174,7 @@ def update(self): self.info = [] def intersection(lst1, lst2): - """Return items contained in both lists""" + """Return items contained in both lists.""" return list(set(lst1) & set(lst2)) # Limit search to selected types, to get more results From 7e3c24eb25049a9d73b44926b4bfecb0de9a8d73 Mon Sep 17 00:00:00 2001 From: Martin Troels Eberhardt Date: Thu, 1 Aug 2019 18:41:22 +0200 Subject: [PATCH 4/4] Fix formatting --- homeassistant/components/rejseplanen/sensor.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/rejseplanen/sensor.py b/homeassistant/components/rejseplanen/sensor.py index a112e364fcda3f..99cfe1067e8932 100755 --- a/homeassistant/components/rejseplanen/sensor.py +++ b/homeassistant/components/rejseplanen/sensor.py @@ -111,10 +111,7 @@ def state(self): def device_state_attributes(self): """Return the state attributes.""" if not self._times: - return { - ATTR_STOP_ID: self._stop_id, - ATTR_ATTRIBUTION: ATTRIBUTION - } + return {ATTR_STOP_ID: self._stop_id, ATTR_ATTRIBUTION: ATTRIBUTION} next_up = [] if len(self._times) > 1: