Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions homeassistant/components/rejseplanen/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def state(self):
def device_state_attributes(self):
"""Return the state attributes."""
if not self._times:
return None
return {ATTR_STOP_ID: self._stop_id, ATTR_ATTRIBUTION: ATTRIBUTION}

next_up = []
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],
Expand All @@ -128,7 +128,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):
Expand All @@ -144,10 +143,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:
Expand All @@ -159,20 +162,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."""
Expand Down Expand Up @@ -200,11 +190,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
Expand Down Expand Up @@ -246,7 +234,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))