Files

95 lines
2.8 KiB
Python

"""Home Assistant component for accessing the Wallbox Portal API switch."""
from typing import Any, override
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import (
CHARGER_DATA_KEY,
CHARGER_PAUSE_RESUME_KEY,
CHARGER_SERIAL_NUMBER_KEY,
CHARGER_STATUS_DESCRIPTION_KEY,
ChargerStatus,
)
from .coordinator import WallboxConfigEntry, WallboxCoordinator
from .entity import WallboxEntity
SWITCH_TYPES: dict[str, SwitchEntityDescription] = {
CHARGER_PAUSE_RESUME_KEY: SwitchEntityDescription(
key=CHARGER_PAUSE_RESUME_KEY,
translation_key="pause_resume",
),
}
async def async_setup_entry(
hass: HomeAssistant,
entry: WallboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Create wallbox sensor entities in HASS."""
coordinator: WallboxCoordinator = entry.runtime_data
async_add_entities(
[WallboxSwitch(coordinator, SWITCH_TYPES[CHARGER_PAUSE_RESUME_KEY])]
)
# Coordinator is used to centralize the data updates
PARALLEL_UPDATES = 0
class WallboxSwitch(WallboxEntity, SwitchEntity):
"""Representation of the Wallbox portal."""
def __init__(
self,
coordinator: WallboxCoordinator,
description: SwitchEntityDescription,
) -> None:
"""Initialize a Wallbox switch."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = (
f"{description.key}"
f"-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
)
@property
@override
def available(self) -> bool:
"""Return the availability of the switch."""
return super().available and self.coordinator.data[
CHARGER_STATUS_DESCRIPTION_KEY
] not in {
ChargerStatus.UNKNOWN,
ChargerStatus.UPDATING,
ChargerStatus.ERROR,
ChargerStatus.LOCKED,
ChargerStatus.LOCKED_CAR_CONNECTED,
ChargerStatus.DISCONNECTED,
ChargerStatus.READY,
}
@property
@override
def is_on(self) -> bool:
"""Return the status of pause/resume."""
return self.coordinator.data[CHARGER_STATUS_DESCRIPTION_KEY] in {
ChargerStatus.CHARGING,
ChargerStatus.DISCHARGING,
ChargerStatus.WAITING_FOR_CAR,
ChargerStatus.WAITING,
}
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Pause charger."""
await self.coordinator.async_pause_charger(True)
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Resume charger."""
await self.coordinator.async_pause_charger(False)