2020-05-26 13:47:25 +00:00
|
|
|
"""Switches for the Elexa Guardian integration."""
|
2021-03-18 07:02:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-04 05:17:12 +00:00
|
|
|
from collections.abc import Awaitable, Callable, Mapping
|
2022-07-18 15:18:07 +00:00
|
|
|
from dataclasses import dataclass
|
2021-07-22 06:01:05 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2022-09-17 21:01:57 +00:00
|
|
|
from aioguardian import Client
|
2020-05-26 13:47:25 +00:00
|
|
|
from aioguardian.errors import GuardianError
|
|
|
|
|
2021-08-25 08:34:02 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2024-01-04 05:17:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-19 21:14:08 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2022-07-22 02:32:42 +00:00
|
|
|
from . import GuardianData, ValveControllerEntity, ValveControllerEntityDescription
|
2022-09-17 21:01:57 +00:00
|
|
|
from .const import API_VALVE_STATUS, API_WIFI_STATUS, DOMAIN
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
ATTR_AVG_CURRENT = "average_current"
|
2022-09-17 21:01:57 +00:00
|
|
|
ATTR_CONNECTED_CLIENTS = "connected_clients"
|
2020-05-26 13:47:25 +00:00
|
|
|
ATTR_INST_CURRENT = "instantaneous_current"
|
|
|
|
ATTR_INST_CURRENT_DDT = "instantaneous_current_ddt"
|
2022-09-17 21:01:57 +00:00
|
|
|
ATTR_STATION_CONNECTED = "station_connected"
|
2020-05-26 13:47:25 +00:00
|
|
|
ATTR_TRAVEL_COUNT = "travel_count"
|
|
|
|
|
2022-09-17 21:01:57 +00:00
|
|
|
SWITCH_KIND_ONBOARD_AP = "onboard_ap"
|
2021-08-25 08:34:02 +00:00
|
|
|
SWITCH_KIND_VALVE = "valve"
|
|
|
|
|
2024-01-04 05:17:12 +00:00
|
|
|
ON_STATES = {
|
|
|
|
"start_opening",
|
|
|
|
"opening",
|
|
|
|
"finish_opening",
|
|
|
|
"opened",
|
|
|
|
}
|
2022-07-18 15:18:07 +00:00
|
|
|
|
2022-09-17 21:01:57 +00:00
|
|
|
|
2024-01-04 05:17:12 +00:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
2022-07-18 15:18:07 +00:00
|
|
|
class ValveControllerSwitchDescription(
|
2024-01-04 05:17:12 +00:00
|
|
|
SwitchEntityDescription, ValveControllerEntityDescription
|
2022-07-18 15:18:07 +00:00
|
|
|
):
|
|
|
|
"""Describe a Guardian valve controller switch."""
|
|
|
|
|
2024-01-04 05:17:12 +00:00
|
|
|
extra_state_attributes_fn: Callable[[dict[str, Any]], Mapping[str, Any]]
|
|
|
|
is_on_fn: Callable[[dict[str, Any]], bool]
|
|
|
|
off_fn: Callable[[Client], Awaitable]
|
|
|
|
on_fn: Callable[[Client], Awaitable]
|
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
|
2022-09-17 21:01:57 +00:00
|
|
|
async def _async_disable_ap(client: Client) -> None:
|
|
|
|
"""Disable the onboard AP."""
|
|
|
|
await client.wifi.disable_ap()
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_enable_ap(client: Client) -> None:
|
|
|
|
"""Enable the onboard AP."""
|
|
|
|
await client.wifi.enable_ap()
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_close_valve(client: Client) -> None:
|
|
|
|
"""Close the valve."""
|
|
|
|
await client.valve.close()
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_open_valve(client: Client) -> None:
|
|
|
|
"""Open the valve."""
|
|
|
|
await client.valve.open()
|
|
|
|
|
|
|
|
|
2022-07-18 15:18:07 +00:00
|
|
|
VALVE_CONTROLLER_DESCRIPTIONS = (
|
2022-09-17 21:01:57 +00:00
|
|
|
ValveControllerSwitchDescription(
|
|
|
|
key=SWITCH_KIND_ONBOARD_AP,
|
2023-06-27 20:24:41 +00:00
|
|
|
translation_key="onboard_access_point",
|
2022-09-17 21:01:57 +00:00
|
|
|
icon="mdi:wifi",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
2024-01-04 05:17:12 +00:00
|
|
|
extra_state_attributes_fn=lambda data: {
|
|
|
|
ATTR_CONNECTED_CLIENTS: data.get("ap_clients"),
|
|
|
|
ATTR_STATION_CONNECTED: data["station_connected"],
|
|
|
|
},
|
2022-09-17 21:01:57 +00:00
|
|
|
api_category=API_WIFI_STATUS,
|
2024-01-04 05:17:12 +00:00
|
|
|
is_on_fn=lambda data: data["ap_enabled"],
|
|
|
|
off_fn=_async_disable_ap,
|
|
|
|
on_fn=_async_enable_ap,
|
2022-09-17 21:01:57 +00:00
|
|
|
),
|
2022-07-18 15:18:07 +00:00
|
|
|
ValveControllerSwitchDescription(
|
|
|
|
key=SWITCH_KIND_VALVE,
|
2023-06-27 20:24:41 +00:00
|
|
|
translation_key="valve_controller",
|
2022-07-18 15:18:07 +00:00
|
|
|
icon="mdi:water",
|
|
|
|
api_category=API_VALVE_STATUS,
|
2024-01-04 05:17:12 +00:00
|
|
|
extra_state_attributes_fn=lambda data: {
|
|
|
|
ATTR_AVG_CURRENT: data["average_current"],
|
|
|
|
ATTR_INST_CURRENT: data["instantaneous_current"],
|
|
|
|
ATTR_INST_CURRENT_DDT: data["instantaneous_current_ddt"],
|
|
|
|
ATTR_TRAVEL_COUNT: data["travel_count"],
|
|
|
|
},
|
|
|
|
is_on_fn=lambda data: data["state"] in ON_STATES,
|
|
|
|
off_fn=_async_close_valve,
|
|
|
|
on_fn=_async_open_valve,
|
2022-07-18 15:18:07 +00:00
|
|
|
),
|
2021-08-25 08:34:02 +00:00
|
|
|
)
|
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-29 10:28:14 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-07-05 22:09:40 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Set up Guardian switches based on a config entry."""
|
2022-07-22 02:32:42 +00:00
|
|
|
data: GuardianData = hass.data[DOMAIN][entry.entry_id]
|
2022-07-18 15:18:07 +00:00
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
async_add_entities(
|
2022-07-22 02:32:42 +00:00
|
|
|
ValveControllerSwitch(entry, data, description)
|
2022-07-18 15:18:07 +00:00
|
|
|
for description in VALVE_CONTROLLER_DESCRIPTIONS
|
2020-07-05 22:09:40 +00:00
|
|
|
)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
|
2022-09-17 21:01:57 +00:00
|
|
|
"""Define a switch related to a Guardian valve controller."""
|
2022-07-18 15:18:07 +00:00
|
|
|
|
2022-09-17 21:01:57 +00:00
|
|
|
entity_description: ValveControllerSwitchDescription
|
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry: ConfigEntry,
|
2022-07-22 02:32:42 +00:00
|
|
|
data: GuardianData,
|
2022-07-18 15:18:07 +00:00
|
|
|
description: ValveControllerSwitchDescription,
|
2021-05-20 15:47:30 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Initialize."""
|
2022-07-22 02:32:42 +00:00
|
|
|
super().__init__(entry, data.valve_controller_coordinators, description)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2022-07-22 02:32:42 +00:00
|
|
|
self._client = data.client
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2024-01-04 05:17:12 +00:00
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
|
|
"""Return entity specific state attributes."""
|
|
|
|
return self.entity_description.extra_state_attributes_fn(self.coordinator.data)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self.entity_description.is_on_fn(self.coordinator.data)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-08-21 07:20:09 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2022-09-17 21:01:57 +00:00
|
|
|
"""Turn the switch off."""
|
2022-09-19 19:15:32 +00:00
|
|
|
if not self._attr_is_on:
|
|
|
|
return
|
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
2024-01-04 05:17:12 +00:00
|
|
|
await self.entity_description.off_fn(self._client)
|
2020-05-26 13:47:25 +00:00
|
|
|
except GuardianError as err:
|
2022-09-17 21:01:57 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f'Error while turning "{self.entity_id}" off: {err}'
|
|
|
|
) from err
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = False
|
2020-06-26 16:19:38 +00:00
|
|
|
self.async_write_ha_state()
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-08-21 07:20:09 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2022-09-17 21:01:57 +00:00
|
|
|
"""Turn the switch on."""
|
2022-09-19 19:15:32 +00:00
|
|
|
if self._attr_is_on:
|
|
|
|
return
|
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
2024-01-04 05:17:12 +00:00
|
|
|
await self.entity_description.on_fn(self._client)
|
2020-05-26 13:47:25 +00:00
|
|
|
except GuardianError as err:
|
2022-09-17 21:01:57 +00:00
|
|
|
raise HomeAssistantError(
|
|
|
|
f'Error while turning "{self.entity_id}" on: {err}'
|
|
|
|
) from err
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = True
|
2020-06-26 16:19:38 +00:00
|
|
|
self.async_write_ha_state()
|