Support local push updates for most ScreenLogic entities (#87438)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Kevin Worrel 2023-02-06 18:13:36 -08:00 committed by GitHub
parent 4fbb14ecc7
commit 687d326bb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 352 additions and 252 deletions

View file

@ -1,5 +1,5 @@
"""Support for a ScreenLogic Binary Sensor."""
from screenlogicpy.const import DATA as SL_DATA, DEVICE_TYPE, EQUIPMENT, ON_OFF
from screenlogicpy.const import CODE, DATA as SL_DATA, DEVICE_TYPE, EQUIPMENT, ON_OFF
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
@ -10,8 +10,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ScreenlogicEntity
from . import ScreenlogicDataUpdateCoordinator
from .const import DOMAIN
from .entity import ScreenlogicEntity, ScreenLogicPushEntity
SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS = {DEVICE_TYPE.ALARM: BinarySensorDeviceClass.PROBLEM}
@ -29,69 +30,70 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
entities = []
coordinator = hass.data[DOMAIN][config_entry.entry_id]
entities: list[ScreenLogicBinarySensorEntity] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
]
gateway_data = coordinator.gateway_data
chemistry = gateway_data[SL_DATA.KEY_CHEMISTRY]
config = gateway_data[SL_DATA.KEY_CONFIG]
# Generic binary sensor
entities.append(ScreenLogicBinarySensor(coordinator, "chem_alarm"))
entities.append(
ScreenLogicStatusBinarySensor(coordinator, "chem_alarm", CODE.STATUS_CHANGED)
)
entities.extend(
[
ScreenlogicConfigBinarySensor(coordinator, cfg_sensor)
for cfg_sensor in coordinator.data[SL_DATA.KEY_CONFIG]
ScreenlogicConfigBinarySensor(coordinator, cfg_sensor, CODE.STATUS_CHANGED)
for cfg_sensor in config
if cfg_sensor in SUPPORTED_CONFIG_BINARY_SENSORS
]
)
if (
coordinator.data[SL_DATA.KEY_CONFIG]["equipment_flags"]
& EQUIPMENT.FLAG_INTELLICHEM
):
if config["equipment_flags"] & EQUIPMENT.FLAG_INTELLICHEM:
# IntelliChem alarm sensors
entities.extend(
[
ScreenlogicChemistryAlarmBinarySensor(coordinator, chem_alarm)
for chem_alarm in coordinator.data[SL_DATA.KEY_CHEMISTRY][
SL_DATA.KEY_ALERTS
]
if chem_alarm != "_raw"
ScreenlogicChemistryAlarmBinarySensor(
coordinator, chem_alarm, CODE.CHEMISTRY_CHANGED
)
for chem_alarm in chemistry[SL_DATA.KEY_ALERTS]
if not chem_alarm.startswith("_")
]
)
# Intellichem notification sensors
entities.extend(
[
ScreenlogicChemistryNotificationBinarySensor(coordinator, chem_notif)
for chem_notif in coordinator.data[SL_DATA.KEY_CHEMISTRY][
SL_DATA.KEY_NOTIFICATIONS
]
if chem_notif != "_raw"
ScreenlogicChemistryNotificationBinarySensor(
coordinator, chem_notif, CODE.CHEMISTRY_CHANGED
)
for chem_notif in chemistry[SL_DATA.KEY_NOTIFICATIONS]
if not chem_notif.startswith("_")
]
)
if (
coordinator.data[SL_DATA.KEY_CONFIG]["equipment_flags"]
& EQUIPMENT.FLAG_CHLORINATOR
):
if config["equipment_flags"] & EQUIPMENT.FLAG_CHLORINATOR:
# SCG binary sensor
entities.append(ScreenlogicSCGBinarySensor(coordinator, "scg_status"))
async_add_entities(entities)
class ScreenLogicBinarySensor(ScreenlogicEntity, BinarySensorEntity):
"""Representation of the basic ScreenLogic binary sensor entity."""
class ScreenLogicBinarySensorEntity(ScreenlogicEntity, BinarySensorEntity):
"""Base class for all ScreenLogic binary sensor entities."""
_attr_has_entity_name = True
_attr_entity_category = EntityCategory.DIAGNOSTIC
@property
def name(self):
def name(self) -> str | None:
"""Return the sensor name."""
return self.sensor["name"]
@property
def device_class(self):
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the device class."""
device_type = self.sensor.get("device_type")
return SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(device_type)
@ -102,46 +104,58 @@ class ScreenLogicBinarySensor(ScreenlogicEntity, BinarySensorEntity):
return self.sensor["value"] == ON_OFF.ON
@property
def sensor(self):
def sensor(self) -> dict:
"""Shortcut to access the sensor data."""
return self.coordinator.data[SL_DATA.KEY_SENSORS][self._data_key]
return self.gateway_data[SL_DATA.KEY_SENSORS][self._data_key]
class ScreenlogicChemistryAlarmBinarySensor(ScreenLogicBinarySensor):
class ScreenLogicStatusBinarySensor(
ScreenLogicBinarySensorEntity, ScreenLogicPushEntity
):
"""Representation of a basic ScreenLogic sensor entity."""
class ScreenlogicChemistryAlarmBinarySensor(
ScreenLogicBinarySensorEntity, ScreenLogicPushEntity
):
"""Representation of a ScreenLogic IntelliChem alarm binary sensor entity."""
@property
def sensor(self):
def sensor(self) -> dict:
"""Shortcut to access the sensor data."""
return self.coordinator.data[SL_DATA.KEY_CHEMISTRY][SL_DATA.KEY_ALERTS][
return self.gateway_data[SL_DATA.KEY_CHEMISTRY][SL_DATA.KEY_ALERTS][
self._data_key
]
class ScreenlogicChemistryNotificationBinarySensor(ScreenLogicBinarySensor):
class ScreenlogicChemistryNotificationBinarySensor(
ScreenLogicBinarySensorEntity, ScreenLogicPushEntity
):
"""Representation of a ScreenLogic IntelliChem notification binary sensor entity."""
@property
def sensor(self):
def sensor(self) -> dict:
"""Shortcut to access the sensor data."""
return self.coordinator.data[SL_DATA.KEY_CHEMISTRY][SL_DATA.KEY_NOTIFICATIONS][
return self.gateway_data[SL_DATA.KEY_CHEMISTRY][SL_DATA.KEY_NOTIFICATIONS][
self._data_key
]
class ScreenlogicSCGBinarySensor(ScreenLogicBinarySensor):
class ScreenlogicSCGBinarySensor(ScreenLogicBinarySensorEntity):
"""Representation of a ScreenLogic SCG binary sensor entity."""
@property
def sensor(self):
def sensor(self) -> dict:
"""Shortcut to access the sensor data."""
return self.coordinator.data[SL_DATA.KEY_SCG][self._data_key]
return self.gateway_data[SL_DATA.KEY_SCG][self._data_key]
class ScreenlogicConfigBinarySensor(ScreenLogicBinarySensor):
class ScreenlogicConfigBinarySensor(
ScreenLogicBinarySensorEntity, ScreenLogicPushEntity
):
"""Representation of a ScreenLogic config data binary sensor entity."""
@property
def sensor(self):
def sensor(self) -> dict:
"""Shortcut to access the sensor data."""
return self.coordinator.data[SL_DATA.KEY_CONFIG][self._data_key]
return self.gateway_data[SL_DATA.KEY_CONFIG][self._data_key]