1 """Binary sensor platform for hvv_departures."""
3 from __future__
import annotations
6 from datetime
import timedelta
10 from aiohttp
import ClientConnectorError
11 from pygti.exceptions
import InvalidAuth
14 BinarySensorDeviceClass,
23 DataUpdateCoordinator,
27 from .const
import ATTRIBUTION, CONF_STATION, DOMAIN, MANUFACTURER
29 _LOGGER = logging.getLogger(__name__)
33 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
35 """Set up the binary_sensor platform."""
36 hub = hass.data[DOMAIN][entry.entry_id]
37 station_name = entry.data[CONF_STATION][
"name"]
38 station = entry.data[CONF_STATION]
40 def get_elevator_entities_from_station_information(
41 station_name, station_information
43 """Convert station information into a list of elevators."""
46 if station_information
is None:
49 for partial_station
in station_information.get(
"partialStations", []):
50 for elevator
in partial_station.get(
"elevators", []):
51 state = elevator.get(
"state") !=
"READY"
52 available = elevator.get(
"state") !=
"UNKNOWN"
53 label = elevator.get(
"label")
54 description = elevator.get(
"description")
57 name = f
"Elevator {label}"
59 name =
"Unknown elevator"
61 if description
is not None:
62 name += f
" ({description})"
64 lines = elevator.get(
"lines")
66 idx = f
"{station_name}-{label}-{lines}"
71 "available": available,
73 "cabin_width": elevator.get(
"cabinWidth"),
74 "cabin_length": elevator.get(
"cabinLength"),
75 "door_width": elevator.get(
"doorWidth"),
76 "elevator_type": elevator.get(
"elevatorType"),
77 "button_type": elevator.get(
"buttonType"),
78 "cause": elevator.get(
"cause"),
84 async
def async_update_data():
85 """Fetch data from API endpoint.
87 This is the place to pre-process the data to lookup tables
88 so entities can quickly look up their data.
91 payload = {
"station": {
"id": station[
"id"],
"type": station[
"type"]}}
94 async
with asyncio.timeout(10):
95 return get_elevator_entities_from_station_information(
96 station_name, await hub.gti.stationInformation(payload)
98 except InvalidAuth
as err:
99 raise UpdateFailed(f
"Authentication failed: {err}")
from err
100 except ClientConnectorError
as err:
101 raise UpdateFailed(f
"Network not available: {err}")
from err
102 except Exception
as err:
103 raise UpdateFailed(f
"Error occurred while fetching data: {err}")
from err
109 name=
"hvv_departures.binary_sensor",
110 update_method=async_update_data,
116 await coordinator.async_refresh()
120 for (idx, ent)
in coordinator.data.items()
125 """HVVDepartureBinarySensor class."""
127 _attr_attribution = ATTRIBUTION
128 _attr_has_entity_name =
True
129 _attr_device_class = BinarySensorDeviceClass.PROBLEM
131 def __init__(self, coordinator, idx, config_entry):
140 entry_type=DeviceEntryType.SERVICE,
144 config_entry.entry_id,
145 config_entry.data[CONF_STATION][
"id"],
146 config_entry.data[CONF_STATION][
"type"],
149 manufacturer=MANUFACTURER,
150 name=f
"Departures at {config_entry.data[CONF_STATION]['name']}",
155 """Return entity state."""
160 """Return if entity is available."""
168 """Return the state attributes."""
176 for k, v
in self.
coordinatorcoordinator.data[self.
idxidx][
"attributes"].items()
def __init__(self, coordinator, idx, config_entry)
dict[str, Any]|None extra_state_attributes(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)