1 """Support for Transport NSW (AU) to query next leave event."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from TransportNSW
import TransportNSW
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
23 ATTR_STOP_ID =
"stop_id"
27 ATTR_REAL_TIME =
"real_time"
28 ATTR_DESTINATION =
"destination"
30 CONF_STOP_ID =
"stop_id"
32 CONF_DESTINATION =
"destination"
34 DEFAULT_NAME =
"Next Bus"
37 "Lightrail":
"mdi:tram",
41 "Schoolbus":
"mdi:bus",
48 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
50 vol.Required(CONF_STOP_ID): cv.string,
51 vol.Required(CONF_API_KEY): cv.string,
52 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
53 vol.Optional(CONF_ROUTE, default=
""): cv.string,
54 vol.Optional(CONF_DESTINATION, default=
""): cv.string,
62 add_entities: AddEntitiesCallback,
63 discovery_info: DiscoveryInfoType |
None =
None,
65 """Set up the Transport NSW sensor."""
66 stop_id = config[CONF_STOP_ID]
67 api_key = config[CONF_API_KEY]
68 route = config.get(CONF_ROUTE)
69 destination = config.get(CONF_DESTINATION)
70 name = config.get(CONF_NAME)
77 """Implementation of an Transport NSW sensor."""
79 _attr_attribution =
"Data provided by Transport NSW"
80 _attr_device_class = SensorDeviceClass.DURATION
81 _attr_state_class = SensorStateClass.MEASUREMENT
84 """Initialize the sensor."""
93 """Return the name of the sensor."""
94 return self.
_name_name
98 """Return the state of the sensor."""
103 """Return the state attributes."""
104 if self.
_times_times
is not None:
106 ATTR_DUE_IN: self.
_times_times[ATTR_DUE_IN],
107 ATTR_STOP_ID: self.
_stop_id_stop_id,
108 ATTR_ROUTE: self.
_times_times[ATTR_ROUTE],
109 ATTR_DELAY: self.
_times_times[ATTR_DELAY],
110 ATTR_REAL_TIME: self.
_times_times[ATTR_REAL_TIME],
111 ATTR_DESTINATION: self.
_times_times[ATTR_DESTINATION],
112 ATTR_MODE: self.
_times_times[ATTR_MODE],
118 """Return the unit this state is expressed in."""
119 return UnitOfTime.MINUTES
123 """Icon to use in the frontend, if any."""
124 return self.
_icon_icon
127 """Get the latest data from Transport NSW and update the states."""
131 self.
_icon_icon = ICONS[self.
_times_times[ATTR_MODE]]
135 """Replace the API response 'n/a' value with None."""
136 return None if (value
is None or value ==
"n/a")
else value
140 """The Class for handling the data retrieval."""
142 def __init__(self, stop_id, route, destination, api_key):
143 """Initialize the data object."""
149 ATTR_ROUTE: self.
_route_route,
152 ATTR_REAL_TIME:
None,
153 ATTR_DESTINATION:
None,
159 """Get the next leave time."""
160 _data = self.
tnswtnsw.get_departures(
167 ATTR_REAL_TIME:
_get_value(_data[
"real_time"]),
168 ATTR_DESTINATION:
_get_value(_data[
"destination"]),
def __init__(self, stop_id, route, destination, api_key)
def native_unit_of_measurement(self)
dict[str, Any]|None extra_state_attributes(self)
def __init__(self, data, stop_id, name)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)