86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import EaseeSolarCoordinator
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
coordinator: EaseeSolarCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
async_add_entities([
|
|
SolarChargingSwitch(coordinator),
|
|
StopGraceSwitch(coordinator),
|
|
])
|
|
|
|
|
|
class SolarChargingSwitch(RestoreEntity, SwitchEntity):
|
|
_attr_has_entity_name = True
|
|
_attr_name = "Solar Charging"
|
|
_attr_icon = "mdi:solar-power"
|
|
|
|
def __init__(self, coordinator: EaseeSolarCoordinator) -> None:
|
|
self._coordinator = coordinator
|
|
self._attr_unique_id = f"{coordinator.charger_id}_solar_charging_enabled"
|
|
self.entity_id = f"switch.esc_{coordinator.charger_id.lower()}_solar_charging"
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
await super().async_added_to_hass()
|
|
last_state = await self.async_get_last_state()
|
|
if last_state is not None:
|
|
self._coordinator.solar_charging_enabled = last_state.state == "on"
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return self._coordinator.solar_charging_enabled
|
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
|
self._coordinator.solar_charging_enabled = True
|
|
await self._coordinator.async_request_refresh()
|
|
self.async_write_ha_state()
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
self._coordinator.solar_charging_enabled = False
|
|
await self._coordinator.async_request_refresh()
|
|
self.async_write_ha_state()
|
|
|
|
|
|
class StopGraceSwitch(RestoreEntity, SwitchEntity):
|
|
_attr_has_entity_name = True
|
|
_attr_name = "Stop Grace"
|
|
_attr_icon = "mdi:timer-pause"
|
|
|
|
def __init__(self, coordinator: EaseeSolarCoordinator) -> None:
|
|
self._coordinator = coordinator
|
|
self._attr_unique_id = f"{coordinator.charger_id}_stop_grace_enabled"
|
|
self.entity_id = f"switch.esc_{coordinator.charger_id.lower()}_stop_grace"
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
await super().async_added_to_hass()
|
|
last_state = await self.async_get_last_state()
|
|
if last_state is not None:
|
|
self._coordinator.stop_grace_enabled = last_state.state == "on"
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return self._coordinator.stop_grace_enabled
|
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
|
self._coordinator.stop_grace_enabled = True
|
|
self.async_write_ha_state()
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
self._coordinator.stop_grace_enabled = False
|
|
# Reset any running timer so the charger stops on the next poll
|
|
self._coordinator.cancel_stop_grace()
|
|
await self._coordinator.async_request_refresh()
|
|
self.async_write_ha_state()
|