2022-01-21 09:44:56 +00:00
|
|
|
"""Creates Homewizard Energy switch entities."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
2023-01-15 16:30:27 +00:00
|
|
|
from .entity import HomeWizardEntity
|
|
|
|
from .helpers import homewizard_exception_handler
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up switches."""
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
2022-11-30 09:28:28 +00:00
|
|
|
entities: list[SwitchEntity] = []
|
|
|
|
|
2023-01-16 08:23:03 +00:00
|
|
|
if coordinator.data.state:
|
2022-11-30 09:28:28 +00:00
|
|
|
entities.append(HWEnergyMainSwitchEntity(coordinator, entry))
|
|
|
|
entities.append(HWEnergySwitchLockEntity(coordinator, entry))
|
2022-01-21 09:44:56 +00:00
|
|
|
|
2023-01-16 08:23:03 +00:00
|
|
|
if coordinator.data.system:
|
2022-11-30 09:28:28 +00:00
|
|
|
entities.append(HWEnergyEnableCloudEntity(hass, coordinator, entry))
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
2022-11-27 19:26:15 +00:00
|
|
|
|
2022-01-21 09:44:56 +00:00
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
class HWEnergySwitchEntity(HomeWizardEntity, SwitchEntity):
|
2022-01-21 09:44:56 +00:00
|
|
|
"""Representation switchable entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
key: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_unique_id = f"{entry.unique_id}_{key}"
|
|
|
|
|
|
|
|
|
|
|
|
class HWEnergyMainSwitchEntity(HWEnergySwitchEntity):
|
|
|
|
"""Representation of the main power switch."""
|
|
|
|
|
|
|
|
_attr_device_class = SwitchDeviceClass.OUTLET
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: HWEnergyDeviceUpdateCoordinator, entry: ConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator, entry, "power_on")
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-01-21 09:44:56 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the switch on."""
|
2022-05-25 07:05:11 +00:00
|
|
|
await self.coordinator.api.state_set(power_on=True)
|
2022-01-21 09:44:56 +00:00
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-01-21 09:44:56 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the switch off."""
|
2022-05-25 07:05:11 +00:00
|
|
|
await self.coordinator.api.state_set(power_on=False)
|
2022-01-21 09:44:56 +00:00
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""
|
|
|
|
Return availability of power_on.
|
|
|
|
|
|
|
|
This switch becomes unavailable when switch_lock is enabled.
|
|
|
|
"""
|
2023-01-16 08:23:03 +00:00
|
|
|
return (
|
|
|
|
super().available
|
|
|
|
and self.coordinator.data.state is not None
|
|
|
|
and not self.coordinator.data.state.switch_lock
|
|
|
|
)
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
@property
|
2023-01-16 08:23:03 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2022-01-21 09:44:56 +00:00
|
|
|
"""Return true if switch is on."""
|
2023-01-16 08:23:03 +00:00
|
|
|
if self.coordinator.data.state is None:
|
|
|
|
return None
|
|
|
|
return self.coordinator.data.state.power_on
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HWEnergySwitchLockEntity(HWEnergySwitchEntity):
|
|
|
|
"""
|
|
|
|
Representation of the switch-lock configuration.
|
|
|
|
|
|
|
|
Switch-lock is a feature that forces the relay in 'on' state.
|
|
|
|
It disables any method that can turn of the relay.
|
|
|
|
"""
|
|
|
|
|
2022-07-12 13:56:16 +00:00
|
|
|
_attr_name = "Switch lock"
|
2022-01-21 09:44:56 +00:00
|
|
|
_attr_device_class = SwitchDeviceClass.SWITCH
|
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: HWEnergyDeviceUpdateCoordinator, entry: ConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator, entry, "switch_lock")
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-01-21 09:44:56 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn switch-lock on."""
|
2022-05-25 07:05:11 +00:00
|
|
|
await self.coordinator.api.state_set(switch_lock=True)
|
2022-01-21 09:44:56 +00:00
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-01-21 09:44:56 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn switch-lock off."""
|
2022-05-25 07:05:11 +00:00
|
|
|
await self.coordinator.api.state_set(switch_lock=False)
|
2022-01-21 09:44:56 +00:00
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
|
|
|
@property
|
2023-01-16 08:23:03 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2022-01-21 09:44:56 +00:00
|
|
|
"""Return true if switch is on."""
|
2023-01-16 08:23:03 +00:00
|
|
|
if self.coordinator.data.state is None:
|
|
|
|
return None
|
|
|
|
return self.coordinator.data.state.switch_lock
|
2022-11-27 19:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HWEnergyEnableCloudEntity(HWEnergySwitchEntity):
|
|
|
|
"""
|
|
|
|
Representation of the enable cloud configuration.
|
|
|
|
|
|
|
|
Turning off 'cloud connection' turns off all communication to HomeWizard Cloud.
|
|
|
|
At this point, the device is fully local.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_attr_name = "Cloud connection"
|
|
|
|
_attr_device_class = SwitchDeviceClass.SWITCH
|
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator, entry, "cloud_connection")
|
|
|
|
self.hass = hass
|
|
|
|
self.entry = entry
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-11-27 19:26:15 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn cloud connection on."""
|
|
|
|
await self.coordinator.api.system_set(cloud_enabled=True)
|
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-11-27 19:26:15 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn cloud connection off."""
|
|
|
|
await self.coordinator.api.system_set(cloud_enabled=False)
|
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str | None:
|
|
|
|
"""Return the icon."""
|
|
|
|
return "mdi:cloud" if self.is_on else "mdi:cloud-off-outline"
|
|
|
|
|
|
|
|
@property
|
2023-01-16 08:23:03 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2022-11-27 19:26:15 +00:00
|
|
|
"""Return true if cloud connection is active."""
|
2023-01-16 08:23:03 +00:00
|
|
|
if self.coordinator.data.system is None:
|
|
|
|
return None
|
|
|
|
return self.coordinator.data.system.cloud_enabled
|