Add support for Elexa Guardian paired sensors (#37930)
* Add support for Guardian paired sensors * More work * OOP * More OOP * Binary sensors looking good * Entities all in place * Looking good * Linting * Code review * Code review * Flake * Fix removal * Code review * Linting * Don't use potentially confusing method name * Use CoordinatorEntity * Linting * Pylint * Code review * Code review
This commit is contained in:
parent
f7d3f3a1ed
commit
bb98f7ed1b
6 changed files with 590 additions and 136 deletions
|
@ -1,70 +1,167 @@
|
|||
"""Binary sensors for the Elexa Guardian integration."""
|
||||
from typing import Callable, Dict
|
||||
|
||||
from aioguardian import Client
|
||||
from typing import Callable, Dict, Optional
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
DEVICE_CLASS_MOISTURE,
|
||||
DEVICE_CLASS_MOVING,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import GuardianEntity
|
||||
from . import PairedSensorEntity, ValveControllerEntity
|
||||
from .const import (
|
||||
API_SENSOR_PAIRED_SENSOR_STATUS,
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
||||
API_WIFI_STATUS,
|
||||
DATA_CLIENT,
|
||||
CONF_UID,
|
||||
DATA_COORDINATOR,
|
||||
DATA_UNSUB_DISPATCHER_CONNECT,
|
||||
DOMAIN,
|
||||
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
|
||||
)
|
||||
|
||||
ATTR_CONNECTED_CLIENTS = "connected_clients"
|
||||
|
||||
SENSOR_KIND_AP_INFO = "ap_enabled"
|
||||
SENSOR_KIND_LEAK_DETECTED = "leak_detected"
|
||||
SENSORS = [
|
||||
(SENSOR_KIND_AP_INFO, "Onboard AP Enabled", DEVICE_CLASS_CONNECTIVITY),
|
||||
(SENSOR_KIND_LEAK_DETECTED, "Leak Detected", DEVICE_CLASS_MOISTURE),
|
||||
]
|
||||
SENSOR_KIND_MOVED = "moved"
|
||||
|
||||
SENSOR_ATTRS_MAP = {
|
||||
SENSOR_KIND_AP_INFO: ("Onboard AP Enabled", DEVICE_CLASS_CONNECTIVITY),
|
||||
SENSOR_KIND_LEAK_DETECTED: ("Leak Detected", DEVICE_CLASS_MOISTURE),
|
||||
SENSOR_KIND_MOVED: ("Recently Moved", DEVICE_CLASS_MOVING),
|
||||
}
|
||||
|
||||
PAIRED_SENSOR_SENSORS = [SENSOR_KIND_LEAK_DETECTED, SENSOR_KIND_MOVED]
|
||||
VALVE_CONTROLLER_SENSORS = [SENSOR_KIND_AP_INFO, SENSOR_KIND_LEAK_DETECTED]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: Callable
|
||||
) -> None:
|
||||
"""Set up Guardian switches based on a config entry."""
|
||||
async_add_entities(
|
||||
[
|
||||
GuardianBinarySensor(
|
||||
|
||||
async def add_new_paired_sensor(uid: str) -> None:
|
||||
"""Add a new paired sensor."""
|
||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][
|
||||
API_SENSOR_PAIRED_SENSOR_STATUS
|
||||
][uid]
|
||||
|
||||
entities = []
|
||||
for kind in PAIRED_SENSOR_SENSORS:
|
||||
name, device_class = SENSOR_ATTRS_MAP[kind]
|
||||
entities.append(
|
||||
PairedSensorBinarySensor(
|
||||
entry,
|
||||
coordinator,
|
||||
kind,
|
||||
name,
|
||||
device_class,
|
||||
None,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
# Handle adding paired sensors after HASS startup:
|
||||
hass.data[DOMAIN][DATA_UNSUB_DISPATCHER_CONNECT][entry.entry_id].append(
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED.format(entry.data[CONF_UID]),
|
||||
add_new_paired_sensor,
|
||||
)
|
||||
)
|
||||
|
||||
sensors = []
|
||||
|
||||
# Add all valve controller-specific binary sensors:
|
||||
for kind in VALVE_CONTROLLER_SENSORS:
|
||||
name, device_class = SENSOR_ATTRS_MAP[kind]
|
||||
sensors.append(
|
||||
ValveControllerBinarySensor(
|
||||
entry,
|
||||
hass.data[DOMAIN][DATA_CLIENT][entry.entry_id],
|
||||
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id],
|
||||
kind,
|
||||
name,
|
||||
device_class,
|
||||
None,
|
||||
)
|
||||
for kind, name, device_class in SENSORS
|
||||
],
|
||||
True,
|
||||
)
|
||||
)
|
||||
|
||||
# Add all paired sensor-specific binary sensors:
|
||||
for coordinator in hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id][
|
||||
API_SENSOR_PAIRED_SENSOR_STATUS
|
||||
].values():
|
||||
for kind in PAIRED_SENSOR_SENSORS:
|
||||
name, device_class = SENSOR_ATTRS_MAP[kind]
|
||||
sensors.append(
|
||||
PairedSensorBinarySensor(
|
||||
entry,
|
||||
coordinator,
|
||||
kind,
|
||||
name,
|
||||
device_class,
|
||||
None,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class GuardianBinarySensor(GuardianEntity, BinarySensorEntity):
|
||||
"""Define a generic Guardian sensor."""
|
||||
class PairedSensorBinarySensor(PairedSensorEntity, BinarySensorEntity):
|
||||
"""Define a binary sensor related to a Guardian valve controller."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
kind: str,
|
||||
name: str,
|
||||
device_class: Optional[str],
|
||||
icon: Optional[str],
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(entry, coordinator, kind, name, device_class, icon)
|
||||
|
||||
self._is_on = True
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._is_on
|
||||
|
||||
@callback
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
self._is_on = self.coordinator.data["wet"]
|
||||
elif self._kind == SENSOR_KIND_MOVED:
|
||||
self._is_on = self.coordinator.data["moved"]
|
||||
|
||||
|
||||
class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
||||
"""Define a binary sensor related to a Guardian valve controller."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
client: Client,
|
||||
coordinators: Dict[str, DataUpdateCoordinator],
|
||||
kind: str,
|
||||
name: str,
|
||||
device_class: str,
|
||||
device_class: Optional[str],
|
||||
icon: Optional[str],
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(entry, client, coordinators, kind, name, device_class, None)
|
||||
super().__init__(entry, coordinators, kind, name, device_class, icon)
|
||||
|
||||
self._is_on = True
|
||||
|
||||
|
@ -72,9 +169,9 @@ class GuardianBinarySensor(GuardianEntity, BinarySensorEntity):
|
|||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
if self._kind == SENSOR_KIND_AP_INFO:
|
||||
return self._coordinators[API_WIFI_STATUS].last_update_success
|
||||
return self.coordinators[API_WIFI_STATUS].last_update_success
|
||||
if self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
return self._coordinators[
|
||||
return self.coordinators[
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
||||
].last_update_success
|
||||
return False
|
||||
|
@ -84,7 +181,8 @@ class GuardianBinarySensor(GuardianEntity, BinarySensorEntity):
|
|||
"""Return True if the binary sensor is on."""
|
||||
return self._is_on
|
||||
|
||||
async def _async_internal_added_to_hass(self) -> None:
|
||||
async def _async_continue_entity_setup(self) -> None:
|
||||
"""Add an API listener."""
|
||||
if self._kind == SENSOR_KIND_AP_INFO:
|
||||
self.async_add_coordinator_update_listener(API_WIFI_STATUS)
|
||||
elif self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
|
@ -94,15 +192,15 @@ class GuardianBinarySensor(GuardianEntity, BinarySensorEntity):
|
|||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_AP_INFO:
|
||||
self._is_on = self._coordinators[API_WIFI_STATUS].data["station_connected"]
|
||||
self._is_on = self.coordinators[API_WIFI_STATUS].data["station_connected"]
|
||||
self._attrs.update(
|
||||
{
|
||||
ATTR_CONNECTED_CLIENTS: self._coordinators[API_WIFI_STATUS].data[
|
||||
ATTR_CONNECTED_CLIENTS: self.coordinators[API_WIFI_STATUS].data.get(
|
||||
"ap_clients"
|
||||
]
|
||||
)
|
||||
}
|
||||
)
|
||||
elif self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
self._is_on = self._coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
self._is_on = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
"wet"
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue