Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state and attribute fetching in vasttrafik #19856

Merged
merged 4 commits into from
Jan 8, 2019
Merged
Changes from 2 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
62 changes: 30 additions & 32 deletions homeassistant/components/sensor/vasttrafik.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def __init__(self, vasttrafik, planner, name, departure, heading,
self._lines = lines if lines else None
self._delay = timedelta(minutes=delay)
self._departureboard = None
self._state = None
self._attributes = None

@property
def name(self):
Expand All @@ -98,42 +100,12 @@ def icon(self):
@property
def device_state_attributes(self):
"""Return the state attributes."""
if not self._departureboard:
return

for departure in self._departureboard:
line = departure.get('sname')
if not self._lines or line in self._lines:
params = {
ATTR_ACCESSIBILITY: departure.get('accessibility'),
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_DIRECTION: departure.get('direction'),
ATTR_LINE: departure.get('sname'),
ATTR_TRACK: departure.get('track'),
}
return {k: v for k, v in params.items() if v}
return self._attributes

@property
def state(self):
"""Return the next departure time."""
if not self._departureboard:
_LOGGER.debug(
"No departures from %s heading %s",
self._departure['name'],
self._heading['name'] if self._heading else 'ANY')
return
for departure in self._departureboard:
line = departure.get('sname')
if not self._lines or line in self._lines:
if 'rtTime' in self._departureboard[0]:
return self._departureboard[0]['rtTime']
return self._departureboard[0]['time']
# No departures of given lines found
_LOGGER.debug(
"No departures from %s heading %s on line(s) %s",
self._departure['name'],
self._heading['name'] if self._heading else 'ANY',
', '.join((str(line) for line in self._lines)))
return self._state

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
Expand All @@ -146,3 +118,29 @@ def update(self):
except self._vasttrafik.Error:
_LOGGER.debug("Unable to read departure board, updating token")
self._planner.update_token()

if not self._departureboard:
_LOGGER.debug(
"No departures from %s heading %s",
self._departure['name'],
self._heading['name'] if self._heading else 'ANY')
self._state = "No departures"
kennedyshead marked this conversation as resolved.
Show resolved Hide resolved
self._attributes = {}
else:
for departure in self._departureboard:
line = departure.get('sname')
if not self._lines or line in self._lines:
if 'rtTime' in self._departureboard[0]:
self._state = self._departureboard[0]['rtTime']
self._state = self._departureboard[0]['time']

params = {
ATTR_ACCESSIBILITY: departure.get('accessibility'),
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_DIRECTION: departure.get('direction'),
ATTR_LINE: departure.get('sname'),
ATTR_TRACK: departure.get('track'),
}

self._attributes = {
k: v for k, v in params.items() if v}