2020-05-26 13:47:25 +00:00
|
|
|
"""Sensors for the Elexa Guardian integration."""
|
2021-03-18 07:02:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Callable
|
2020-07-05 22:09:40 +00:00
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-10-13 03:41:57 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
PERCENTAGE,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
TIME_MINUTES,
|
|
|
|
)
|
2020-07-05 22:09:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2020-10-13 03:41:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
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 PairedSensorEntity, ValveControllerEntity
|
2020-05-26 13:47:25 +00:00
|
|
|
from .const import (
|
2020-10-13 03:41:57 +00:00
|
|
|
API_SENSOR_PAIRED_SENSOR_STATUS,
|
2020-07-05 22:09:40 +00:00
|
|
|
API_SYSTEM_DIAGNOSTICS,
|
|
|
|
API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
2020-10-13 03:41:57 +00:00
|
|
|
CONF_UID,
|
2020-07-05 22:09:40 +00:00
|
|
|
DATA_COORDINATOR,
|
2020-10-13 03:41:57 +00:00
|
|
|
DATA_UNSUB_DISPATCHER_CONNECT,
|
2020-05-26 13:47:25 +00:00
|
|
|
DOMAIN,
|
2020-10-13 03:41:57 +00:00
|
|
|
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
|
2020-05-26 13:47:25 +00:00
|
|
|
)
|
|
|
|
|
2020-10-13 03:41:57 +00:00
|
|
|
SENSOR_KIND_BATTERY = "battery"
|
2020-07-05 22:09:40 +00:00
|
|
|
SENSOR_KIND_TEMPERATURE = "temperature"
|
|
|
|
SENSOR_KIND_UPTIME = "uptime"
|
2020-10-13 03:41:57 +00:00
|
|
|
|
|
|
|
SENSOR_ATTRS_MAP = {
|
|
|
|
SENSOR_KIND_BATTERY: ("Battery", DEVICE_CLASS_BATTERY, None, PERCENTAGE),
|
|
|
|
SENSOR_KIND_TEMPERATURE: (
|
2020-05-26 13:47:25 +00:00
|
|
|
"Temperature",
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
None,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
),
|
2020-10-13 03:41:57 +00:00
|
|
|
SENSOR_KIND_UPTIME: ("Uptime", None, "mdi:timer", TIME_MINUTES),
|
|
|
|
}
|
|
|
|
|
|
|
|
PAIRED_SENSOR_SENSORS = [SENSOR_KIND_BATTERY, SENSOR_KIND_TEMPERATURE]
|
|
|
|
VALVE_CONTROLLER_SENSORS = [SENSOR_KIND_TEMPERATURE, SENSOR_KIND_UPTIME]
|
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-10-13 03:41:57 +00:00
|
|
|
|
2020-10-13 20:01:10 +00:00
|
|
|
@callback
|
|
|
|
def add_new_paired_sensor(uid: str) -> None:
|
2020-10-13 03:41:57 +00:00
|
|
|
"""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, icon, unit = SENSOR_ATTRS_MAP[kind]
|
|
|
|
entities.append(
|
|
|
|
PairedSensorSensor(
|
|
|
|
entry, coordinator, kind, name, device_class, icon, unit
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
# 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, icon, unit = SENSOR_ATTRS_MAP[kind]
|
|
|
|
sensors.append(
|
|
|
|
ValveControllerSensor(
|
2020-07-05 22:09:40 +00:00
|
|
|
entry,
|
|
|
|
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id],
|
|
|
|
kind,
|
|
|
|
name,
|
|
|
|
device_class,
|
|
|
|
icon,
|
|
|
|
unit,
|
|
|
|
)
|
2020-10-13 03:41:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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, icon, unit = SENSOR_ATTRS_MAP[kind]
|
|
|
|
sensors.append(
|
|
|
|
PairedSensorSensor(
|
|
|
|
entry, coordinator, kind, name, device_class, icon, unit
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class PairedSensorSensor(PairedSensorEntity, SensorEntity):
|
2020-10-13 03:41:57 +00:00
|
|
|
"""Define a binary sensor related to a Guardian valve controller."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
kind: str,
|
|
|
|
name: str,
|
2021-03-18 07:02:55 +00:00
|
|
|
device_class: str | None,
|
|
|
|
icon: str | None,
|
|
|
|
unit: str | None,
|
2020-10-13 03:41:57 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(entry, coordinator, kind, name, device_class, icon)
|
|
|
|
|
|
|
|
self._state = None
|
|
|
|
self._unit = unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return whether the entity is available."""
|
|
|
|
return self.coordinator.last_update_success
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the sensor state."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_from_latest_data(self) -> None:
|
|
|
|
"""Update the entity."""
|
|
|
|
if self._kind == SENSOR_KIND_BATTERY:
|
|
|
|
self._state = self.coordinator.data["battery"]
|
|
|
|
elif self._kind == SENSOR_KIND_TEMPERATURE:
|
|
|
|
self._state = self.coordinator.data["temperature"]
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class ValveControllerSensor(ValveControllerEntity, SensorEntity):
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Define a generic Guardian sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2020-07-05 22:09:40 +00:00
|
|
|
entry: ConfigEntry,
|
2021-03-18 07:02:55 +00:00
|
|
|
coordinators: dict[str, DataUpdateCoordinator],
|
2020-05-26 13:47:25 +00:00
|
|
|
kind: str,
|
|
|
|
name: str,
|
2021-03-18 07:02:55 +00:00
|
|
|
device_class: str | None,
|
|
|
|
icon: str | None,
|
|
|
|
unit: str | None,
|
2020-07-05 22:09:40 +00:00
|
|
|
) -> None:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Initialize."""
|
2020-10-13 03:41:57 +00:00
|
|
|
super().__init__(entry, coordinators, kind, name, device_class, icon)
|
2020-05-26 13:47:25 +00:00
|
|
|
|
|
|
|
self._state = None
|
|
|
|
self._unit = unit
|
|
|
|
|
|
|
|
@property
|
2020-07-05 22:09:40 +00:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return whether the entity is available."""
|
|
|
|
if self._kind == SENSOR_KIND_TEMPERATURE:
|
2020-10-13 03:41:57 +00:00
|
|
|
return self.coordinators[
|
2020-07-05 22:09:40 +00:00
|
|
|
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
|
|
|
].last_update_success
|
|
|
|
if self._kind == SENSOR_KIND_UPTIME:
|
2020-10-13 03:41:57 +00:00
|
|
|
return self.coordinators[API_SYSTEM_DIAGNOSTICS].last_update_success
|
2020-07-05 22:09:40 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Return the sensor state."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2020-07-05 22:09:40 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2020-05-26 13:47:25 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit
|
|
|
|
|
2020-10-13 03:41:57 +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."""
|
|
|
|
if self._kind == SENSOR_KIND_TEMPERATURE:
|
|
|
|
self.async_add_coordinator_update_listener(API_SYSTEM_ONBOARD_SENSOR_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."""
|
|
|
|
if self._kind == SENSOR_KIND_TEMPERATURE:
|
2020-10-13 03:41:57 +00:00
|
|
|
self._state = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
2020-07-05 22:09:40 +00:00
|
|
|
"temperature"
|
|
|
|
]
|
2020-05-26 13:47:25 +00:00
|
|
|
elif self._kind == SENSOR_KIND_UPTIME:
|
2020-10-13 03:41:57 +00:00
|
|
|
self._state = self.coordinators[API_SYSTEM_DIAGNOSTICS].data["uptime"]
|