2020-05-26 13:47:25 +00:00
|
|
|
"""Switches for the Elexa Guardian integration."""
|
2020-07-05 22:09:40 +00:00
|
|
|
from typing import Callable, Dict
|
|
|
|
|
|
|
|
from aioguardian import Client
|
2020-05-26 13:47:25 +00:00
|
|
|
from aioguardian.errors import GuardianError
|
2020-06-30 00:24:42 +00:00
|
|
|
import voluptuous as vol
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-06-30 00:24:42 +00:00
|
|
|
from homeassistant.const import CONF_FILENAME, CONF_PORT, CONF_URL
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-06-30 00:24:42 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
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
|
|
|
|
from .const import (
|
|
|
|
API_VALVE_STATUS,
|
|
|
|
CONF_UID,
|
|
|
|
DATA_CLIENT,
|
|
|
|
DATA_COORDINATOR,
|
|
|
|
DATA_PAIRED_SENSOR_MANAGER,
|
|
|
|
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"
|
|
|
|
|
2020-06-30 00:24:42 +00:00
|
|
|
SERVICE_DISABLE_AP = "disable_ap"
|
|
|
|
SERVICE_ENABLE_AP = "enable_ap"
|
2020-10-13 03:41:57 +00:00
|
|
|
SERVICE_PAIR_SENSOR = "pair_sensor"
|
2020-06-30 00:24:42 +00:00
|
|
|
SERVICE_REBOOT = "reboot"
|
|
|
|
SERVICE_RESET_VALVE_DIAGNOSTICS = "reset_valve_diagnostics"
|
2020-10-13 03:41:57 +00:00
|
|
|
SERVICE_UNPAIR_SENSOR = "unpair_sensor"
|
2020-06-30 00:24:42 +00:00
|
|
|
SERVICE_UPGRADE_FIRMWARE = "upgrade_firmware"
|
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2020-07-05 22:09:40 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: Callable
|
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Set up Guardian switches based on a config entry."""
|
2020-06-30 00:24:42 +00:00
|
|
|
platform = entity_platform.current_platform.get()
|
|
|
|
|
|
|
|
for service_name, schema, method in [
|
2020-07-10 02:52:13 +00:00
|
|
|
(SERVICE_DISABLE_AP, {}, "async_disable_ap"),
|
|
|
|
(SERVICE_ENABLE_AP, {}, "async_enable_ap"),
|
2020-10-13 03:41:57 +00:00
|
|
|
(SERVICE_PAIR_SENSOR, {vol.Required(CONF_UID): cv.string}, "async_pair_sensor"),
|
2020-07-10 02:52:13 +00:00
|
|
|
(SERVICE_REBOOT, {}, "async_reboot"),
|
|
|
|
(SERVICE_RESET_VALVE_DIAGNOSTICS, {}, "async_reset_valve_diagnostics"),
|
2020-06-30 00:24:42 +00:00
|
|
|
(
|
|
|
|
SERVICE_UPGRADE_FIRMWARE,
|
2020-07-10 02:52:13 +00:00
|
|
|
{
|
|
|
|
vol.Optional(CONF_URL): cv.url,
|
|
|
|
vol.Optional(CONF_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_FILENAME): cv.string,
|
|
|
|
},
|
2020-06-30 00:24:42 +00:00
|
|
|
"async_upgrade_firmware",
|
|
|
|
),
|
2020-10-13 03:41:57 +00:00
|
|
|
(
|
|
|
|
SERVICE_UNPAIR_SENSOR,
|
|
|
|
{vol.Required(CONF_UID): cv.string},
|
|
|
|
"async_unpair_sensor",
|
|
|
|
),
|
2020-06-30 00:24:42 +00:00
|
|
|
]:
|
|
|
|
platform.async_register_entity_service(service_name, schema, method)
|
|
|
|
|
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,
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT][entry.entry_id],
|
|
|
|
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id],
|
|
|
|
)
|
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,
|
|
|
|
coordinators: Dict[str, DataUpdateCoordinator],
|
|
|
|
):
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Initialize."""
|
2020-07-05 22:09:40 +00:00
|
|
|
super().__init__(
|
2020-10-13 03:41:57 +00:00
|
|
|
entry, coordinators, "valve", "Valve Controller", None, "mdi:water"
|
2020-07-05 22:09:40 +00:00
|
|
|
)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
self._client = client
|
2020-05-26 13:47:25 +00:00
|
|
|
self._is_on = True
|
|
|
|
|
|
|
|
@property
|
2020-07-05 22:09:40 +00:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return whether the entity is available."""
|
2020-10-13 03:41:57 +00:00
|
|
|
return self.coordinators[API_VALVE_STATUS].last_update_success
|
2020-07-05 22:09:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Return True if the valve is open."""
|
|
|
|
return self._is_on
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
async def _async_continue_entity_setup(self):
|
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."""
|
2020-10-13 03:41:57 +00:00
|
|
|
self._is_on = self.coordinators[API_VALVE_STATUS].data["state"] in (
|
2020-05-26 13:47:25 +00:00
|
|
|
"start_opening",
|
|
|
|
"opening",
|
|
|
|
"finish_opening",
|
|
|
|
"opened",
|
|
|
|
)
|
|
|
|
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
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"
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-06-30 00:24:42 +00:00
|
|
|
async def async_disable_ap(self):
|
|
|
|
"""Disable the device's onboard access point."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.wifi.disable_ap()
|
2020-06-30 00:24:42 +00:00
|
|
|
except GuardianError as err:
|
2020-10-13 03:41:57 +00:00
|
|
|
LOGGER.error("Error while disabling valve controller AP: %s", err)
|
2020-06-30 00:24:42 +00:00
|
|
|
|
|
|
|
async def async_enable_ap(self):
|
|
|
|
"""Enable the device's onboard access point."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.wifi.enable_ap()
|
2020-06-30 00:24:42 +00:00
|
|
|
except GuardianError as err:
|
2020-10-13 03:41:57 +00:00
|
|
|
LOGGER.error("Error while enabling valve controller AP: %s", err)
|
|
|
|
|
|
|
|
async def async_pair_sensor(self, *, uid):
|
|
|
|
"""Add a new paired sensor."""
|
|
|
|
try:
|
|
|
|
async with self._client:
|
|
|
|
await self._client.sensor.pair_sensor(uid)
|
|
|
|
except GuardianError as err:
|
|
|
|
LOGGER.error("Error while adding paired sensor: %s", err)
|
|
|
|
return
|
|
|
|
|
|
|
|
await self.hass.data[DOMAIN][DATA_PAIRED_SENSOR_MANAGER][
|
|
|
|
self._entry.entry_id
|
|
|
|
].async_pair_sensor(uid)
|
2020-06-30 00:24:42 +00:00
|
|
|
|
|
|
|
async def async_reboot(self):
|
|
|
|
"""Reboot the device."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.system.reboot()
|
2020-06-30 00:24:42 +00:00
|
|
|
except GuardianError as err:
|
2020-10-13 03:41:57 +00:00
|
|
|
LOGGER.error("Error while rebooting valve controller: %s", err)
|
2020-06-30 00:24:42 +00:00
|
|
|
|
|
|
|
async def async_reset_valve_diagnostics(self):
|
|
|
|
"""Fully reset system motor diagnostics."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.valve.reset()
|
2020-06-30 00:24:42 +00:00
|
|
|
except GuardianError as err:
|
2020-10-13 03:41:57 +00:00
|
|
|
LOGGER.error("Error while resetting valve diagnostics: %s", err)
|
|
|
|
|
|
|
|
async def async_unpair_sensor(self, *, uid):
|
|
|
|
"""Add a new paired sensor."""
|
|
|
|
try:
|
|
|
|
async with self._client:
|
|
|
|
await self._client.sensor.unpair_sensor(uid)
|
|
|
|
except GuardianError as err:
|
|
|
|
LOGGER.error("Error while removing paired sensor: %s", err)
|
|
|
|
return
|
|
|
|
|
|
|
|
await self.hass.data[DOMAIN][DATA_PAIRED_SENSOR_MANAGER][
|
|
|
|
self._entry.entry_id
|
|
|
|
].async_unpair_sensor(uid)
|
2020-06-30 00:24:42 +00:00
|
|
|
|
|
|
|
async def async_upgrade_firmware(self, *, url, port, filename):
|
|
|
|
"""Upgrade the device firmware."""
|
|
|
|
try:
|
2020-07-05 22:09:40 +00:00
|
|
|
async with self._client:
|
|
|
|
await self._client.system.upgrade_firmware(
|
2020-08-27 11:56:20 +00:00
|
|
|
url=url,
|
|
|
|
port=port,
|
|
|
|
filename=filename,
|
2020-06-30 00:24:42 +00:00
|
|
|
)
|
|
|
|
except GuardianError as err:
|
2020-10-13 03:41:57 +00:00
|
|
|
LOGGER.error("Error while upgrading firmware: %s", err)
|
2020-06-30 00:24:42 +00:00
|
|
|
|
2020-05-26 13:47:25 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
|
|
"""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
|
|
|
|
|
|
|
|
self._is_on = False
|
2020-06-26 16:19:38 +00:00
|
|
|
self.async_write_ha_state()
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
|
|
|
"""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
|
|
|
|
|
|
|
|
self._is_on = True
|
2020-06-26 16:19:38 +00:00
|
|
|
self.async_write_ha_state()
|