2022-07-09 18:41:39 +00:00
|
|
|
"""Home Assistant component for accessing the Wallbox Portal API. The number component allows control of charging current."""
|
2021-11-02 10:11:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2021-11-23 21:30:22 +00:00
|
|
|
from typing import Optional, cast
|
2021-11-02 10:11:46 +00:00
|
|
|
|
|
|
|
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
2021-11-23 21:30:22 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-10-14 11:09:00 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2021-11-23 21:30:22 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-10-27 17:53:14 +00:00
|
|
|
|
2022-01-04 14:46:57 +00:00
|
|
|
from . import InvalidAuth, WallboxCoordinator, WallboxEntity
|
|
|
|
from .const import (
|
2022-07-09 18:41:39 +00:00
|
|
|
BIDIRECTIONAL_MODEL_PREFIXES,
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_DATA_KEY,
|
|
|
|
CHARGER_MAX_AVAILABLE_POWER_KEY,
|
|
|
|
CHARGER_MAX_CHARGING_CURRENT_KEY,
|
2022-07-09 18:41:39 +00:00
|
|
|
CHARGER_PART_NUMBER_KEY,
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_SERIAL_NUMBER_KEY,
|
2022-01-04 14:46:57 +00:00
|
|
|
DOMAIN,
|
|
|
|
)
|
2021-10-27 17:53:14 +00:00
|
|
|
|
|
|
|
|
2021-11-02 10:11:46 +00:00
|
|
|
@dataclass
|
|
|
|
class WallboxNumberEntityDescription(NumberEntityDescription):
|
2022-07-09 18:41:39 +00:00
|
|
|
"""Describes Wallbox number entity."""
|
2021-11-02 10:11:46 +00:00
|
|
|
|
|
|
|
|
2021-11-07 14:57:34 +00:00
|
|
|
NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
2022-04-19 06:44:25 +00:00
|
|
|
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
|
|
|
|
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
|
2021-11-02 10:11:46 +00:00
|
|
|
name="Max. Charging Current",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-23 21:30:22 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2022-07-09 18:41:39 +00:00
|
|
|
"""Create wallbox number entities in HASS."""
|
2021-11-23 21:30:22 +00:00
|
|
|
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-10-27 17:53:14 +00:00
|
|
|
# Check if the user is authorized to change current, if so, add number component:
|
|
|
|
try:
|
|
|
|
await coordinator.async_set_charging_current(
|
2022-04-19 06:44:25 +00:00
|
|
|
coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
|
2021-10-27 17:53:14 +00:00
|
|
|
)
|
|
|
|
except InvalidAuth:
|
2021-11-02 10:11:46 +00:00
|
|
|
return
|
2022-10-14 11:09:00 +00:00
|
|
|
except ConnectionError as exc:
|
|
|
|
raise PlatformNotReady from exc
|
2021-11-02 10:11:46 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
2021-11-23 21:30:22 +00:00
|
|
|
WallboxNumber(coordinator, entry, description)
|
2021-11-02 10:11:46 +00:00
|
|
|
for ent in coordinator.data
|
|
|
|
if (description := NUMBER_TYPES.get(ent))
|
|
|
|
]
|
|
|
|
)
|
2021-10-27 17:53:14 +00:00
|
|
|
|
|
|
|
|
2022-01-04 14:46:57 +00:00
|
|
|
class WallboxNumber(WallboxEntity, NumberEntity):
|
2021-10-27 17:53:14 +00:00
|
|
|
"""Representation of the Wallbox portal."""
|
|
|
|
|
2021-11-07 14:57:34 +00:00
|
|
|
entity_description: WallboxNumberEntityDescription
|
|
|
|
|
2021-11-02 10:11:46 +00:00
|
|
|
def __init__(
|
2021-11-23 21:30:22 +00:00
|
|
|
self,
|
|
|
|
coordinator: WallboxCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
description: WallboxNumberEntityDescription,
|
|
|
|
) -> None:
|
2022-07-09 18:41:39 +00:00
|
|
|
"""Initialize a Wallbox number entity."""
|
2021-10-27 17:53:14 +00:00
|
|
|
super().__init__(coordinator)
|
2021-11-02 10:11:46 +00:00
|
|
|
self.entity_description = description
|
2021-10-27 17:53:14 +00:00
|
|
|
self._coordinator = coordinator
|
2021-11-23 21:30:22 +00:00
|
|
|
self._attr_name = f"{entry.title} {description.name}"
|
2022-04-19 06:44:25 +00:00
|
|
|
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
|
2022-07-09 18:41:39 +00:00
|
|
|
self._is_bidirectional = (
|
|
|
|
coordinator.data[CHARGER_DATA_KEY][CHARGER_PART_NUMBER_KEY][0:3]
|
|
|
|
in BIDIRECTIONAL_MODEL_PREFIXES
|
|
|
|
)
|
2021-10-27 17:53:14 +00:00
|
|
|
|
|
|
|
@property
|
2022-06-14 18:15:56 +00:00
|
|
|
def native_max_value(self) -> float:
|
2021-10-27 17:53:14 +00:00
|
|
|
"""Return the maximum available current."""
|
2022-04-19 06:44:25 +00:00
|
|
|
return cast(float, self._coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY])
|
2021-10-27 17:53:14 +00:00
|
|
|
|
2022-07-09 18:41:39 +00:00
|
|
|
@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
|
|
|
|
|
2021-10-27 17:53:14 +00:00
|
|
|
@property
|
2022-06-14 18:15:56 +00:00
|
|
|
def native_value(self) -> float | None:
|
2022-07-09 18:41:39 +00:00
|
|
|
"""Return the value of the entity."""
|
2021-11-23 21:30:22 +00:00
|
|
|
return cast(
|
2022-04-19 06:44:25 +00:00
|
|
|
Optional[float], self._coordinator.data[CHARGER_MAX_CHARGING_CURRENT_KEY]
|
2021-11-23 21:30:22 +00:00
|
|
|
)
|
2021-10-27 17:53:14 +00:00
|
|
|
|
2022-06-14 18:15:56 +00:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
2021-10-27 17:53:14 +00:00
|
|
|
"""Set the value of the entity."""
|
|
|
|
await self._coordinator.async_set_charging_current(value)
|