Add nmbs canceled attribute (#57113)

* nmbs: Add canceled attribute

If a train is canceled, change the state to canceled and also add an attribute that can be matched.
Personnaly I look for the attribute and add a "line-through" CSS style to show my train was canceled.

I discovered this was not displayed the hard way :)

Signed-off-by: Tom Barbette <t.barbette@gmail.com>

* Update homeassistant/components/nmbs/sensor.py

canceled must be compared as an int, as suggested by @MartinHjelmare

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Tom Barbette 2022-05-26 18:04:22 +02:00 committed by GitHub
parent 576fc9dc64
commit 33784446f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -215,10 +215,9 @@ class NMBSSensor(SensorEntity):
delay = get_delay_in_minutes(self._attrs["departure"]["delay"])
departure = get_time_until(self._attrs["departure"]["time"])
canceled = int(self._attrs["departure"]["canceled"])
attrs = {
"departure": f"In {departure} minutes",
"departure_minutes": departure,
"destination": self._station_to,
"direction": self._attrs["departure"]["direction"]["name"],
"platform_arriving": self._attrs["arrival"]["platform"],
@ -227,6 +226,15 @@ class NMBSSensor(SensorEntity):
ATTR_ATTRIBUTION: "https://api.irail.be/",
}
if canceled != 1:
attrs["departure"] = f"In {departure} minutes"
attrs["departure_minutes"] = departure
attrs["canceled"] = False
else:
attrs["departure"] = None
attrs["departure_minutes"] = None
attrs["canceled"] = True
if self._show_on_map and self.station_coordinates:
attrs[ATTR_LATITUDE] = self.station_coordinates[0]
attrs[ATTR_LONGITUDE] = self.station_coordinates[1]