Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Get ride details and liveboard details for NMBS (Belgian railway)."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyrail import iRail
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
12  SensorEntity,
13 )
14 from homeassistant.const import (
15  ATTR_LATITUDE,
16  ATTR_LONGITUDE,
17  CONF_NAME,
18  CONF_SHOW_ON_MAP,
19  UnitOfTime,
20 )
21 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
25 import homeassistant.util.dt as dt_util
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 API_FAILURE = -1
30 
31 DEFAULT_NAME = "NMBS"
32 
33 DEFAULT_ICON = "mdi:train"
34 DEFAULT_ICON_ALERT = "mdi:alert-octagon"
35 
36 CONF_STATION_FROM = "station_from"
37 CONF_STATION_TO = "station_to"
38 CONF_STATION_LIVE = "station_live"
39 CONF_EXCLUDE_VIAS = "exclude_vias"
40 
41 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
42  {
43  vol.Required(CONF_STATION_FROM): cv.string,
44  vol.Required(CONF_STATION_TO): cv.string,
45  vol.Optional(CONF_STATION_LIVE): cv.string,
46  vol.Optional(CONF_EXCLUDE_VIAS, default=False): cv.boolean,
47  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
48  vol.Optional(CONF_SHOW_ON_MAP, default=False): cv.boolean,
49  }
50 )
51 
52 
53 def get_time_until(departure_time=None):
54  """Calculate the time between now and a train's departure time."""
55  if departure_time is None:
56  return 0
57 
58  delta = dt_util.utc_from_timestamp(int(departure_time)) - dt_util.now()
59  return round(delta.total_seconds() / 60)
60 
61 
62 def get_delay_in_minutes(delay=0):
63  """Get the delay in minutes from a delay in seconds."""
64  return round(int(delay) / 60)
65 
66 
67 def get_ride_duration(departure_time, arrival_time, delay=0):
68  """Calculate the total travel time in minutes."""
69  duration = dt_util.utc_from_timestamp(
70  int(arrival_time)
71  ) - dt_util.utc_from_timestamp(int(departure_time))
72  duration_time = int(round(duration.total_seconds() / 60))
73  return duration_time + get_delay_in_minutes(delay)
74 
75 
77  hass: HomeAssistant,
78  config: ConfigType,
79  add_entities: AddEntitiesCallback,
80  discovery_info: DiscoveryInfoType | None = None,
81 ) -> None:
82  """Set up the NMBS sensor with iRail API."""
83 
84  api_client = iRail()
85 
86  name = config[CONF_NAME]
87  show_on_map = config[CONF_SHOW_ON_MAP]
88  station_from = config[CONF_STATION_FROM]
89  station_to = config[CONF_STATION_TO]
90  station_live = config.get(CONF_STATION_LIVE)
91  excl_vias = config[CONF_EXCLUDE_VIAS]
92 
93  sensors: list[SensorEntity] = [
94  NMBSSensor(api_client, name, show_on_map, station_from, station_to, excl_vias)
95  ]
96 
97  if station_live is not None:
98  sensors.append(
99  NMBSLiveBoard(api_client, station_live, station_from, station_to)
100  )
101 
102  add_entities(sensors, True)
103 
104 
106  """Get the next train from a station's liveboard."""
107 
108  _attr_attribution = "https://api.irail.be/"
109 
110  def __init__(self, api_client, live_station, station_from, station_to):
111  """Initialize the sensor for getting liveboard data."""
112  self._station_station = live_station
113  self._api_client_api_client = api_client
114  self._station_from_station_from = station_from
115  self._station_to_station_to = station_to
116  self._attrs_attrs = {}
117  self._state_state = None
118 
119  @property
120  def name(self):
121  """Return the sensor default name."""
122  return f"NMBS Live ({self._station})"
123 
124  @property
125  def unique_id(self):
126  """Return a unique ID."""
127  unique_id = f"{self._station}_{self._station_from}_{self._station_to}"
128 
129  return f"nmbs_live_{unique_id}"
130 
131  @property
132  def icon(self):
133  """Return the default icon or an alert icon if delays."""
134  if self._attrs_attrs and int(self._attrs_attrs["delay"]) > 0:
135  return DEFAULT_ICON_ALERT
136 
137  return DEFAULT_ICON
138 
139  @property
140  def native_value(self):
141  """Return sensor state."""
142  return self._state_state
143 
144  @property
146  """Return the sensor attributes if data is available."""
147  if self._state_state is None or not self._attrs_attrs:
148  return None
149 
150  delay = get_delay_in_minutes(self._attrs_attrs["delay"])
151  departure = get_time_until(self._attrs_attrs["time"])
152 
153  attrs = {
154  "departure": f"In {departure} minutes",
155  "departure_minutes": departure,
156  "extra_train": int(self._attrs_attrs["isExtra"]) > 0,
157  "vehicle_id": self._attrs_attrs["vehicle"],
158  "monitored_station": self._station_station,
159  }
160 
161  if delay > 0:
162  attrs["delay"] = f"{delay} minutes"
163  attrs["delay_minutes"] = delay
164 
165  return attrs
166 
167  def update(self) -> None:
168  """Set the state equal to the next departure."""
169  liveboard = self._api_client_api_client.get_liveboard(self._station_station)
170 
171  if liveboard == API_FAILURE:
172  _LOGGER.warning("API failed in NMBSLiveBoard")
173  return
174 
175  if not (departures := liveboard.get("departures")):
176  _LOGGER.warning("API returned invalid departures: %r", liveboard)
177  return
178 
179  _LOGGER.debug("API returned departures: %r", departures)
180  if departures["number"] == "0":
181  # No trains are scheduled
182  return
183  next_departure = departures["departure"][0]
184 
185  self._attrs_attrs = next_departure
186  self._state_state = (
187  f"Track {next_departure['platform']} - {next_departure['station']}"
188  )
189 
190 
192  """Get the total travel time for a given connection."""
193 
194  _attr_attribution = "https://api.irail.be/"
195  _attr_native_unit_of_measurement = UnitOfTime.MINUTES
196 
197  def __init__(
198  self, api_client, name, show_on_map, station_from, station_to, excl_vias
199  ):
200  """Initialize the NMBS connection sensor."""
201  self._name_name = name
202  self._show_on_map_show_on_map = show_on_map
203  self._api_client_api_client = api_client
204  self._station_from_station_from = station_from
205  self._station_to_station_to = station_to
206  self._excl_vias_excl_vias = excl_vias
207 
208  self._attrs_attrs = {}
209  self._state_state = None
210 
211  @property
212  def name(self):
213  """Return the name of the sensor."""
214  return self._name_name
215 
216  @property
217  def icon(self):
218  """Return the sensor default icon or an alert icon if any delay."""
219  if self._attrs_attrs:
220  delay = get_delay_in_minutes(self._attrs_attrs["departure"]["delay"])
221  if delay > 0:
222  return "mdi:alert-octagon"
223 
224  return "mdi:train"
225 
226  @property
228  """Return sensor attributes if data is available."""
229  if self._state_state is None or not self._attrs_attrs:
230  return None
231 
232  delay = get_delay_in_minutes(self._attrs_attrs["departure"]["delay"])
233  departure = get_time_until(self._attrs_attrs["departure"]["time"])
234  canceled = int(self._attrs_attrs["departure"]["canceled"])
235 
236  attrs = {
237  "destination": self._station_to_station_to,
238  "direction": self._attrs_attrs["departure"]["direction"]["name"],
239  "platform_arriving": self._attrs_attrs["arrival"]["platform"],
240  "platform_departing": self._attrs_attrs["departure"]["platform"],
241  "vehicle_id": self._attrs_attrs["departure"]["vehicle"],
242  }
243 
244  if canceled != 1:
245  attrs["departure"] = f"In {departure} minutes"
246  attrs["departure_minutes"] = departure
247  attrs["canceled"] = False
248  else:
249  attrs["departure"] = None
250  attrs["departure_minutes"] = None
251  attrs["canceled"] = True
252 
253  if self._show_on_map_show_on_map and self.station_coordinatesstation_coordinates:
254  attrs[ATTR_LATITUDE] = self.station_coordinatesstation_coordinates[0]
255  attrs[ATTR_LONGITUDE] = self.station_coordinatesstation_coordinates[1]
256 
257  if self.is_via_connectionis_via_connection and not self._excl_vias_excl_vias:
258  via = self._attrs_attrs["vias"]["via"][0]
259 
260  attrs["via"] = via["station"]
261  attrs["via_arrival_platform"] = via["arrival"]["platform"]
262  attrs["via_transfer_platform"] = via["departure"]["platform"]
263  attrs["via_transfer_time"] = get_delay_in_minutes(
264  via["timebetween"]
265  ) + get_delay_in_minutes(via["departure"]["delay"])
266 
267  if delay > 0:
268  attrs["delay"] = f"{delay} minutes"
269  attrs["delay_minutes"] = delay
270 
271  return attrs
272 
273  @property
274  def native_value(self):
275  """Return the state of the device."""
276  return self._state_state
277 
278  @property
280  """Get the lat, long coordinates for station."""
281  if self._state_state is None or not self._attrs_attrs:
282  return []
283 
284  latitude = float(self._attrs_attrs["departure"]["stationinfo"]["locationY"])
285  longitude = float(self._attrs_attrs["departure"]["stationinfo"]["locationX"])
286  return [latitude, longitude]
287 
288  @property
289  def is_via_connection(self):
290  """Return whether the connection goes through another station."""
291  if not self._attrs_attrs:
292  return False
293 
294  return "vias" in self._attrs_attrs and int(self._attrs_attrs["vias"]["number"]) > 0
295 
296  def update(self) -> None:
297  """Set the state to the duration of a connection."""
298  connections = self._api_client_api_client.get_connections(
299  self._station_from_station_from, self._station_to_station_to
300  )
301 
302  if connections == API_FAILURE:
303  _LOGGER.warning("API failed in NMBSSensor")
304  return
305 
306  if not (connection := connections.get("connection")):
307  _LOGGER.warning("API returned invalid connection: %r", connections)
308  return
309 
310  _LOGGER.debug("API returned connection: %r", connection)
311  if int(connection[0]["departure"]["left"]) > 0:
312  next_connection = connection[1]
313  else:
314  next_connection = connection[0]
315 
316  self._attrs_attrs = next_connection
317 
318  if self._excl_vias_excl_vias and self.is_via_connectionis_via_connection:
319  _LOGGER.debug(
320  "Skipping update of NMBSSensor because this connection is a via"
321  )
322  return
323 
324  duration = get_ride_duration(
325  next_connection["departure"]["time"],
326  next_connection["arrival"]["time"],
327  next_connection["departure"]["delay"],
328  )
329 
330  self._state_state = duration
def __init__(self, api_client, live_station, station_from, station_to)
Definition: sensor.py:110
def __init__(self, api_client, name, show_on_map, station_from, station_to, excl_vias)
Definition: sensor.py:199
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:81
def get_time_until(departure_time=None)
Definition: sensor.py:53
def get_ride_duration(departure_time, arrival_time, delay=0)
Definition: sensor.py:67