1 """Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
7 from typing
import cast
12 SensorEntityDescription,
18 UnitOfElectricCurrent,
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,
35 CHARGER_CURRENT_MODE_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,
47 from .coordinator
import WallboxCoordinator
48 from .entity
import WallboxEntity
50 CHARGER_STATION =
"station"
53 _LOGGER = logging.getLogger(__name__)
56 @dataclass(frozen=True)
58 """Describes Wallbox sensor entity."""
60 precision: int |
None =
None
63 SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
65 key=CHARGER_CHARGING_POWER_KEY,
66 translation_key=CHARGER_CHARGING_POWER_KEY,
68 native_unit_of_measurement=UnitOfPower.KILO_WATT,
69 device_class=SensorDeviceClass.POWER,
70 state_class=SensorStateClass.MEASUREMENT,
73 key=CHARGER_MAX_AVAILABLE_POWER_KEY,
74 translation_key=CHARGER_MAX_AVAILABLE_POWER_KEY,
76 native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
77 device_class=SensorDeviceClass.CURRENT,
78 state_class=SensorStateClass.MEASUREMENT,
81 key=CHARGER_CHARGING_SPEED_KEY,
82 translation_key=CHARGER_CHARGING_SPEED_KEY,
84 state_class=SensorStateClass.MEASUREMENT,
87 key=CHARGER_ADDED_RANGE_KEY,
88 translation_key=CHARGER_ADDED_RANGE_KEY,
90 native_unit_of_measurement=UnitOfLength.KILOMETERS,
91 device_class=SensorDeviceClass.DISTANCE,
92 state_class=SensorStateClass.TOTAL_INCREASING,
95 key=CHARGER_ADDED_ENERGY_KEY,
96 translation_key=CHARGER_ADDED_ENERGY_KEY,
98 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
99 device_class=SensorDeviceClass.ENERGY,
100 state_class=SensorStateClass.TOTAL_INCREASING,
103 key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
104 translation_key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
106 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
107 device_class=SensorDeviceClass.ENERGY,
108 state_class=SensorStateClass.TOTAL_INCREASING,
111 key=CHARGER_COST_KEY,
112 translation_key=CHARGER_COST_KEY,
113 state_class=SensorStateClass.TOTAL_INCREASING,
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,
123 key=CHARGER_CURRENT_MODE_KEY,
124 translation_key=CHARGER_CURRENT_MODE_KEY,
127 key=CHARGER_DEPOT_PRICE_KEY,
128 translation_key=CHARGER_DEPOT_PRICE_KEY,
130 state_class=SensorStateClass.MEASUREMENT,
133 key=CHARGER_ENERGY_PRICE_KEY,
134 translation_key=CHARGER_ENERGY_PRICE_KEY,
136 state_class=SensorStateClass.MEASUREMENT,
139 key=CHARGER_STATUS_DESCRIPTION_KEY,
140 translation_key=CHARGER_STATUS_DESCRIPTION_KEY,
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,
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,
160 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
162 """Create wallbox sensor entities in HASS."""
163 coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
167 for ent
in coordinator.data
168 if (description := SENSOR_TYPES.get(ent))
173 """Representation of the Wallbox portal."""
175 entity_description: WallboxSensorEntityDescription
179 coordinator: WallboxCoordinator,
180 description: WallboxSensorEntityDescription,
182 """Initialize a Wallbox sensor."""
185 self.
_attr_unique_id_attr_unique_id = f
"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
189 """Return the state of the sensor. Round the value when it, and the precision property are not None."""
192 )
is not None and self.coordinator.data[
197 round(self.coordinator.data[self.
entity_descriptionentity_description.key], sensor_round),
199 return cast(StateType, self.coordinator.data[self.
entity_descriptionentity_description.key])
203 """Return the unit of measurement of the sensor. When monetary, get the value from the api."""
205 CHARGER_ENERGY_PRICE_KEY,
206 CHARGER_DEPOT_PRICE_KEY,
208 return cast(str, self.coordinator.data[CHARGER_CURRENCY_KEY])
209 return cast(str, self.
entity_descriptionentity_description.native_unit_of_measurement)
str|None native_unit_of_measurement(self)
None __init__(self, WallboxCoordinator coordinator, WallboxSensorEntityDescription description)
StateType native_value(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)