Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 from typing import cast
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  PERCENTAGE,
18  UnitOfElectricCurrent,
19  UnitOfEnergy,
20  UnitOfLength,
21  UnitOfPower,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import StateType
26 
27 from .const import (
28  CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
29  CHARGER_ADDED_ENERGY_KEY,
30  CHARGER_ADDED_RANGE_KEY,
31  CHARGER_CHARGING_POWER_KEY,
32  CHARGER_CHARGING_SPEED_KEY,
33  CHARGER_COST_KEY,
34  CHARGER_CURRENCY_KEY,
35  CHARGER_CURRENT_MODE_KEY,
36  CHARGER_DATA_KEY,
37  CHARGER_DEPOT_PRICE_KEY,
38  CHARGER_ENERGY_PRICE_KEY,
39  CHARGER_MAX_AVAILABLE_POWER_KEY,
40  CHARGER_MAX_CHARGING_CURRENT_KEY,
41  CHARGER_MAX_ICP_CURRENT_KEY,
42  CHARGER_SERIAL_NUMBER_KEY,
43  CHARGER_STATE_OF_CHARGE_KEY,
44  CHARGER_STATUS_DESCRIPTION_KEY,
45  DOMAIN,
46 )
47 from .coordinator import WallboxCoordinator
48 from .entity import WallboxEntity
49 
50 CHARGER_STATION = "station"
51 UPDATE_INTERVAL = 30
52 
53 _LOGGER = logging.getLogger(__name__)
54 
55 
56 @dataclass(frozen=True)
58  """Describes Wallbox sensor entity."""
59 
60  precision: int | None = None
61 
62 
63 SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
64  CHARGER_CHARGING_POWER_KEY: WallboxSensorEntityDescription(
65  key=CHARGER_CHARGING_POWER_KEY,
66  translation_key=CHARGER_CHARGING_POWER_KEY,
67  precision=2,
68  native_unit_of_measurement=UnitOfPower.KILO_WATT,
69  device_class=SensorDeviceClass.POWER,
70  state_class=SensorStateClass.MEASUREMENT,
71  ),
72  CHARGER_MAX_AVAILABLE_POWER_KEY: WallboxSensorEntityDescription(
73  key=CHARGER_MAX_AVAILABLE_POWER_KEY,
74  translation_key=CHARGER_MAX_AVAILABLE_POWER_KEY,
75  precision=0,
76  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
77  device_class=SensorDeviceClass.CURRENT,
78  state_class=SensorStateClass.MEASUREMENT,
79  ),
80  CHARGER_CHARGING_SPEED_KEY: WallboxSensorEntityDescription(
81  key=CHARGER_CHARGING_SPEED_KEY,
82  translation_key=CHARGER_CHARGING_SPEED_KEY,
83  precision=0,
84  state_class=SensorStateClass.MEASUREMENT,
85  ),
86  CHARGER_ADDED_RANGE_KEY: WallboxSensorEntityDescription(
87  key=CHARGER_ADDED_RANGE_KEY,
88  translation_key=CHARGER_ADDED_RANGE_KEY,
89  precision=0,
90  native_unit_of_measurement=UnitOfLength.KILOMETERS,
91  device_class=SensorDeviceClass.DISTANCE,
92  state_class=SensorStateClass.TOTAL_INCREASING,
93  ),
94  CHARGER_ADDED_ENERGY_KEY: WallboxSensorEntityDescription(
95  key=CHARGER_ADDED_ENERGY_KEY,
96  translation_key=CHARGER_ADDED_ENERGY_KEY,
97  precision=2,
98  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
99  device_class=SensorDeviceClass.ENERGY,
100  state_class=SensorStateClass.TOTAL_INCREASING,
101  ),
102  CHARGER_ADDED_DISCHARGED_ENERGY_KEY: WallboxSensorEntityDescription(
103  key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
104  translation_key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
105  precision=2,
106  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
107  device_class=SensorDeviceClass.ENERGY,
108  state_class=SensorStateClass.TOTAL_INCREASING,
109  ),
110  CHARGER_COST_KEY: WallboxSensorEntityDescription(
111  key=CHARGER_COST_KEY,
112  translation_key=CHARGER_COST_KEY,
113  state_class=SensorStateClass.TOTAL_INCREASING,
114  ),
115  CHARGER_STATE_OF_CHARGE_KEY: WallboxSensorEntityDescription(
116  key=CHARGER_STATE_OF_CHARGE_KEY,
117  translation_key=CHARGER_STATE_OF_CHARGE_KEY,
118  native_unit_of_measurement=PERCENTAGE,
119  device_class=SensorDeviceClass.BATTERY,
120  state_class=SensorStateClass.MEASUREMENT,
121  ),
122  CHARGER_CURRENT_MODE_KEY: WallboxSensorEntityDescription(
123  key=CHARGER_CURRENT_MODE_KEY,
124  translation_key=CHARGER_CURRENT_MODE_KEY,
125  ),
126  CHARGER_DEPOT_PRICE_KEY: WallboxSensorEntityDescription(
127  key=CHARGER_DEPOT_PRICE_KEY,
128  translation_key=CHARGER_DEPOT_PRICE_KEY,
129  precision=2,
130  state_class=SensorStateClass.MEASUREMENT,
131  ),
132  CHARGER_ENERGY_PRICE_KEY: WallboxSensorEntityDescription(
133  key=CHARGER_ENERGY_PRICE_KEY,
134  translation_key=CHARGER_ENERGY_PRICE_KEY,
135  precision=2,
136  state_class=SensorStateClass.MEASUREMENT,
137  ),
138  CHARGER_STATUS_DESCRIPTION_KEY: WallboxSensorEntityDescription(
139  key=CHARGER_STATUS_DESCRIPTION_KEY,
140  translation_key=CHARGER_STATUS_DESCRIPTION_KEY,
141  ),
142  CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxSensorEntityDescription(
143  key=CHARGER_MAX_CHARGING_CURRENT_KEY,
144  translation_key=CHARGER_MAX_CHARGING_CURRENT_KEY,
145  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
146  device_class=SensorDeviceClass.CURRENT,
147  state_class=SensorStateClass.MEASUREMENT,
148  ),
149  CHARGER_MAX_ICP_CURRENT_KEY: WallboxSensorEntityDescription(
150  key=CHARGER_MAX_ICP_CURRENT_KEY,
151  translation_key=CHARGER_MAX_ICP_CURRENT_KEY,
152  native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
153  device_class=SensorDeviceClass.CURRENT,
154  state_class=SensorStateClass.MEASUREMENT,
155  ),
156 }
157 
158 
160  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
161 ) -> None:
162  """Create wallbox sensor entities in HASS."""
163  coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
164 
166  WallboxSensor(coordinator, description)
167  for ent in coordinator.data
168  if (description := SENSOR_TYPES.get(ent))
169  )
170 
171 
173  """Representation of the Wallbox portal."""
174 
175  entity_description: WallboxSensorEntityDescription
176 
177  def __init__(
178  self,
179  coordinator: WallboxCoordinator,
180  description: WallboxSensorEntityDescription,
181  ) -> None:
182  """Initialize a Wallbox sensor."""
183  super().__init__(coordinator)
184  self.entity_descriptionentity_description = description
185  self._attr_unique_id_attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
186 
187  @property
188  def native_value(self) -> StateType:
189  """Return the state of the sensor. Round the value when it, and the precision property are not None."""
190  if (
191  sensor_round := self.entity_descriptionentity_description.precision
192  ) is not None and self.coordinator.data[
193  self.entity_descriptionentity_description.key
194  ] is not None:
195  return cast(
196  StateType,
197  round(self.coordinator.data[self.entity_descriptionentity_description.key], sensor_round),
198  )
199  return cast(StateType, self.coordinator.data[self.entity_descriptionentity_description.key])
200 
201  @property
202  def native_unit_of_measurement(self) -> str | None:
203  """Return the unit of measurement of the sensor. When monetary, get the value from the api."""
204  if self.entity_descriptionentity_description.key in (
205  CHARGER_ENERGY_PRICE_KEY,
206  CHARGER_DEPOT_PRICE_KEY,
207  ):
208  return cast(str, self.coordinator.data[CHARGER_CURRENCY_KEY])
209  return cast(str, self.entity_descriptionentity_description.native_unit_of_measurement)
None __init__(self, WallboxCoordinator coordinator, WallboxSensorEntityDescription description)
Definition: sensor.py:181
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:161