diff --git a/homeassistant/components/sensor/vasttrafik.py b/homeassistant/components/sensor/vasttrafik.py index afa07f46fde..124b0ff44ea 100644 --- a/homeassistant/components/sensor/vasttrafik.py +++ b/homeassistant/components/sensor/vasttrafik.py @@ -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