Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for KEBA charging station binary sensors."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
14 
15 from . import DOMAIN, KebaHandler
16 
17 
19  hass: HomeAssistant,
20  config: ConfigType,
21  async_add_entities: AddEntitiesCallback,
22  discovery_info: DiscoveryInfoType | None = None,
23 ) -> None:
24  """Set up the KEBA charging station platform."""
25  if discovery_info is None:
26  return
27 
28  keba: KebaHandler = hass.data[DOMAIN]
29 
30  sensors = [
32  keba,
33  "Online",
34  "Status",
35  "device_state",
36  BinarySensorDeviceClass.CONNECTIVITY,
37  ),
39  keba,
40  "Plug",
41  "Plug",
42  "plug_state",
43  BinarySensorDeviceClass.PLUG,
44  ),
46  keba,
47  "State",
48  "Charging State",
49  "charging_state",
50  BinarySensorDeviceClass.POWER,
51  ),
53  keba,
54  "Tmo FS",
55  "Failsafe Mode",
56  "failsafe_mode_state",
57  BinarySensorDeviceClass.SAFETY,
58  ),
59  ]
60  async_add_entities(sensors)
61 
62 
64  """Representation of a binary sensor of a KEBA charging station."""
65 
66  _attr_should_poll = False
67 
68  def __init__(
69  self,
70  keba: KebaHandler,
71  key: str,
72  name: str,
73  entity_type: str,
74  device_class: BinarySensorDeviceClass,
75  ) -> None:
76  """Initialize the KEBA Sensor."""
77  self._key_key = key
78  self._keba_keba = keba
79  self._attributes: dict[str, Any] = {}
80 
81  self._attr_device_class_attr_device_class = device_class
82  self._attr_name_attr_name = f"{keba.device_name} {name}"
83  self._attr_unique_id_attr_unique_id = f"{keba.device_id}_{entity_type}"
84 
85  @property
86  def extra_state_attributes(self) -> dict[str, Any]:
87  """Return the state attributes of the binary sensor."""
88  return self._attributes
89 
90  async def async_update(self) -> None:
91  """Get latest cached states from the device."""
92  if self._key_key == "Online":
93  self._attr_is_on_attr_is_on = self._keba_keba.get_value(self._key_key)
94 
95  elif self._key_key == "Plug":
96  self._attr_is_on_attr_is_on = self._keba_keba.get_value("Plug_plugged")
97  self._attributes["plugged_on_wallbox"] = self._keba_keba.get_value(
98  "Plug_wallbox"
99  )
100  self._attributes["plug_locked"] = self._keba_keba.get_value("Plug_locked")
101  self._attributes["plugged_on_EV"] = self._keba_keba.get_value("Plug_EV")
102 
103  elif self._key_key == "State":
104  self._attr_is_on_attr_is_on = self._keba_keba.get_value("State_on")
105  self._attributes["status"] = self._keba_keba.get_value("State_details")
106  self._attributes["max_charging_rate"] = str(
107  self._keba_keba.get_value("Max curr")
108  )
109 
110  elif self._key_key == "Tmo FS":
111  self._attr_is_on_attr_is_on = not self._keba_keba.get_value("FS_on")
112  self._attributes["failsafe_timeout"] = str(self._keba_keba.get_value("Tmo FS"))
113  self._attributes["fallback_current"] = str(self._keba_keba.get_value("Curr FS"))
114  elif self._key_key == "Authreq":
115  self._attr_is_on_attr_is_on = self._keba_keba.get_value(self._key_key) == 0
116 
117  def update_callback(self) -> None:
118  """Schedule a state update."""
119  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
120 
121  async def async_added_to_hass(self) -> None:
122  """Add update callback after being added to hass."""
123  self._keba_keba.add_update_listener(self.update_callbackupdate_callback)
None __init__(self, KebaHandler keba, str key, str name, str entity_type, BinarySensorDeviceClass device_class)
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
float|int|str|None get_value(Sensor sensor, str field)
Definition: sensor.py:46