Add mvglive option to store multiple departures in attributes (#15454)

* MVG Live sensor: add option to store multiple departures in attributes

* Fix lint error

* mvglive: take into account timeoffset in API call

* Prevent exception if departure list is empty

* Rename state_attributes -> device_state_attributes
This commit is contained in:
David Straub 2018-07-30 10:32:39 +02:00 committed by Martin Hjelmare
parent 8dbe78a21a
commit 460bb69ade

View File

@ -7,6 +7,7 @@ https://home-assistant.io/components/sensor.mvglive/
import logging import logging
from datetime import timedelta from datetime import timedelta
from copy import deepcopy
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -28,6 +29,7 @@ CONF_DIRECTIONS = 'directions'
CONF_LINES = 'lines' CONF_LINES = 'lines'
CONF_PRODUCTS = 'products' CONF_PRODUCTS = 'products'
CONF_TIMEOFFSET = 'timeoffset' CONF_TIMEOFFSET = 'timeoffset'
CONF_NUMBER = 'number'
DEFAULT_PRODUCT = ['U-Bahn', 'Tram', 'Bus', 'S-Bahn'] DEFAULT_PRODUCT = ['U-Bahn', 'Tram', 'Bus', 'S-Bahn']
@ -52,6 +54,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_PRODUCTS, default=DEFAULT_PRODUCT): vol.Optional(CONF_PRODUCTS, default=DEFAULT_PRODUCT):
cv.ensure_list_csv, cv.ensure_list_csv,
vol.Optional(CONF_TIMEOFFSET, default=0): cv.positive_int, vol.Optional(CONF_TIMEOFFSET, default=0): cv.positive_int,
vol.Optional(CONF_NUMBER, default=1): cv.positive_int,
vol.Optional(CONF_NAME): cv.string}] vol.Optional(CONF_NAME): cv.string}]
}) })
@ -68,6 +71,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
nextdeparture.get(CONF_LINES), nextdeparture.get(CONF_LINES),
nextdeparture.get(CONF_PRODUCTS), nextdeparture.get(CONF_PRODUCTS),
nextdeparture.get(CONF_TIMEOFFSET), nextdeparture.get(CONF_TIMEOFFSET),
nextdeparture.get(CONF_NUMBER),
nextdeparture.get(CONF_NAME))) nextdeparture.get(CONF_NAME)))
add_devices(sensors, True) add_devices(sensors, True)
@ -76,12 +80,12 @@ class MVGLiveSensor(Entity):
"""Implementation of an MVG Live sensor.""" """Implementation of an MVG Live sensor."""
def __init__(self, station, destinations, directions, def __init__(self, station, destinations, directions,
lines, products, timeoffset, name): lines, products, timeoffset, number, name):
"""Initialize the sensor.""" """Initialize the sensor."""
self._station = station self._station = station
self._name = name self._name = name
self.data = MVGLiveData(station, destinations, directions, self.data = MVGLiveData(station, destinations, directions,
lines, products, timeoffset) lines, products, timeoffset, number)
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self._icon = ICONS['-'] self._icon = ICONS['-']
@ -98,9 +102,14 @@ class MVGLiveSensor(Entity):
return self._state return self._state
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return self.data.departures dep = self.data.departures
if not dep:
return None
attr = dep[0] # next depature attributes
attr['departures'] = deepcopy(dep) # all departures dictionary
return attr
@property @property
def icon(self): def icon(self):
@ -119,15 +128,15 @@ class MVGLiveSensor(Entity):
self._state = '-' self._state = '-'
self._icon = ICONS['-'] self._icon = ICONS['-']
else: else:
self._state = self.data.departures.get('time', '-') self._state = self.data.departures[0].get('time', '-')
self._icon = ICONS[self.data.departures.get('product', '-')] self._icon = ICONS[self.data.departures[0].get('product', '-')]
class MVGLiveData: class MVGLiveData:
"""Pull data from the mvg-live.de web page.""" """Pull data from the mvg-live.de web page."""
def __init__(self, station, destinations, directions, def __init__(self, station, destinations, directions,
lines, products, timeoffset): lines, products, timeoffset, number):
"""Initialize the sensor.""" """Initialize the sensor."""
import MVGLive import MVGLive
self._station = station self._station = station
@ -136,25 +145,30 @@ class MVGLiveData:
self._lines = lines self._lines = lines
self._products = products self._products = products
self._timeoffset = timeoffset self._timeoffset = timeoffset
self._number = number
self._include_ubahn = True if 'U-Bahn' in self._products else False self._include_ubahn = True if 'U-Bahn' in self._products else False
self._include_tram = True if 'Tram' in self._products else False self._include_tram = True if 'Tram' in self._products else False
self._include_bus = True if 'Bus' in self._products else False self._include_bus = True if 'Bus' in self._products else False
self._include_sbahn = True if 'S-Bahn' in self._products else False self._include_sbahn = True if 'S-Bahn' in self._products else False
self.mvg = MVGLive.MVGLive() self.mvg = MVGLive.MVGLive()
self.departures = {} self.departures = []
def update(self): def update(self):
"""Update the connection data.""" """Update the connection data."""
try: try:
_departures = self.mvg.getlivedata( _departures = self.mvg.getlivedata(
station=self._station, ubahn=self._include_ubahn, station=self._station,
tram=self._include_tram, bus=self._include_bus, timeoffset=self._timeoffset,
ubahn=self._include_ubahn,
tram=self._include_tram,
bus=self._include_bus,
sbahn=self._include_sbahn) sbahn=self._include_sbahn)
except ValueError: except ValueError:
self.departures = {} self.departures = []
_LOGGER.warning("Returned data not understood") _LOGGER.warning("Returned data not understood")
return return
for _departure in _departures: self.departures = []
for i, _departure in enumerate(_departures):
# find the first departure meeting the criteria # find the first departure meeting the criteria
if ('' not in self._destinations[:1] and if ('' not in self._destinations[:1] and
_departure['destination'] not in self._destinations): _departure['destination'] not in self._destinations):
@ -173,5 +187,6 @@ class MVGLiveData:
'product']: 'product']:
_nextdep[k] = _departure.get(k, '') _nextdep[k] = _departure.get(k, '')
_nextdep['time'] = int(_nextdep['time']) _nextdep['time'] = int(_nextdep['time'])
self.departures = _nextdep self.departures.append(_nextdep)
break if i == self._number - 1:
break