Skip to content

Commit

Permalink
Merge pull request #36 from Crewski/flow-detected
Browse files Browse the repository at this point in the history
Add flow detected sensor for Chem Controller
  • Loading branch information
Crewski authored Jul 6, 2023
2 parents 14a3ae0 + 9177865 commit 3c06c2c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
15 changes: 15 additions & 0 deletions custom_components/njspc_ha/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
65 changes: 65 additions & 0 deletions custom_components/njspc_ha/chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
SensorEntity,
SensorStateClass,
)
from homeassistant.components.binary_sensor import (
BinarySensorEntity
)
from homeassistant.components.number import (
NumberEntity,
NumberMode
Expand All @@ -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__(
Expand Down

0 comments on commit 3c06c2c

Please sign in to comment.