1 """Support for Västtrafik public transport."""
3 from __future__
import annotations
5 from datetime
import datetime, timedelta
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
23 _LOGGER = logging.getLogger(__name__)
25 ATTR_ACCESSIBILITY =
"accessibility"
26 ATTR_DIRECTION =
"direction"
33 CONF_DEPARTURES =
"departures"
35 CONF_HEADING =
"heading"
38 CONF_SECRET =
"secret"
44 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
46 vol.Required(CONF_KEY): cv.string,
47 vol.Required(CONF_SECRET): cv.string,
48 vol.Required(CONF_DEPARTURES): [
50 vol.Required(CONF_FROM): cv.string,
51 vol.Optional(CONF_DELAY, default=DEFAULT_DELAY): cv.positive_int,
52 vol.Optional(CONF_HEADING): cv.string,
53 vol.Optional(CONF_LINES, default=[]): vol.All(
54 cv.ensure_list, [cv.string]
56 vol.Optional(CONF_NAME): cv.string,
66 add_entities: AddEntitiesCallback,
67 discovery_info: DiscoveryInfoType |
None =
None,
69 """Set up the departure sensor."""
70 planner = vasttrafik.JournyPlanner(config.get(CONF_KEY), config.get(CONF_SECRET))
75 departure.get(CONF_NAME),
76 departure.get(CONF_FROM),
77 departure.get(CONF_HEADING),
78 departure.get(CONF_LINES),
79 departure.get(CONF_DELAY),
81 for departure
in config[CONF_DEPARTURES]
88 """Implementation of a Vasttrafik Departure Sensor."""
90 _attr_attribution =
"Data provided by Västtrafik"
91 _attr_icon =
"mdi:train"
93 def __init__(self, planner, name, departure, heading, lines, delay):
94 """Initialize the sensor."""
96 self.
_name_name = name
or departure
99 self.
_lines_lines = lines
if lines
else None
106 """Get the station ID."""
107 if location.isdecimal():
108 station_info = {
"station_name": location,
"station_id": location}
110 station_id = self.
_planner_planner.location_name(location)[0][
"gid"]
111 station_info = {
"station_name": location,
"station_id": station_id}
116 """Return the name of the sensor."""
117 return self.
_name_name
121 """Return the state attributes."""
126 """Return the next departure time."""
129 @Throttle(MIN_TIME_BETWEEN_UPDATES)
131 """Get the departure board."""
135 direction=self.
_heading_heading[
"station_id"]
if self.
_heading_heading
else None,
138 except vasttrafik.Error:
139 _LOGGER.debug(
"Unable to read departure board, updating token")
140 self.
_planner_planner.update_token()
144 "No departures from departure station %s to destination station %s",
152 service_journey = departure.get(
"serviceJourney", {})
153 line = service_journey.get(
"line", {})
155 if departure.get(
"isCancelled"):
157 if not self.
_lines_lines
or line.get(
"shortName")
in self.
_lines_lines:
158 if "estimatedOtherwisePlannedTime" in departure:
160 self.
_state_state = datetime.fromisoformat(
161 departure[
"estimatedOtherwisePlannedTime"]
164 self.
_state_state = departure[
"estimatedOtherwisePlannedTime"]
168 stop_point = departure.get(
"stopPoint", {})
171 ATTR_ACCESSIBILITY:
"wheelChair"
172 if line.get(
"isWheelchairAccessible")
174 ATTR_DIRECTION: service_journey.get(
"direction"),
175 ATTR_LINE: line.get(
"shortName"),
176 ATTR_TRACK: stop_point.get(
"platform"),
177 ATTR_FROM: stop_point.get(
"name"),
178 ATTR_TO: self.
_heading_heading[
"station_name"]
181 ATTR_DELAY: self.
_delay_delay.seconds // 60 % 60,
184 self.
_attributes_attributes = {k: v
for k, v
in params.items()
if v}
def __init__(self, planner, name, departure, heading, lines, delay)
def get_station_id(self, location)
def extra_state_attributes(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
datetime now(HomeAssistant hass)