Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Tankerkoenig binary sensor integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from aiotankerkoenig import PriceInfo, Station, Status
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12 )
13 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .coordinator import TankerkoenigConfigEntry, TankerkoenigDataUpdateCoordinator
18 from .entity import TankerkoenigCoordinatorEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
24  hass: HomeAssistant,
25  entry: TankerkoenigConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the tankerkoenig binary sensors."""
29  coordinator = entry.runtime_data
30 
33  station,
34  coordinator,
35  )
36  for station in coordinator.stations.values()
37  )
38 
39 
41  """Shows if a station is open or closed."""
42 
43  _attr_device_class = BinarySensorDeviceClass.DOOR
44  _attr_translation_key = "status"
45 
46  def __init__(
47  self,
48  station: Station,
49  coordinator: TankerkoenigDataUpdateCoordinator,
50  ) -> None:
51  """Initialize the sensor."""
52  super().__init__(coordinator, station)
53  self._station_id_station_id = station.id
54  self._attr_unique_id_attr_unique_id = f"{station.id}_status"
55  if coordinator.show_on_map:
56  self._attr_extra_state_attributes_attr_extra_state_attributes = {
57  ATTR_LATITUDE: station.lat,
58  ATTR_LONGITUDE: station.lng,
59  }
60 
61  @property
62  def is_on(self) -> bool | None:
63  """Return true if the station is open."""
64  data: PriceInfo = self.coordinator.data[self._station_id_station_id]
65  return data is not None and data.status == Status.OPEN
None __init__(self, Station station, TankerkoenigDataUpdateCoordinator coordinator)
None async_setup_entry(HomeAssistant hass, TankerkoenigConfigEntry entry, AddEntitiesCallback async_add_entities)