From 9177865b84e65f9f66fb71427f70692b41c039f3 Mon Sep 17 00:00:00 2001 From: David Naylor Date: Thu, 6 Jul 2023 12:16:45 -0400 Subject: [PATCH] Add flow detected sensor for Chem Controller --- custom_components/njspc_ha/binary_sensor.py | 15 +++++ custom_components/njspc_ha/chemistry.py | 65 +++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/custom_components/njspc_ha/binary_sensor.py b/custom_components/njspc_ha/binary_sensor.py index f20fe8d..51a6ffe 100644 --- a/custom_components/njspc_ha/binary_sensor.py +++ b/custom_components/njspc_ha/binary_sensor.py @@ -7,6 +7,7 @@ from .controller import FreezeProtectionSensor +from .chemistry import FlowDetectedSensor from .pumps import PumpOnSensor from .bodies import FilterOnSensor, BodyCoveredSensor from .features import VirtualCircuit @@ -52,5 +53,19 @@ async def async_setup_entry( BodyCoveredSensor(coordinator=coordinator, body=body) ) + for chem_controller in config["chemControllers"]: + if ( + "name" in chem_controller + and "type" in chem_controller + and "name" in chem_controller["type"] + and chem_controller["type"]["name"] != "none" + ): + new_devices.append( + FlowDetectedSensor( + coordinator=coordinator, + chem_controller=chem_controller + ) + ) + if new_devices: async_add_entities(new_devices) diff --git a/custom_components/njspc_ha/chemistry.py b/custom_components/njspc_ha/chemistry.py index f38c00d..46ba654 100644 --- a/custom_components/njspc_ha/chemistry.py +++ b/custom_components/njspc_ha/chemistry.py @@ -19,6 +19,9 @@ SensorEntity, SensorStateClass, ) +from homeassistant.components.binary_sensor import ( + BinarySensorEntity +) from homeassistant.components.number import ( NumberEntity, NumberMode @@ -43,6 +46,68 @@ SUPER_CHLOR ) +class FlowDetectedSensor(PoolEquipmentEntity, BinarySensorEntity): + """The current freeze protection status for the control panel""" + + def __init__(self, coordinator: NjsPCHAdata, chem_controller) -> None: + """Initialize the sensor.""" + super().__init__(coordinator = coordinator, equipment_class=PoolEquipmentClass.CHEM_CONTROLLER, data=chem_controller) + if "flowDetected" in chem_controller: + self._value = chem_controller["flowDetected"] + self._available = True + else: + self._value = None + self._available = False + + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + if self.coordinator.data["event"] == EVENT_CHEM_CONTROLLER: + if "flowDetected" in self.coordinator.data: + self._available = True + self._value = self.coordinator.data["flowDetected"] + else: + self._available = False + self._value = None + self.async_write_ha_state() + elif self.coordinator.data["event"] == EVENT_AVAILABILITY: + self._available = self.coordinator.data["available"] + self.async_write_ha_state() + + @property + def should_poll(self) -> bool: + return False + + @property + def available(self) -> bool: + return self._available + + @property + def name(self) -> str | None: + """Name of the sensor""" + return "Flow Detected" + + @property + def unique_id(self) -> str | None: + """ID of the sensor""" + return f"{self.coordinator.controller_id}_{self.equipment_class}_{self.equipment_id}_flow_detected" + + @property + def native_value(self) -> bool | None: + """Raw value of the sensor""" + return self._value + + @property + def is_on(self) -> bool: + """Return if motion is detected.""" + return self._value + + + @property + def icon(self) -> str: + if self._value is True: + return "mdi:waves-arrow-right" + return "mdi:wave" + class ChemControllerSetpoint(PoolEquipmentEntity, NumberEntity): """Chemistry setpoint for ORP or pH""" def __init__(