2023-01-20 13:49:04 +00:00
|
|
|
"""Creates HomeWizard Energy switch entities."""
|
2022-01-21 09:44:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from dataclasses import dataclass
|
2022-01-21 09:44:56 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
from homewizard_energy import HomeWizardEnergy
|
|
|
|
|
|
|
|
from homeassistant.components.switch import (
|
|
|
|
SwitchDeviceClass,
|
|
|
|
SwitchEntity,
|
|
|
|
SwitchEntityDescription,
|
|
|
|
)
|
2022-01-21 09:44:56 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2022-01-21 09:44:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
from .const import DOMAIN, DeviceResponseEntry
|
2022-01-21 09:44:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
@dataclass
|
|
|
|
class HomeWizardEntityDescriptionMixin:
|
|
|
|
"""Mixin values for HomeWizard entities."""
|
|
|
|
|
2023-04-13 11:42:35 +00:00
|
|
|
create_fn: Callable[[HWEnergyDeviceUpdateCoordinator], bool]
|
2023-01-20 13:49:04 +00:00
|
|
|
available_fn: Callable[[DeviceResponseEntry], bool]
|
|
|
|
is_on_fn: Callable[[DeviceResponseEntry], bool | None]
|
|
|
|
set_fn: Callable[[HomeWizardEnergy, bool], Awaitable[Any]]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class HomeWizardSwitchEntityDescription(
|
|
|
|
SwitchEntityDescription, HomeWizardEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Class describing HomeWizard switch entities."""
|
|
|
|
|
|
|
|
icon_off: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
SWITCHES = [
|
|
|
|
HomeWizardSwitchEntityDescription(
|
|
|
|
key="power_on",
|
2023-06-30 15:33:50 +00:00
|
|
|
name=None,
|
2023-01-20 13:49:04 +00:00
|
|
|
device_class=SwitchDeviceClass.OUTLET,
|
2023-04-13 11:42:35 +00:00
|
|
|
create_fn=lambda coordinator: coordinator.supports_state(),
|
2023-01-20 13:49:04 +00:00
|
|
|
available_fn=lambda data: data.state is not None and not data.state.switch_lock,
|
|
|
|
is_on_fn=lambda data: data.state.power_on if data.state else None,
|
|
|
|
set_fn=lambda api, active: api.state_set(power_on=active),
|
|
|
|
),
|
|
|
|
HomeWizardSwitchEntityDescription(
|
|
|
|
key="switch_lock",
|
2023-06-28 12:02:54 +00:00
|
|
|
translation_key="switch_lock",
|
2023-01-20 13:49:04 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
icon="mdi:lock",
|
|
|
|
icon_off="mdi:lock-open",
|
2023-04-13 11:42:35 +00:00
|
|
|
create_fn=lambda coordinator: coordinator.supports_state(),
|
2023-01-20 13:49:04 +00:00
|
|
|
available_fn=lambda data: data.state is not None,
|
|
|
|
is_on_fn=lambda data: data.state.switch_lock if data.state else None,
|
|
|
|
set_fn=lambda api, active: api.state_set(switch_lock=active),
|
|
|
|
),
|
|
|
|
HomeWizardSwitchEntityDescription(
|
|
|
|
key="cloud_connection",
|
2023-06-28 12:02:54 +00:00
|
|
|
translation_key="cloud_connection",
|
2023-01-20 13:49:04 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
icon="mdi:cloud",
|
|
|
|
icon_off="mdi:cloud-off-outline",
|
2023-04-13 11:42:35 +00:00
|
|
|
create_fn=lambda coordinator: coordinator.supports_system(),
|
2023-01-20 13:49:04 +00:00
|
|
|
available_fn=lambda data: data.system is not None,
|
|
|
|
is_on_fn=lambda data: data.system.cloud_enabled if data.system else None,
|
|
|
|
set_fn=lambda api, active: api.system_set(cloud_enabled=active),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
async_add_entities(
|
|
|
|
HomeWizardSwitchEntity(
|
|
|
|
coordinator=coordinator,
|
|
|
|
description=description,
|
|
|
|
entry=entry,
|
|
|
|
)
|
|
|
|
for description in SWITCHES
|
2023-04-13 11:42:35 +00:00
|
|
|
if description.create_fn(coordinator)
|
2023-01-20 13:49:04 +00:00
|
|
|
)
|
2022-11-30 09:28:28 +00:00
|
|
|
|
2022-11-27 19:26:15 +00:00
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
class HomeWizardSwitchEntity(HomeWizardEntity, SwitchEntity):
|
|
|
|
"""Representation of a HomeWizard switch."""
|
2022-01-21 09:44:56 +00:00
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
entity_description: HomeWizardSwitchEntityDescription
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
2023-01-20 13:49:04 +00:00
|
|
|
description: HomeWizardSwitchEntityDescription,
|
2022-01-21 09:44:56 +00:00
|
|
|
entry: ConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the switch."""
|
|
|
|
super().__init__(coordinator)
|
2023-01-20 13:49:04 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
|
2022-01-21 09:44:56 +00:00
|
|
|
|
2023-01-20 13:49:04 +00:00
|
|
|
@property
|
|
|
|
def icon(self) -> str | None:
|
|
|
|
"""Return the icon."""
|
|
|
|
if self.entity_description.icon_off and self.is_on is False:
|
|
|
|
return self.entity_description.icon_off
|
|
|
|
return super().icon
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
2023-01-20 13:49:04 +00:00
|
|
|
"""Return if entity is available."""
|
|
|
|
return super().available and self.entity_description.available_fn(
|
|
|
|
self.coordinator.data
|
2023-01-16 08:23:03 +00:00
|
|
|
)
|
2022-01-21 09:44:56 +00:00
|
|
|
|
|
|
|
@property
|
2023-01-16 08:23:03 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2023-01-20 13:49:04 +00:00
|
|
|
"""Return state of the switch."""
|
|
|
|
return self.entity_description.is_on_fn(self.coordinator.data)
|
2022-11-27 19:26:15 +00:00
|
|
|
|
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:
|
2023-01-20 13:49:04 +00:00
|
|
|
"""Turn the switch on."""
|
|
|
|
await self.entity_description.set_fn(self.coordinator.api, True)
|
2022-11-27 19:26:15 +00:00
|
|
|
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:
|
2023-01-20 13:49:04 +00:00
|
|
|
"""Turn the switch off."""
|
|
|
|
await self.entity_description.set_fn(self.coordinator.api, False)
|
2022-11-27 19:26:15 +00:00
|
|
|
await self.coordinator.async_refresh()
|