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
|
|
|
|
|
2021-07-22 06:01:05 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2020-07-05 22:09:40 +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
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
from . import ValveControllerEntity
|
2021-12-02 19:47:15 +00:00
|
|
|
from .const import API_VALVE_STATUS, DATA_CLIENT, DATA_COORDINATOR, DOMAIN, LOGGER
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
ATTR_AVG_CURRENT = "average_current"
|
|
|
|
ATTR_INST_CURRENT = "instantaneous_current"
|
|
|
|
ATTR_INST_CURRENT_DDT = "instantaneous_current_ddt"
|
|
|
|
ATTR_TRAVEL_COUNT = "travel_count"
|
|
|
|
|
2021-08-25 08:34:02 +00:00
|
|
|
SWITCH_KIND_VALVE = "valve"
|
|
|
|
|
|
|
|
SWITCH_DESCRIPTION_VALVE = SwitchEntityDescription(
|
|
|
|
key=SWITCH_KIND_VALVE,
|
|
|
|
name="Valve Controller",
|
|
|
|
icon="mdi:water",
|
|
|
|
)
|
|
|
|
|
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."""
|
2020-07-05 22:09:40 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2020-10-13 03:41:57 +00:00
|
|
|
ValveControllerSwitch(
|
2020-07-05 22:09:40 +00:00
|
|
|
entry,
|
2021-10-22 10:25:05 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_CLIENT],
|
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR],
|
2020-07-05 22:09:40 +00:00
|
|
|
)
|
2020-10-13 03:41:57 +00:00
|
|
|
]
|
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):
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Define a switch to open/close the Guardian valve."""
|
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
client: Client,
|
2021-03-18 07:02:55 +00:00
|
|
|
coordinators: dict[str, DataUpdateCoordinator],
|
2021-05-20 15:47:30 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Initialize."""
|
2021-08-25 08:34:02 +00:00
|
|
|
super().__init__(entry, coordinators, SWITCH_DESCRIPTION_VALVE)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_is_on = True
|
2020-10-13 03:41:57 +00:00
|
|
|
self._client = client
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2021-07-22 06:01:05 +00:00
|
|
|
async def _async_continue_entity_setup(self) -> None:
|
2020-07-05 22:09:40 +00:00
|
|
|
"""Register API interest (and related tasks) when the entity is added."""
|
|
|
|
self.async_add_coordinator_update_listener(API_VALVE_STATUS)
|
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
@callback
|
2020-07-05 22:09:40 +00:00
|
|
|
def _async_update_from_latest_data(self) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Update the entity."""
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_available = self.coordinators[API_VALVE_STATUS].last_update_success
|
|
|
|
self._attr_is_on = self.coordinators[API_VALVE_STATUS].data["state"] in (
|
2020-05-26 13:47:25 +00:00
|
|
|
"start_opening",
|
|
|
|
"opening",
|
|
|
|
"finish_opening",
|
|
|
|
"opened",
|
|
|
|
)
|
|
|
|
|
2021-07-02 05:49:05 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2020-05-26 13:47:25 +00:00
|
|
|
{
|
2020-10-13 03:41:57 +00:00
|
|
|
ATTR_AVG_CURRENT: self.coordinators[API_VALVE_STATUS].data[
|
2020-05-26 13:47:25 +00:00
|
|
|
"average_current"
|
|
|
|
],
|
2020-10-13 03:41:57 +00:00
|
|
|
ATTR_INST_CURRENT: self.coordinators[API_VALVE_STATUS].data[
|
2020-05-26 13:47:25 +00:00
|
|
|
"instantaneous_current"
|
|
|
|
],
|
2020-10-13 03:41:57 +00:00
|
|
|
ATTR_INST_CURRENT_DDT: self.coordinators[API_VALVE_STATUS].data[
|
2020-05-26 13:47:25 +00:00
|
|
|
"instantaneous_current_ddt"
|
|
|
|
],
|
2020-10-13 03:41:57 +00:00
|
|
|
ATTR_TRAVEL_COUNT: self.coordinators[API_VALVE_STATUS].data[
|
2020-05-26 13:47:25 +00:00
|
|
|
"travel_count"
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-21 07:20:09 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Turn the valve off (closed)."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.valve.close()
|
2020-05-26 13:47:25 +00:00
|
|
|
except GuardianError as err:
|
|
|
|
LOGGER.error("Error while closing the valve: %s", err)
|
|
|
|
return
|
|
|
|
|
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:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Turn the valve on (open)."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.valve.open()
|
2020-05-26 13:47:25 +00:00
|
|
|
except GuardianError as err:
|
|
|
|
LOGGER.error("Error while opening the valve: %s", err)
|
|
|
|
return
|
|
|
|
|
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()
|