2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Västtrafik public transport."""
|
2016-10-05 05:37:45 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2017-08-06 08:03:57 +00:00
|
|
|
|
2016-10-05 05:37:45 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-08-06 08:03:57 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import CONF_NAME, ATTR_ATTRIBUTION
|
2016-10-05 05:37:45 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
2019-05-19 09:23:55 +00:00
|
|
|
from homeassistant.util.dt import now
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ACCESSIBILITY = "accessibility"
|
|
|
|
ATTR_DIRECTION = "direction"
|
|
|
|
ATTR_LINE = "line"
|
|
|
|
ATTR_TRACK = "track"
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by Västtrafik"
|
2017-08-06 08:03:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DELAY = "delay"
|
|
|
|
CONF_DEPARTURES = "departures"
|
|
|
|
CONF_FROM = "from"
|
|
|
|
CONF_HEADING = "heading"
|
|
|
|
CONF_LINES = "lines"
|
|
|
|
CONF_KEY = "key"
|
|
|
|
CONF_SECRET = "secret"
|
2016-10-05 05:37:45 +00:00
|
|
|
|
2017-08-06 08:03:57 +00:00
|
|
|
DEFAULT_DELAY = 0
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:train"
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_KEY): cv.string,
|
|
|
|
vol.Required(CONF_SECRET): cv.string,
|
|
|
|
vol.Optional(CONF_DEPARTURES): [
|
|
|
|
{
|
|
|
|
vol.Required(CONF_FROM): cv.string,
|
|
|
|
vol.Optional(CONF_DELAY, default=DEFAULT_DELAY): cv.positive_int,
|
|
|
|
vol.Optional(CONF_HEADING): cv.string,
|
|
|
|
vol.Optional(CONF_LINES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2016-10-05 05:37:45 +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 departure sensor."""
|
2017-02-12 19:57:29 +00:00
|
|
|
import vasttrafik
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
planner = vasttrafik.JournyPlanner(config.get(CONF_KEY), config.get(CONF_SECRET))
|
2016-10-05 05:37:45 +00:00
|
|
|
sensors = []
|
2019-01-06 20:06:20 +00:00
|
|
|
|
2016-10-05 05:37:45 +00:00
|
|
|
for departure in config.get(CONF_DEPARTURES):
|
|
|
|
sensors.append(
|
|
|
|
VasttrafikDepartureSensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
vasttrafik,
|
|
|
|
planner,
|
|
|
|
departure.get(CONF_NAME),
|
|
|
|
departure.get(CONF_FROM),
|
|
|
|
departure.get(CONF_HEADING),
|
|
|
|
departure.get(CONF_LINES),
|
|
|
|
departure.get(CONF_DELAY),
|
|
|
|
)
|
|
|
|
)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors, True)
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VasttrafikDepartureSensor(Entity):
|
|
|
|
"""Implementation of a Vasttrafik Departure Sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, vasttrafik, planner, name, departure, heading, lines, delay):
|
2016-10-05 05:37:45 +00:00
|
|
|
"""Initialize the sensor."""
|
2017-02-12 19:57:29 +00:00
|
|
|
self._vasttrafik = vasttrafik
|
2016-10-05 05:37:45 +00:00
|
|
|
self._planner = planner
|
|
|
|
self._name = name or departure
|
|
|
|
self._departure = planner.location_name(departure)[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
self._heading = planner.location_name(heading)[0] if heading else None
|
2018-03-21 22:21:51 +00:00
|
|
|
self._lines = lines if lines else None
|
2016-10-05 05:37:45 +00:00
|
|
|
self._delay = timedelta(minutes=delay)
|
|
|
|
self._departureboard = None
|
2019-01-08 08:54:22 +00:00
|
|
|
self._state = None
|
|
|
|
self._attributes = None
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon for the frontend."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-01-08 08:54:22 +00:00
|
|
|
return self._attributes
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the next departure time."""
|
2019-01-08 08:54:22 +00:00
|
|
|
return self._state
|
2016-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Get the departure board."""
|
2017-02-12 19:57:29 +00:00
|
|
|
try:
|
|
|
|
self._departureboard = self._planner.departureboard(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._departure["id"],
|
|
|
|
direction=self._heading["id"] if self._heading else None,
|
|
|
|
date=now() + self._delay,
|
|
|
|
)
|
2017-02-12 19:57:29 +00:00
|
|
|
except self._vasttrafik.Error:
|
2019-01-06 20:06:20 +00:00
|
|
|
_LOGGER.debug("Unable to read departure board, updating token")
|
2017-02-12 19:57:29 +00:00
|
|
|
self._planner.update_token()
|
2019-01-08 08:54:22 +00:00
|
|
|
|
|
|
|
if not self._departureboard:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"No departures from %s heading %s",
|
2019-07-31 19:25:30 +00:00
|
|
|
self._departure["name"],
|
|
|
|
self._heading["name"] if self._heading else "ANY",
|
|
|
|
)
|
2019-01-08 08:54:22 +00:00
|
|
|
self._state = None
|
|
|
|
self._attributes = {}
|
|
|
|
else:
|
|
|
|
for departure in self._departureboard:
|
2019-07-31 19:25:30 +00:00
|
|
|
line = departure.get("sname")
|
2019-01-08 08:54:22 +00:00
|
|
|
if not self._lines or line in self._lines:
|
2019-07-31 19:25:30 +00:00
|
|
|
if "rtTime" in self._departureboard[0]:
|
|
|
|
self._state = self._departureboard[0]["rtTime"]
|
2019-01-08 08:54:22 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self._departureboard[0]["time"]
|
2019-01-08 08:54:22 +00:00
|
|
|
|
|
|
|
params = {
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ACCESSIBILITY: departure.get("accessibility"),
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DIRECTION: departure.get("direction"),
|
|
|
|
ATTR_LINE: departure.get("sname"),
|
|
|
|
ATTR_TRACK: departure.get("track"),
|
2019-01-08 08:54:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._attributes = {k: v for k, v in params.items() if v}
|
2019-01-08 08:54:22 +00:00
|
|
|
break
|