Add support for bidirectional chargers to Wallbox integration (#74313)
* Add support for the Quasar bidirectional charger to the Wallbox integration, including ability to control charger while discharging, set a negative charge rate and monitor discharged amount * Make code more generic in order to support other bidirectional models in the future * Updates to files to comply with HA formatting rules * Change const file to fix black check failure * Remove unnecessay loop in number entitypull/74504/head
parent
2cc9db5468
commit
5b32eea3d0
|
@ -3,7 +3,10 @@ from homeassistant.backports.enum import StrEnum
|
||||||
|
|
||||||
DOMAIN = "wallbox"
|
DOMAIN = "wallbox"
|
||||||
|
|
||||||
|
BIDIRECTIONAL_MODEL_PREFIXES = ["QSX"]
|
||||||
|
|
||||||
CONF_STATION = "station"
|
CONF_STATION = "station"
|
||||||
|
CHARGER_ADDED_DISCHARGED_ENERGY_KEY = "added_discharged_energy"
|
||||||
CHARGER_ADDED_ENERGY_KEY = "added_energy"
|
CHARGER_ADDED_ENERGY_KEY = "added_energy"
|
||||||
CHARGER_ADDED_RANGE_KEY = "added_range"
|
CHARGER_ADDED_RANGE_KEY = "added_range"
|
||||||
CHARGER_CHARGING_POWER_KEY = "charging_power"
|
CHARGER_CHARGING_POWER_KEY = "charging_power"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"""Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance."""
|
"""Home Assistant component for accessing the Wallbox Portal API. The number component allows control of charging current."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
@ -11,9 +11,11 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import InvalidAuth, WallboxCoordinator, WallboxEntity
|
from . import InvalidAuth, WallboxCoordinator, WallboxEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
|
BIDIRECTIONAL_MODEL_PREFIXES,
|
||||||
CHARGER_DATA_KEY,
|
CHARGER_DATA_KEY,
|
||||||
CHARGER_MAX_AVAILABLE_POWER_KEY,
|
CHARGER_MAX_AVAILABLE_POWER_KEY,
|
||||||
CHARGER_MAX_CHARGING_CURRENT_KEY,
|
CHARGER_MAX_CHARGING_CURRENT_KEY,
|
||||||
|
CHARGER_PART_NUMBER_KEY,
|
||||||
CHARGER_SERIAL_NUMBER_KEY,
|
CHARGER_SERIAL_NUMBER_KEY,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
@ -21,14 +23,13 @@ from .const import (
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class WallboxNumberEntityDescription(NumberEntityDescription):
|
class WallboxNumberEntityDescription(NumberEntityDescription):
|
||||||
"""Describes Wallbox sensor entity."""
|
"""Describes Wallbox number entity."""
|
||||||
|
|
||||||
|
|
||||||
NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
||||||
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
|
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
|
||||||
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
|
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
|
||||||
name="Max. Charging Current",
|
name="Max. Charging Current",
|
||||||
native_min_value=6,
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create wallbox sensor entities in HASS."""
|
"""Create wallbox number entities in HASS."""
|
||||||
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
# Check if the user is authorized to change current, if so, add number component:
|
# Check if the user is authorized to change current, if so, add number component:
|
||||||
try:
|
try:
|
||||||
|
@ -66,21 +67,30 @@ class WallboxNumber(WallboxEntity, NumberEntity):
|
||||||
entry: ConfigEntry,
|
entry: ConfigEntry,
|
||||||
description: WallboxNumberEntityDescription,
|
description: WallboxNumberEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Wallbox sensor."""
|
"""Initialize a Wallbox number entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._coordinator = coordinator
|
self._coordinator = coordinator
|
||||||
self._attr_name = f"{entry.title} {description.name}"
|
self._attr_name = f"{entry.title} {description.name}"
|
||||||
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
|
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
|
||||||
|
self._is_bidirectional = (
|
||||||
|
coordinator.data[CHARGER_DATA_KEY][CHARGER_PART_NUMBER_KEY][0:3]
|
||||||
|
in BIDIRECTIONAL_MODEL_PREFIXES
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_max_value(self) -> float:
|
def native_max_value(self) -> float:
|
||||||
"""Return the maximum available current."""
|
"""Return the maximum available current."""
|
||||||
return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY])
|
return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_min_value(self) -> float:
|
||||||
|
"""Return the minimum available current based on charger type - some chargers can discharge."""
|
||||||
|
return (self.max_value * -1) if self._is_bidirectional else 6
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> float | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the value of the entity."""
|
||||||
return cast(
|
return cast(
|
||||||
Optional[float], self._coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
|
Optional[float], self._coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
|
||||||
)
|
)
|
||||||
|
|
|
@ -25,6 +25,7 @@ from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from . import WallboxCoordinator, WallboxEntity
|
from . import WallboxCoordinator, WallboxEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
|
CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
|
||||||
CHARGER_ADDED_ENERGY_KEY,
|
CHARGER_ADDED_ENERGY_KEY,
|
||||||
CHARGER_ADDED_RANGE_KEY,
|
CHARGER_ADDED_RANGE_KEY,
|
||||||
CHARGER_CHARGING_POWER_KEY,
|
CHARGER_CHARGING_POWER_KEY,
|
||||||
|
@ -94,6 +95,14 @@ SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
|
||||||
device_class=SensorDeviceClass.ENERGY,
|
device_class=SensorDeviceClass.ENERGY,
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
|
CHARGER_ADDED_DISCHARGED_ENERGY_KEY: WallboxSensorEntityDescription(
|
||||||
|
key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
|
||||||
|
name="Discharged Energy",
|
||||||
|
precision=2,
|
||||||
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
|
device_class=SensorDeviceClass.ENERGY,
|
||||||
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
),
|
||||||
CHARGER_COST_KEY: WallboxSensorEntityDescription(
|
CHARGER_COST_KEY: WallboxSensorEntityDescription(
|
||||||
key=CHARGER_COST_KEY,
|
key=CHARGER_COST_KEY,
|
||||||
icon="mdi:ev-station",
|
icon="mdi:ev-station",
|
||||||
|
|
Loading…
Reference in New Issue