1 """Support for Irish Rail RTPI information."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from pyirishrail.pyirishrail
import IrishRailRTPI
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
21 ATTR_STATION =
"Station"
22 ATTR_ORIGIN =
"Origin"
23 ATTR_DESTINATION =
"Destination"
24 ATTR_DIRECTION =
"Direction"
25 ATTR_STOPS_AT =
"Stops at"
26 ATTR_DUE_IN =
"Due in"
27 ATTR_DUE_AT =
"Due at"
28 ATTR_EXPECT_AT =
"Expected at"
29 ATTR_NEXT_UP =
"Later Train"
30 ATTR_TRAIN_TYPE =
"Train type"
32 CONF_STATION =
"station"
33 CONF_DESTINATION =
"destination"
34 CONF_DIRECTION =
"direction"
35 CONF_STOPS_AT =
"stops_at"
37 DEFAULT_NAME =
"Next Train"
41 TIME_STR_FORMAT =
"%H:%M"
43 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
45 vol.Required(CONF_STATION): cv.string,
46 vol.Optional(CONF_DIRECTION): cv.string,
47 vol.Optional(CONF_DESTINATION): cv.string,
48 vol.Optional(CONF_STOPS_AT): cv.string,
49 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
57 add_entities: AddEntitiesCallback,
58 discovery_info: DiscoveryInfoType |
None =
None,
60 """Set up the Irish Rail transport sensor."""
62 station = config.get(CONF_STATION)
63 direction = config.get(CONF_DIRECTION)
64 destination = config.get(CONF_DESTINATION)
65 stops_at = config.get(CONF_STOPS_AT)
66 name = config.get(CONF_NAME)
68 irish_rail = IrishRailRTPI()
73 data, station, direction, destination, stops_at, name
81 """Implementation of an irish rail public transport sensor."""
83 _attr_attribution =
"Data provided by Irish Rail"
84 _attr_icon =
"mdi:train"
86 def __init__(self, data, station, direction, destination, stops_at, name):
87 """Initialize the sensor."""
99 """Return the name of the sensor."""
100 return self.
_name_name
104 """Return the state of the sensor."""
109 """Return the state attributes."""
112 if len(self.
_times_times) > 1:
114 f
"{self._times[1][ATTR_ORIGIN]} to "
115 f
"{self._times[1][ATTR_DESTINATION]} in "
116 f
"{self._times[1][ATTR_DUE_IN]}"
120 ATTR_STATION: self.
_station_station,
121 ATTR_ORIGIN: self.
_times_times[0][ATTR_ORIGIN],
122 ATTR_DESTINATION: self.
_times_times[0][ATTR_DESTINATION],
123 ATTR_DUE_IN: self.
_times_times[0][ATTR_DUE_IN],
124 ATTR_DUE_AT: self.
_times_times[0][ATTR_DUE_AT],
125 ATTR_EXPECT_AT: self.
_times_times[0][ATTR_EXPECT_AT],
126 ATTR_DIRECTION: self.
_times_times[0][ATTR_DIRECTION],
127 ATTR_STOPS_AT: self.
_times_times[0][ATTR_STOPS_AT],
128 ATTR_NEXT_UP: next_up,
129 ATTR_TRAIN_TYPE: self.
_times_times[0][ATTR_TRAIN_TYPE],
135 """Return the unit this state is expressed in."""
136 return UnitOfTime.MINUTES
139 """Get the latest data and update the states."""
149 """The Class for handling the data retrieval."""
151 def __init__(self, irish_rail, station, direction, destination, stops_at):
152 """Initialize the data object."""
161 """Get the latest data from irishrail."""
162 trains = self.
_ir_api_ir_api.get_station_by_name(
172 ATTR_STATION: self.
stationstation,
173 ATTR_ORIGIN: train.get(
"origin"),
174 ATTR_DESTINATION: train.get(
"destination"),
175 ATTR_DUE_IN: train.get(
"due_in_mins"),
176 ATTR_DUE_AT: train.get(
"scheduled_arrival_time"),
177 ATTR_EXPECT_AT: train.get(
"expected_departure_time"),
178 ATTR_DIRECTION: train.get(
"direction"),
179 ATTR_STOPS_AT: stops_at,
180 ATTR_TRAIN_TYPE: train.get(
"type"),
182 self.
infoinfo.append(train_data)
184 if not self.
infoinfo:
188 """Generate info for an empty train."""
194 ATTR_STATION: self.
stationstation,
196 ATTR_DESTINATION: dest,
199 ATTR_EXPECT_AT:
"n/a",
200 ATTR_DIRECTION: direction,
201 ATTR_STOPS_AT: stops_at,
def __init__(self, irish_rail, station, direction, destination, stops_at)
def _empty_train_data(self)
def __init__(self, data, station, direction, destination, stops_at, name)
dict[str, Any]|None extra_state_attributes(self)
def native_unit_of_measurement(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)