core/homeassistant/components/sensor/deutsche_bahn.py

111 lines
3.3 KiB
Python
Raw Normal View History

2016-02-28 14:35:20 +00:00
"""
2016-06-30 00:44:35 +00:00
Support for information about the German train system.
2016-02-28 14:35:20 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.deutsche_bahn/
"""
import logging
2016-06-30 00:44:35 +00:00
from datetime import timedelta
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
2016-06-30 00:44:35 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle
from homeassistant.helpers.entity import Entity
2016-07-02 07:09:22 +00:00
import homeassistant.util.dt as dt_util
2016-11-12 20:30:05 +00:00
REQUIREMENTS = ['schiene==0.18']
2016-06-30 00:44:35 +00:00
_LOGGER = logging.getLogger(__name__)
2016-06-30 00:44:35 +00:00
CONF_DESTINATION = 'to'
CONF_START = 'from'
ICON = 'mdi:train'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
2016-06-30 00:44:35 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
2016-06-30 00:44:35 +00:00
vol.Required(CONF_DESTINATION): cv.string,
vol.Required(CONF_START): cv.string,
2016-06-30 00:44:35 +00:00
})
2016-06-30 00:44:35 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-02-28 14:35:20 +00:00
"""Setup the Deutsche Bahn Sensor."""
2016-06-30 00:44:35 +00:00
start = config.get(CONF_START)
destination = config.get(CONF_DESTINATION)
2016-06-30 00:44:35 +00:00
add_devices([DeutscheBahnSensor(start, destination)])
class DeutscheBahnSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Implementation of a Deutsche Bahn sensor."""
def __init__(self, start, goal):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
self._name = '{} to {}'.format(start, goal)
self.data = SchieneData(start, goal)
self.update()
@property
def name(self):
2016-02-28 14:35:20 +00:00
"""Return the name of the sensor."""
return self._name
@property
def icon(self):
2016-02-28 14:35:20 +00:00
"""Return the icon for the frontend."""
return ICON
@property
def state(self):
2016-02-28 14:35:20 +00:00
"""Return the departure time of the next train."""
return self._state
@property
def device_state_attributes(self):
2016-02-28 14:35:20 +00:00
"""Return the state attributes."""
2016-06-30 00:44:35 +00:00
connections = self.data.connections[0]
connections['next'] = self.data.connections[1]['departure']
connections['next_on'] = self.data.connections[2]['departure']
return connections
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest delay from bahn.de and updates the state."""
self.data.update()
self._state = self.data.connections[0].get('departure', 'Unknown')
if self.data.connections[0]['delay'] != 0:
2016-06-30 00:44:35 +00:00
self._state += " + {}".format(self.data.connections[0]['delay'])
class SchieneData(object):
2016-03-08 15:46:34 +00:00
"""Pull data from the bahn.de web page."""
def __init__(self, start, goal):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
import schiene
2016-07-02 07:09:22 +00:00
self.start = start
self.goal = goal
self.schiene = schiene.Schiene()
self.connections = [{}]
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
2016-02-28 14:35:20 +00:00
"""Update the connection data."""
2016-07-02 07:09:22 +00:00
self.connections = self.schiene.connections(
self.start, self.goal, dt_util.as_local(dt_util.utcnow()))
2016-06-30 00:44:35 +00:00
for con in self.connections:
2016-06-30 00:44:35 +00:00
# Detail info is not useful. Having a more consistent interface
# simplifies usage of template sensors.
if 'details' in con:
2016-02-28 14:35:20 +00:00
con.pop('details')
2016-06-30 00:44:35 +00:00
delay = con.get('delay', {'delay_departure': 0,
'delay_arrival': 0})
# IMHO only delay_departure is useful
con['delay'] = delay['delay_departure']
con['ontime'] = con.get('ontime', False)