2017-03-26 17:06:40 +00:00
|
|
|
"""
|
|
|
|
Support for real-time departure information for public transport in Munich.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.mvglive/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2018-07-30 08:32:39 +00:00
|
|
|
from copy import deepcopy
|
2017-03-26 17:06:40 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-03-26 19:13:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-03-26 17:06:40 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2017-04-20 07:11:55 +00:00
|
|
|
from homeassistant.const import (
|
2019-01-24 07:20:20 +00:00
|
|
|
CONF_NAME, ATTR_ATTRIBUTION)
|
2017-03-26 17:06:40 +00:00
|
|
|
|
2017-05-30 16:26:26 +00:00
|
|
|
REQUIREMENTS = ['PyMVGLive==1.1.4']
|
2017-03-26 17:06:40 +00:00
|
|
|
|
2017-03-26 19:13:38 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-03-26 17:06:40 +00:00
|
|
|
|
2017-04-20 07:11:55 +00:00
|
|
|
CONF_NEXT_DEPARTURE = 'nextdeparture'
|
2017-03-26 19:13:38 +00:00
|
|
|
|
2017-04-20 07:11:55 +00:00
|
|
|
CONF_STATION = 'station'
|
|
|
|
CONF_DESTINATIONS = 'destinations'
|
|
|
|
CONF_DIRECTIONS = 'directions'
|
|
|
|
CONF_LINES = 'lines'
|
|
|
|
CONF_PRODUCTS = 'products'
|
|
|
|
CONF_TIMEOFFSET = 'timeoffset'
|
2018-07-30 08:32:39 +00:00
|
|
|
CONF_NUMBER = 'number'
|
2017-04-20 07:11:55 +00:00
|
|
|
|
2018-09-04 06:48:03 +00:00
|
|
|
DEFAULT_PRODUCT = ['U-Bahn', 'Tram', 'Bus', 'ExpressBus', 'S-Bahn']
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2017-04-20 07:11:55 +00:00
|
|
|
ICONS = {
|
|
|
|
'U-Bahn': 'mdi:subway',
|
|
|
|
'Tram': 'mdi:tram',
|
|
|
|
'Bus': 'mdi:bus',
|
2018-09-04 06:48:03 +00:00
|
|
|
'ExpressBus': 'mdi:bus',
|
2017-04-20 07:11:55 +00:00
|
|
|
'S-Bahn': 'mdi:train',
|
|
|
|
'SEV': 'mdi:checkbox-blank-circle-outline',
|
|
|
|
'-': 'mdi:clock'
|
|
|
|
}
|
|
|
|
ATTRIBUTION = "Data provided by MVG-live.de"
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2017-04-20 07:11:55 +00:00
|
|
|
vol.Optional(CONF_NEXT_DEPARTURE): [{
|
|
|
|
vol.Required(CONF_STATION): cv.string,
|
|
|
|
vol.Optional(CONF_DESTINATIONS, default=['']): cv.ensure_list_csv,
|
|
|
|
vol.Optional(CONF_DIRECTIONS, default=['']): cv.ensure_list_csv,
|
|
|
|
vol.Optional(CONF_LINES, default=['']): cv.ensure_list_csv,
|
2017-05-02 16:18:47 +00:00
|
|
|
vol.Optional(CONF_PRODUCTS, default=DEFAULT_PRODUCT):
|
|
|
|
cv.ensure_list_csv,
|
2017-04-20 07:11:55 +00:00
|
|
|
vol.Optional(CONF_TIMEOFFSET, default=0): cv.positive_int,
|
2018-07-30 08:32:39 +00:00
|
|
|
vol.Optional(CONF_NUMBER, default=1): cv.positive_int,
|
2017-04-20 07:11:55 +00:00
|
|
|
vol.Optional(CONF_NAME): cv.string}]
|
2017-03-26 17:06:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the MVGLive sensor."""
|
2017-04-20 07:11:55 +00:00
|
|
|
sensors = []
|
|
|
|
for nextdeparture in config.get(CONF_NEXT_DEPARTURE):
|
|
|
|
sensors.append(
|
|
|
|
MVGLiveSensor(
|
|
|
|
nextdeparture.get(CONF_STATION),
|
|
|
|
nextdeparture.get(CONF_DESTINATIONS),
|
|
|
|
nextdeparture.get(CONF_DIRECTIONS),
|
|
|
|
nextdeparture.get(CONF_LINES),
|
|
|
|
nextdeparture.get(CONF_PRODUCTS),
|
|
|
|
nextdeparture.get(CONF_TIMEOFFSET),
|
2018-07-30 08:32:39 +00:00
|
|
|
nextdeparture.get(CONF_NUMBER),
|
2017-04-20 07:11:55 +00:00
|
|
|
nextdeparture.get(CONF_NAME)))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors, True)
|
2017-04-20 07:11:55 +00:00
|
|
|
|
|
|
|
|
2017-03-26 17:06:40 +00:00
|
|
|
class MVGLiveSensor(Entity):
|
|
|
|
"""Implementation of an MVG Live sensor."""
|
|
|
|
|
2017-04-20 07:11:55 +00:00
|
|
|
def __init__(self, station, destinations, directions,
|
2018-07-30 08:32:39 +00:00
|
|
|
lines, products, timeoffset, number, name):
|
2017-03-26 17:06:40 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._station = station
|
2017-04-20 07:11:55 +00:00
|
|
|
self._name = name
|
|
|
|
self.data = MVGLiveData(station, destinations, directions,
|
2018-07-30 08:32:39 +00:00
|
|
|
lines, products, timeoffset, number)
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2017-04-20 07:11:55 +00:00
|
|
|
self._icon = ICONS['-']
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2017-04-20 07:11:55 +00:00
|
|
|
if self._name:
|
|
|
|
return self._name
|
2017-07-06 06:30:01 +00:00
|
|
|
return self._station
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2017-04-20 07:11:55 +00:00
|
|
|
"""Return the next departure time."""
|
2017-03-26 17:06:40 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2018-07-30 08:32:39 +00:00
|
|
|
def device_state_attributes(self):
|
2017-03-26 17:06:40 +00:00
|
|
|
"""Return the state attributes."""
|
2018-07-30 08:32:39 +00:00
|
|
|
dep = self.data.departures
|
|
|
|
if not dep:
|
|
|
|
return None
|
|
|
|
attr = dep[0] # next depature attributes
|
|
|
|
attr['departures'] = deepcopy(dep) # all departures dictionary
|
|
|
|
return attr
|
2017-04-20 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return "min"
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and update the state."""
|
|
|
|
self.data.update()
|
2017-04-20 07:11:55 +00:00
|
|
|
if not self.data.departures:
|
2017-03-26 17:06:40 +00:00
|
|
|
self._state = '-'
|
2017-04-20 07:11:55 +00:00
|
|
|
self._icon = ICONS['-']
|
2017-03-26 17:06:40 +00:00
|
|
|
else:
|
2018-07-30 08:32:39 +00:00
|
|
|
self._state = self.data.departures[0].get('time', '-')
|
|
|
|
self._icon = ICONS[self.data.departures[0].get('product', '-')]
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class MVGLiveData:
|
2017-03-26 17:06:40 +00:00
|
|
|
"""Pull data from the mvg-live.de web page."""
|
|
|
|
|
2017-04-20 07:11:55 +00:00
|
|
|
def __init__(self, station, destinations, directions,
|
2018-07-30 08:32:39 +00:00
|
|
|
lines, products, timeoffset, number):
|
2017-03-26 17:06:40 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
import MVGLive
|
|
|
|
self._station = station
|
2017-04-20 07:11:55 +00:00
|
|
|
self._destinations = destinations
|
|
|
|
self._directions = directions
|
|
|
|
self._lines = lines
|
|
|
|
self._products = products
|
|
|
|
self._timeoffset = timeoffset
|
2018-07-30 08:32:39 +00:00
|
|
|
self._number = number
|
2018-12-06 10:54:44 +00:00
|
|
|
self._include_ubahn = 'U-Bahn' in self._products
|
|
|
|
self._include_tram = 'Tram' in self._products
|
|
|
|
self._include_bus = 'Bus' in self._products
|
|
|
|
self._include_sbahn = 'S-Bahn' in self._products
|
2017-03-26 17:06:40 +00:00
|
|
|
self.mvg = MVGLive.MVGLive()
|
2018-07-30 08:32:39 +00:00
|
|
|
self.departures = []
|
2017-03-26 17:06:40 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the connection data."""
|
|
|
|
try:
|
2017-05-02 16:18:47 +00:00
|
|
|
_departures = self.mvg.getlivedata(
|
2018-07-30 08:32:39 +00:00
|
|
|
station=self._station,
|
|
|
|
timeoffset=self._timeoffset,
|
|
|
|
ubahn=self._include_ubahn,
|
|
|
|
tram=self._include_tram,
|
|
|
|
bus=self._include_bus,
|
2017-05-02 16:18:47 +00:00
|
|
|
sbahn=self._include_sbahn)
|
2017-03-26 17:06:40 +00:00
|
|
|
except ValueError:
|
2018-07-30 08:32:39 +00:00
|
|
|
self.departures = []
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.warning("Returned data not understood")
|
2017-03-26 17:06:40 +00:00
|
|
|
return
|
2018-07-30 08:32:39 +00:00
|
|
|
self.departures = []
|
|
|
|
for i, _departure in enumerate(_departures):
|
2017-03-26 17:06:40 +00:00
|
|
|
# find the first departure meeting the criteria
|
2017-04-20 07:11:55 +00:00
|
|
|
if ('' not in self._destinations[:1] and
|
|
|
|
_departure['destination'] not in self._destinations):
|
|
|
|
continue
|
|
|
|
elif ('' not in self._directions[:1] and
|
|
|
|
_departure['direction'] not in self._directions):
|
2017-03-26 17:06:40 +00:00
|
|
|
continue
|
2017-04-20 07:11:55 +00:00
|
|
|
elif ('' not in self._lines[:1] and
|
|
|
|
_departure['linename'] not in self._lines):
|
2017-04-11 11:55:42 +00:00
|
|
|
continue
|
2017-04-20 07:11:55 +00:00
|
|
|
elif _departure['time'] < self._timeoffset:
|
2017-03-26 17:06:40 +00:00
|
|
|
continue
|
|
|
|
# now select the relevant data
|
2017-04-20 07:11:55 +00:00
|
|
|
_nextdep = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2017-03-26 17:06:40 +00:00
|
|
|
for k in ['destination', 'linename', 'time', 'direction',
|
|
|
|
'product']:
|
|
|
|
_nextdep[k] = _departure.get(k, '')
|
|
|
|
_nextdep['time'] = int(_nextdep['time'])
|
2018-07-30 08:32:39 +00:00
|
|
|
self.departures.append(_nextdep)
|
|
|
|
if i == self._number - 1:
|
|
|
|
break
|