3 from __future__
import annotations
6 from typing
import cast
16 from .const
import CONF_AGENCY, CONF_ROUTE, DOMAIN
17 from .coordinator
import NextBusDataUpdateCoordinator
18 from .util
import maybe_first
20 _LOGGER = logging.getLogger(__name__)
26 async_add_entities: AddEntitiesCallback,
28 """Load values from configuration and initialize the platform."""
29 _LOGGER.debug(config.data)
30 entry_agency = config.data[CONF_AGENCY]
31 entry_stop = config.data[CONF_STOP]
32 coordinator_key = f
"{entry_agency}-{entry_stop}"
34 coordinator: NextBusDataUpdateCoordinator = hass.data[DOMAIN].
get(coordinator_key)
40 cast(str, config.unique_id),
41 config.data[CONF_AGENCY],
42 config.data[CONF_ROUTE],
43 config.data[CONF_STOP],
44 config.data.get(CONF_NAME)
or config.title,
51 CoordinatorEntity[NextBusDataUpdateCoordinator], SensorEntity
53 """Sensor class that displays upcoming NextBus times.
55 To function, this requires knowing the agency tag as well as the tags for
56 both the route and the stop.
58 This is possibly a little convoluted to provide as it requires making a
59 request to the service to get these values. Perhaps it can be simplified in
60 the future using fuzzy logic and matching.
63 _attr_device_class = SensorDeviceClass.TIMESTAMP
64 _attr_translation_key =
"nextbus"
68 coordinator: NextBusDataUpdateCoordinator,
75 """Initialize sensor with all required config."""
80 self._attr_extra_state_attributes: dict[str, str] = {
89 """Log debug message with prefix."""
90 msg = f
"{self.agency}:{self.route}:{self.stop}:{message}"
91 _LOGGER.debug(msg, *args)
94 """Log error message with prefix."""
95 msg = f
"{self.agency}:{self.route}:{self.stop}:{message}"
96 _LOGGER.error(msg, *args)
99 """Read data from coordinator after adding to hass."""
105 """Update sensor with new departures times."""
108 self.
_log_debug_log_debug(
"Predictions results: %s", results)
111 self.
_log_err_log_err(
"Error getting predictions: %s",
str(results))
113 self._attr_extra_state_attributes.pop(
"upcoming",
None)
117 self._attr_extra_state_attributes.
update(
119 "route":
str(results[
"route"][
"title"]),
120 "stop":
str(results[
"stop"][
"name"]),
125 predictions = results[
"values"]
129 self.
_log_debug_log_debug(
"No upcoming predictions available")
131 self._attr_extra_state_attributes[
"upcoming"] =
"No upcoming predictions"
134 self._attr_extra_state_attributes[
"upcoming"] =
", ".join(
135 str(p[
"minutes"])
for p
in predictions
140 latest_prediction[
"timestamp"] / 1000
dict[str, Any]|None get_prediction_data(self, str stop_id, str route_id)
None async_added_to_hass(self)
None _handle_coordinator_update(self)
def _log_err(self, message, *args)
None __init__(self, NextBusDataUpdateCoordinator coordinator, str unique_id, str agency, str route, str stop, str name)
def _log_debug(self, message, *args)
None async_write_ha_state(self)
web.Response get(self, web.Request request, str config_key)
IssData update(pyiss.ISS iss)
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Any maybe_first(list[Any]|None maybe_list)