Skip to content

Commit

Permalink
Added body cover sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Crewski committed May 16, 2023
1 parent cbad89a commit e40d007
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
109 changes: 100 additions & 9 deletions custom_components/njspc_ha/bodies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from typing import Any


from homeassistant.const import UnitOfTemperature, UnitOfPressure, ATTR_TEMPERATURE, PERCENTAGE
from homeassistant.const import (
UnitOfTemperature,
UnitOfPressure,
ATTR_TEMPERATURE,
PERCENTAGE,
)

from homeassistant.components.switch import SwitchEntity
from homeassistant.components.climate import (
Expand All @@ -19,7 +24,9 @@
SensorStateClass,
SensorDeviceClass,
)
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
)

from .entity import PoolEquipmentEntity
from .__init__ import NjsPCHAdata
Expand All @@ -46,12 +53,18 @@
"hpcool": HVACAction.COOLING,
"cooldown": HVACAction.OFF,
}


class FilterOnSensor(PoolEquipmentEntity, BinarySensorEntity):
"""The current running state for a pump"""

def __init__(self, coordinator: NjsPCHAdata, pool_filter: Any) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.FILTER, data=pool_filter)
super().__init__(
coordinator=coordinator,
equipment_class=PoolEquipmentClass.FILTER,
data=pool_filter,
)
self._value = False
if "isOn" in pool_filter:
self._value = pool_filter["isOn"]
Expand Down Expand Up @@ -104,12 +117,17 @@ def icon(self) -> str:
return "mdi:filter"
return "mdi:filter-off"


class FilterCleanSensor(PoolEquipmentEntity, SensorEntity):
"""Sensor for filter clean percentage"""

def __init__(self, coordinator: NjsPCHAdata, pool_filter: Any) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.FILTER, data=pool_filter)
super().__init__(
coordinator=coordinator,
equipment_class=PoolEquipmentClass.FILTER,
data=pool_filter,
)
self._value = None
if "cleanPercentage" in pool_filter:
self._value = pool_filter["cleanPercentage"]
Expand Down Expand Up @@ -162,12 +180,17 @@ def device_class(self) -> SensorDeviceClass | None:
def native_unit_of_measurement(self) -> str | UnitOfTemperature | None:
return PERCENTAGE


class FilterPressureSensor(PoolEquipmentEntity, SensorEntity):
"""Sensor for filter pressure"""

def __init__(self, coordinator: NjsPCHAdata, pool_filter: Any) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.FILTER, data=pool_filter)
super().__init__(
coordinator=coordinator,
equipment_class=PoolEquipmentClass.FILTER,
data=pool_filter,
)
self._value = 0
self._units = "psi"
if "pressure" in pool_filter:
Expand All @@ -186,7 +209,10 @@ def _handle_coordinator_update(self) -> None:
):
if "pressure" in self.coordinator.data:
self._value = self.coordinator.data["pressure"]
if "pressureUnits" in self.coordinator.data and "name" in self.coordinator.data["pressureUnits"]:
if (
"pressureUnits" in self.coordinator.data
and "name" in self.coordinator.data["pressureUnits"]
):
self._units = self.coordinator.data["pressureUnits"]["name"]
self.async_write_ha_state()
elif self.coordinator.data["event"] == EVENT_AVAILABILITY:
Expand Down Expand Up @@ -237,12 +263,15 @@ def native_unit_of_measurement(self) -> str | UnitOfTemperature | None:
case _:
return UnitOfPressure.PSI


class BodyTempSensor(PoolEquipmentEntity, SensorEntity):
"""Body Temp Sensor for njsPC-HA"""

def __init__(self, coordinator: NjsPCHAdata, units: str, body: Any) -> None:
"""Initialize the sensor."""
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body)
super().__init__(
coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body
)
self._units = units
self._value = None
if "temp" in body:
Expand Down Expand Up @@ -303,6 +332,7 @@ def native_unit_of_measurement(self) -> str | UnitOfTemperature | None:
return UnitOfTemperature.FAHRENHEIT
return UnitOfTemperature.CELSIUS


class BodyCircuitSwitch(PoolEquipmentEntity, SwitchEntity):
"""Body Circuit switch for njsPC-HA"""

Expand All @@ -315,7 +345,9 @@ def __init__(
"""Initialize the sensor."""
# Need to add delays to this at some point. We will get
# delay messages when this happens
super().__init__(coordinator=coordinator,equipment_class=PoolEquipmentClass.BODY, data=body)
super().__init__(
coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body
)
self.body_type = "pool"
if "type" in body:
self.body_type = body["type"]["name"]
Expand Down Expand Up @@ -386,6 +418,7 @@ def icon(self) -> str:
else:
return "mdi:toggle-switch-variant"


class BodyHeater(PoolEquipmentEntity, ClimateEntity):
"""Climate entity for njsPC-HA"""

Expand All @@ -410,7 +443,9 @@ class BodyHeater(PoolEquipmentEntity, ClimateEntity):
# This is not the heater device and we may add it later to show diagnostic data.
def __init__(self, coordinator, body, heatmodes, units, has_cooling) -> None:
"""Initialize the body heater."""
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body)
super().__init__(
coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body
)
self._heatmodes = heatmodes
self._units = units
self._available = True
Expand Down Expand Up @@ -654,3 +689,59 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
return
data = {"id": self.equipment_id, "mode": njspc_value}
await self.coordinator.api.command(url=API_SET_HEATMODE, data=data)


class BodyCoveredSensor(PoolEquipmentEntity, BinarySensorEntity):
"""The current status of a body cover"""

def __init__(self, coordinator: NjsPCHAdata, body: Any) -> None:
"""Initialize the sensor."""
super().__init__(
coordinator=coordinator, equipment_class=PoolEquipmentClass.BODY, data=body
)
self._value = False
if "isCovered" in body:
self._value = body["isCovered"]
self._available = True

def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if (
self.coordinator.data["event"] == EVENT_BODY
and self.coordinator.data["id"] == self.equipment_id
):
if "isCovered" in self.coordinator.data:
self._value = self.coordinator.data["isCovered"]
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 "Cover"

@property
def unique_id(self) -> str | None:
"""ID of the sensor"""
return f"{self.coordinator.controller_id}_{self.equipment_class}_{self.equipment_id}_isCovered"

@property
def is_on(self) -> bool:
"""Return if the body is covered."""
return self._value

@property
def icon(self) -> str:
if self._value is True:
return "mdi:arrow-down-drop-circle"
return "mdi:arrow-up-drop-circle-outline"
6 changes: 5 additions & 1 deletion custom_components/njspc_ha/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)
from .pumps import PumpPowerSensor, PumpFlowSensor, PumpSpeedSensor
from .controller import PanelModeSensor, TempProbeSensor
from .bodies import BodyTempSensor, FilterPressureSensor, FilterCleanSensor
from .bodies import BodyTempSensor, FilterPressureSensor, FilterCleanSensor, BodyCoveredSensor
from .const import (
PoolEquipmentClass,
PoolEquipmentModel,
Expand Down Expand Up @@ -82,6 +82,10 @@ async def async_setup_entry(
new_devices.append(
BodyTempSensor(coordinator=coordinator, units=units, body=body)
)
if "isCovered" in body:
new_devices.append(
BodyCoveredSensor(coordinator=coordinator, body=body)
)

for pump in config["pumps"]:
# Pump sensors vary by type. This may need a re-visit for pump types that use a
Expand Down

0 comments on commit e40d007

Please sign in to comment.