Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for KEBA charging station sensors."""
2 
3 from __future__ import annotations
4 
6  SensorDeviceClass,
7  SensorEntity,
8  SensorEntityDescription,
9  SensorStateClass,
10 )
11 from homeassistant.const import UnitOfElectricCurrent, UnitOfEnergy, UnitOfPower
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
15 
16 from . import DOMAIN, KebaHandler
17 
18 
20  hass: HomeAssistant,
21  config: ConfigType,
22  async_add_entities: AddEntitiesCallback,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> None:
25  """Set up the KEBA charging station platform."""
26  if discovery_info is None:
27  return
28 
29  keba = hass.data[DOMAIN]
30 
31  sensors = [
32  KebaSensor(
33  keba,
34  "max_current",
36  key="Curr user",
37  name="Max Current",
38  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
39  device_class=SensorDeviceClass.CURRENT,
40  ),
41  ),
42  KebaSensor(
43  keba,
44  "energy_target",
46  key="Setenergy",
47  name="Energy Target",
48  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
49  device_class=SensorDeviceClass.ENERGY,
50  ),
51  ),
52  KebaSensor(
53  keba,
54  "charging_power",
56  key="P",
57  name="Charging Power",
58  native_unit_of_measurement=UnitOfPower.KILO_WATT,
59  device_class=SensorDeviceClass.POWER,
60  state_class=SensorStateClass.MEASUREMENT,
61  ),
62  ),
63  KebaSensor(
64  keba,
65  "session_energy",
67  key="E pres", # codespell:ignore pres
68  name="Session Energy",
69  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
70  device_class=SensorDeviceClass.ENERGY,
71  ),
72  ),
73  KebaSensor(
74  keba,
75  "total_energy",
77  key="E total",
78  name="Total Energy",
79  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
80  device_class=SensorDeviceClass.ENERGY,
81  state_class=SensorStateClass.TOTAL_INCREASING,
82  ),
83  ),
84  ]
85  async_add_entities(sensors)
86 
87 
89  """The entity class for KEBA charging stations sensors."""
90 
91  _attr_should_poll = False
92 
93  def __init__(
94  self,
95  keba: KebaHandler,
96  entity_type: str,
97  description: SensorEntityDescription,
98  ) -> None:
99  """Initialize the KEBA Sensor."""
100  self._keba_keba = keba
101  self.entity_descriptionentity_description = description
102 
103  self._attr_name_attr_name = f"{keba.device_name} {description.name}"
104  self._attr_unique_id_attr_unique_id = f"{keba.device_id}_{entity_type}"
105 
106  self._attributes: dict[str, str] = {}
107 
108  @property
109  def extra_state_attributes(self) -> dict[str, str]:
110  """Return the state attributes of the binary sensor."""
111  return self._attributes
112 
113  async def async_update(self) -> None:
114  """Get latest cached states from the device."""
115  self._attr_native_value_attr_native_value = self._keba_keba.get_value(self.entity_descriptionentity_description.key)
116 
117  if self.entity_descriptionentity_description.key == "P":
118  self._attributes["power_factor"] = self._keba_keba.get_value("PF")
119  self._attributes["voltage_u1"] = str(self._keba_keba.get_value("U1"))
120  self._attributes["voltage_u2"] = str(self._keba_keba.get_value("U2"))
121  self._attributes["voltage_u3"] = str(self._keba_keba.get_value("U3"))
122  self._attributes["current_i1"] = str(self._keba_keba.get_value("I1"))
123  self._attributes["current_i2"] = str(self._keba_keba.get_value("I2"))
124  self._attributes["current_i3"] = str(self._keba_keba.get_value("I3"))
125  elif self.entity_descriptionentity_description.key == "Curr user":
126  self._attributes["max_current_hardware"] = self._keba_keba.get_value("Curr HW")
127 
128  def update_callback(self) -> None:
129  """Schedule a state update."""
130  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
131 
132  async def async_added_to_hass(self) -> None:
133  """Add update callback after being added to hass."""
134  self._keba_keba.add_update_listener(self.update_callbackupdate_callback)
None __init__(self, KebaHandler keba, str entity_type, SensorEntityDescription description)
Definition: sensor.py:98
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)
Definition: sensor.py:24
float|int|str|None get_value(Sensor sensor, str field)
Definition: sensor.py:46