Fix handling of empty results from Rejseplanen (#25610)

* Improve handling of empty results from Rejseplanen (Fixes #25566)

* Exclude attributes with null value

* Add period back into docstring

* Fix formatting
This commit is contained in:
Martin Eberhardt 2019-08-01 22:02:11 +02:00 committed by Martin Hjelmare
parent f7a47c6cab
commit c3cdd3e7d2

View File

@ -111,14 +111,14 @@ class RejseplanenTransportSensor(Entity):
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
if not self._times: if not self._times:
return None return {ATTR_STOP_ID: self._stop_id, ATTR_ATTRIBUTION: ATTRIBUTION}
next_up = [] next_up = []
if len(self._times) > 1: if len(self._times) > 1:
next_up = self._times[1:] next_up = self._times[1:]
params = { return {
ATTR_DUE_IN: str(self._times[0][ATTR_DUE_IN]), ATTR_DUE_IN: self._times[0][ATTR_DUE_IN],
ATTR_DUE_AT: self._times[0][ATTR_DUE_AT], ATTR_DUE_AT: self._times[0][ATTR_DUE_AT],
ATTR_TYPE: self._times[0][ATTR_TYPE], ATTR_TYPE: self._times[0][ATTR_TYPE],
ATTR_ROUTE: self._times[0][ATTR_ROUTE], ATTR_ROUTE: self._times[0][ATTR_ROUTE],
@ -128,7 +128,6 @@ class RejseplanenTransportSensor(Entity):
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_NEXT_UP: next_up, ATTR_NEXT_UP: next_up,
} }
return {k: v for k, v in params.items() if v}
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
@ -144,10 +143,14 @@ class RejseplanenTransportSensor(Entity):
"""Get the latest data from rejseplanen.dk and update the states.""" """Get the latest data from rejseplanen.dk and update the states."""
self.data.update() self.data.update()
self._times = self.data.info self._times = self.data.info
try:
self._state = self._times[0][ATTR_DUE_IN] if not self._times:
except TypeError: self._state = None
pass else:
try:
self._state = self._times[0][ATTR_DUE_IN]
except TypeError:
pass
class PublicTransportData: class PublicTransportData:
@ -159,20 +162,7 @@ class PublicTransportData:
self.route = route self.route = route
self.direction = direction self.direction = direction
self.departure_type = departure_type self.departure_type = departure_type
self.info = self.empty_result() self.info = []
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",
}
]
def update(self): def update(self):
"""Get the latest data from rejseplanen.""" """Get the latest data from rejseplanen."""
@ -200,11 +190,9 @@ class PublicTransportData:
) )
except rjpl.rjplAPIError as error: except rjpl.rjplAPIError as error:
_LOGGER.debug("API returned error: %s", error) _LOGGER.debug("API returned error: %s", error)
self.info = self.empty_result()
return return
except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): except (rjpl.rjplConnectionError, rjpl.rjplHTTPError):
_LOGGER.debug("Error occured while connecting to the API") _LOGGER.debug("Error occured while connecting to the API")
self.info = self.empty_result()
return return
# Filter result # Filter result
@ -246,7 +234,6 @@ class PublicTransportData:
if not self.info: if not self.info:
_LOGGER.debug("No departures with given parameters") _LOGGER.debug("No departures with given parameters")
self.info = self.empty_result()
# Sort the data by time # Sort the data by time
self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN)) self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN))