Fix state and attribute fetching in vasttrafik (#19856)

* Fixing state and attribute fetching

* Fixing state and attribute fetching

* Setting None state

* Need to brreak loop
pull/19868/head
kennedyshead 2019-01-08 09:54:22 +01:00 committed by Martin Hjelmare
parent fb9aad8791
commit c7a32e59b7
1 changed files with 32 additions and 32 deletions

View File

@ -84,6 +84,8 @@ class VasttrafikDepartureSensor(Entity):
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):
@ -98,42 +100,12 @@ class VasttrafikDepartureSensor(Entity):
@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):
@ -146,3 +118,31 @@ class VasttrafikDepartureSensor(Entity):
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 = None
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']
else:
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}
break