2019-03-11 20:25:29 +00:00
|
|
|
"""Support for information about the German train system."""
|
2016-06-30 00:44:35 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-10-13 12:54:38 +00:00
|
|
|
import schiene
|
2019-12-09 16:42:00 +00:00
|
|
|
import voluptuous as vol
|
2019-10-13 12:54:38 +00:00
|
|
|
|
2017-06-05 15:35:26 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2021-02-05 09:29:59 +00:00
|
|
|
from homeassistant.const import CONF_OFFSET
|
2017-12-01 11:26:15 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-06-05 15:35:26 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-12-01 11:26:15 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-02-21 11:18:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DESTINATION = "to"
|
|
|
|
CONF_START = "from"
|
2019-06-27 13:53:05 +00:00
|
|
|
DEFAULT_OFFSET = timedelta(minutes=0)
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ONLY_DIRECT = "only_direct"
|
2018-01-29 22:17:58 +00:00
|
|
|
DEFAULT_ONLY_DIRECT = False
|
2016-08-20 22:40:16 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:train"
|
2016-02-21 11:18:18 +00:00
|
|
|
|
2017-06-05 15:35:26 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=2)
|
2016-06-30 00:44:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_DESTINATION): cv.string,
|
|
|
|
vol.Required(CONF_START): cv.string,
|
|
|
|
vol.Optional(CONF_OFFSET, default=DEFAULT_OFFSET): cv.time_period,
|
|
|
|
vol.Optional(CONF_ONLY_DIRECT, default=DEFAULT_ONLY_DIRECT): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-06-30 00:44:35 +00:00
|
|
|
|
2016-02-21 11:18:18 +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 Deutsche Bahn Sensor."""
|
2016-06-30 00:44:35 +00:00
|
|
|
start = config.get(CONF_START)
|
2020-04-15 12:10:07 +00:00
|
|
|
destination = config[CONF_DESTINATION]
|
|
|
|
offset = config[CONF_OFFSET]
|
|
|
|
only_direct = config[CONF_ONLY_DIRECT]
|
2016-02-21 11:18:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities([DeutscheBahnSensor(start, destination, offset, only_direct)], True)
|
2016-02-21 11:18:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeutscheBahnSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of a Deutsche Bahn sensor."""
|
|
|
|
|
2019-06-27 13:53:05 +00:00
|
|
|
def __init__(self, start, goal, offset, only_direct):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-09-03 15:09:59 +00:00
|
|
|
self._name = f"{start} to {goal}"
|
2019-06-27 13:53:05 +00:00
|
|
|
self.data = SchieneData(start, goal, offset, only_direct)
|
2017-06-05 15:35:26 +00:00
|
|
|
self._state = None
|
2016-02-21 11:18:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-28 14:35:20 +00:00
|
|
|
"""Return the name of the sensor."""
|
2016-02-21 11:18:18 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-02-28 14:35:20 +00:00
|
|
|
"""Return the icon for the frontend."""
|
2016-02-21 11:18:18 +00:00
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-02-28 14:35:20 +00:00
|
|
|
"""Return the departure time of the next train."""
|
2016-02-21 11:18:18 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_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]
|
2018-01-29 22:49:38 +00:00
|
|
|
if len(self.data.connections) > 1:
|
2019-07-31 19:25:30 +00:00
|
|
|
connections["next"] = self.data.connections[1]["departure"]
|
2018-01-29 22:49:38 +00:00
|
|
|
if len(self.data.connections) > 2:
|
2019-07-31 19:25:30 +00:00
|
|
|
connections["next_on"] = self.data.connections[2]["departure"]
|
2016-06-30 00:44:35 +00:00
|
|
|
return connections
|
2016-02-21 11:18:18 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest delay from bahn.de and updates the state."""
|
2016-02-21 11:18:18 +00:00
|
|
|
self.data.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.data.connections[0].get("departure", "Unknown")
|
|
|
|
if self.data.connections[0].get("delay", 0) != 0:
|
2020-02-24 16:47:52 +00:00
|
|
|
self._state += f" + {self.data.connections[0]['delay']}"
|
2016-02-21 11:18:18 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class SchieneData:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Pull data from the bahn.de web page."""
|
|
|
|
|
2019-06-27 13:53:05 +00:00
|
|
|
def __init__(self, start, goal, offset, only_direct):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-02-21 11:18:18 +00:00
|
|
|
self.start = start
|
|
|
|
self.goal = goal
|
2019-06-27 13:53:05 +00:00
|
|
|
self.offset = offset
|
2018-01-29 22:17:58 +00:00
|
|
|
self.only_direct = only_direct
|
2016-02-21 11:18:18 +00:00
|
|
|
self.schiene = schiene.Schiene()
|
|
|
|
self.connections = [{}]
|
|
|
|
|
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.start,
|
|
|
|
self.goal,
|
|
|
|
dt_util.as_local(dt_util.utcnow() + self.offset),
|
|
|
|
self.only_direct,
|
|
|
|
)
|
2016-06-30 00:44:35 +00:00
|
|
|
|
2018-01-29 22:49:38 +00:00
|
|
|
if not self.connections:
|
|
|
|
self.connections = [{}]
|
|
|
|
|
2016-02-21 11:18:18 +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.
|
2019-07-31 19:25:30 +00:00
|
|
|
if "details" in con:
|
|
|
|
con.pop("details")
|
|
|
|
delay = con.get("delay", {"delay_departure": 0, "delay_arrival": 0})
|
|
|
|
con["delay"] = delay["delay_departure"]
|
|
|
|
con["delay_arrival"] = delay["delay_arrival"]
|
|
|
|
con["ontime"] = con.get("ontime", False)
|