Add multiple departures to Trafikverket Ferry (#71484)

This commit is contained in:
G Johansson 2022-05-14 02:42:11 +02:00 committed by GitHub
parent 0bc843c133
commit d84c6af55d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 11 deletions

View File

@ -77,8 +77,10 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator):
when = current_time
try:
routedata: FerryStop = await self._ferry_api.async_get_next_ferry_stop(
self._from, self._to, when
routedata: list[
FerryStop
] = await self._ferry_api.async_get_next_ferry_stops(
self._from, self._to, when, 3
)
except ValueError as error:
raise UpdateFailed(
@ -86,11 +88,13 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator):
) from error
states = {
"departure_time": routedata.departure_time,
"departure_from": routedata.from_harbor_name,
"departure_to": routedata.to_harbor_name,
"departure_modified": routedata.modified_time,
"departure_information": routedata.other_information,
"departure_time": routedata[0].departure_time,
"departure_from": routedata[0].from_harbor_name,
"departure_to": routedata[0].to_harbor_name,
"departure_modified": routedata[0].modified_time,
"departure_information": routedata[0].other_information,
"departure_time_next": routedata[1].departure_time,
"departure_time_next_next": routedata[2].departure_time,
}
_LOGGER.debug("States: %s", states)
return states

View File

@ -38,7 +38,7 @@ class TrafikverketRequiredKeysMixin:
"""Mixin for required keys."""
value_fn: Callable[[dict[str, Any]], StateType | datetime]
info_fn: Callable[[dict[str, Any]], StateType | list]
info_fn: Callable[[dict[str, Any]], StateType | list] | None
@dataclass
@ -80,6 +80,24 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
info_fn=lambda data: data["departure_information"],
entity_registry_enabled_default=False,
),
TrafikverketSensorEntityDescription(
key="departure_time_next",
name="Departure Time Next",
icon="mdi:clock",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda data: as_utc(data["departure_time_next"]),
info_fn=None,
entity_registry_enabled_default=False,
),
TrafikverketSensorEntityDescription(
key="departure_time_next_next",
name="Departure Time Next After",
icon="mdi:clock",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda data: as_utc(data["departure_time_next_next"]),
info_fn=None,
entity_registry_enabled_default=False,
),
)
@ -132,9 +150,12 @@ class FerrySensor(CoordinatorEntity[TVDataUpdateCoordinator], SensorEntity):
self.coordinator.data
)
self._attr_extra_state_attributes = {
"other_information": self.entity_description.info_fn(self.coordinator.data),
}
if self.entity_description.info_fn:
self._attr_extra_state_attributes = {
"other_information": self.entity_description.info_fn(
self.coordinator.data
),
}
@callback
def _handle_coordinator_update(self) -> None: