Skip to content

Commit

Permalink
Merge pull request #3 from DanielNagy/Solar-metrics
Browse files Browse the repository at this point in the history
Add Solar metrics
  • Loading branch information
DanielNagy authored Feb 14, 2024
2 parents a692f3b + 6c7f3a5 commit 9abebb3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 55 deletions.
47 changes: 26 additions & 21 deletions custom_components/astralpool_halo_chlorinator/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
)
}

SOLAR_BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
"SolarPumpState": BinarySensorEntityDescription(
key="SolarPumpState",
device_class=BinarySensorDeviceClass.RUNNING,
icon="mdi:pump",
name="Solar Pump On",
)
}


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -75,12 +84,24 @@ async def async_setup_entry(
data: ChlorinatorData = hass.data[DOMAIN][entry.entry_id]
coordinator = data.coordinator

async def add_heater_binary_sensor_callback():
await add_heater_binary_sensors(
hass, coordinator.added_entities, async_add_entities, coordinator
)
async def add_binary_sensor_callback(sensor_type):
binary_sensor_types_dict = {
"SolarEnabled": SOLAR_BINARY_SENSOR_TYPES,
"HeaterEnabled": HEATER_BINARY_SENSOR_TYPES,
}
sensor_descs = binary_sensor_types_dict.get(sensor_type, {})

new_entities = []
for sensor_type, sensor_desc in sensor_descs.items():
unique_id = f"hchlor_{sensor_type}".lower()
if unique_id not in coordinator.added_entities:
new_entities.append(HeaterBinarySensor(coordinator, sensor_desc))
coordinator.added_entities.add(unique_id)

if new_entities:
async_add_entities(new_entities)

coordinator.add_heater_binary_sensor_callback = add_heater_binary_sensor_callback
coordinator.add_binary_sensor_callback = add_binary_sensor_callback
await coordinator.async_config_entry_first_refresh()

entities = [
Expand All @@ -90,22 +111,6 @@ async def add_heater_binary_sensor_callback():
async_add_entities(entities)


async def add_heater_binary_sensors(
hass, added_entities, async_add_entities, coordinator
):
"""Setup Heater sensors if enabled"""

new_entities = []
for sensor_type, sensor_desc in HEATER_BINARY_SENSOR_TYPES.items():
unique_id = f"hchlor_{sensor_type}".lower()
if unique_id not in added_entities:
new_entities.append(HeaterBinarySensor(coordinator, sensor_desc))
added_entities.add(unique_id)

if new_entities:
async_add_entities(new_entities)


class ChlorinatorBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of a Clorinator binary sensor."""

Expand Down
27 changes: 16 additions & 11 deletions custom_components/astralpool_halo_chlorinator/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self, hass: HomeAssistant, chlorinator: HaloChlorinatorAPI) -> None
name="HCHLOR",
)
self.added_entities = set()
self.add_heater_sensor_callback = None
self.add_heater_binary_sensor_callback = None
self.add_sensor_callback = None
self.add_binary_sensor_callback = None
self.add_heater_select_callback = None

async def _async_update_data(self):
Expand All @@ -54,18 +54,23 @@ async def _async_update_data(self):
self.data = data
self._data_age = 0

if "SolarEnabled" in data and data["SolarEnabled"] == 1:
_LOGGER.debug("SolarEnabled : %s", data["SolarEnabled"])
if hasattr(self, "add_sensor_callback"):
await self.add_sensor_callback("SolarEnabled")
if hasattr(self, "add_binary_sensor_callback"):
await self.add_binary_sensor_callback("SolarEnabled")

if "HeaterEnabled" in data and data["HeaterEnabled"] == 1:
_LOGGER.debug("HeaterEnabled : %s", data["HeaterEnabled"])
if (
self.add_heater_sensor_callback
and self.add_heater_binary_sensor_callback
and self.add_heater_select_callback is not None
):
await self.add_heater_sensor_callback()
await self.add_heater_binary_sensor_callback()
if hasattr(self, "add_sensor_callback"):
await self.add_sensor_callback("HeaterEnabled")

if hasattr(self, "add_binary_sensor_callback"):
await self.add_binary_sensor_callback("HeaterEnabled")

if hasattr(self, "add_heater_select_callback"):
await self.add_heater_select_callback()
else:
_LOGGER.warning("add_heater_callback(s) not set")

if "PoolSpaEnabled" in data and data["PoolSpaEnabled"] == 1:
_LOGGER.debug("PoolSpaEnabled : %s", data["PoolSpaEnabled"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"bluetooth-data-tools>=0.4.0",
"pychlorinator>=0.2.9"
],
"version": "0.1.5"
"version": "0.1.6"
}
70 changes: 48 additions & 22 deletions custom_components/astralpool_halo_chlorinator/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from homeassistant import config_entries
from homeassistant.components.sensor import (
EntityCategory,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -119,11 +119,12 @@
native_unit_of_measurement="mL",
device_class=None,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"WaterTemp": SensorEntityDescription(
key="WaterTemp",
icon="mdi:temperature-celsius",
name="Water Temp",
name="Water Temperature",
native_unit_of_measurement="°C",
device_class="temperature",
state_class=SensorStateClass.MEASUREMENT,
Expand Down Expand Up @@ -153,6 +154,7 @@
native_unit_of_measurement="%",
device_class=None,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
}

Expand All @@ -166,6 +168,33 @@
)
}

SOLAR_SENSOR_TYPES: dict[str, SensorEntityDescription] = {
"SolarRoof": SensorEntityDescription(
key="SolarRoof",
icon="mdi:temperature-celsius",
name="Solar Roof Temperature",
native_unit_of_measurement="°C",
device_class="temperature",
state_class=SensorStateClass.MEASUREMENT,
),
"SolarWater": SensorEntityDescription(
key="SolarWater",
icon="mdi:temperature-celsius",
name="Solar Water Temperature",
native_unit_of_measurement="°C",
device_class="temperature",
state_class=SensorStateClass.MEASUREMENT,
),
"SolarMode": SensorEntityDescription(
key="SolarMode",
icon="mdi:solar-power-variant",
name="Solar Mode",
native_unit_of_measurement=None,
device_class=SensorDeviceClass.ENUM,
state_class=None,
),
}


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -176,36 +205,33 @@ async def async_setup_entry(
data: ChlorinatorData = hass.data[DOMAIN][entry.entry_id]
coordinator = data.coordinator

async def add_heater_sensor_callback():
await add_heater_sensors(
hass, coordinator.added_entities, async_add_entities, coordinator
)
async def add_sensor_callback(sensor_type):
sensor_types_dict = {
"SolarEnabled": SOLAR_SENSOR_TYPES,
"HeaterEnabled": HEATER_SENSOR_TYPES,
}
sensor_descs = sensor_types_dict.get(sensor_type, {})

coordinator.add_heater_sensor_callback = add_heater_sensor_callback
new_entities = []
for sensor_type, sensor_desc in sensor_descs.items():
unique_id = f"hchlor_{sensor_type}".lower()
if unique_id not in coordinator.added_entities:
new_entities.append(HeaterSensor(coordinator, sensor_desc))
coordinator.added_entities.add(unique_id)

if new_entities:
async_add_entities(new_entities)

coordinator.add_sensor_callback = add_sensor_callback
await coordinator.async_config_entry_first_refresh()

entities = [
ChlorinatorSensor(data.coordinator, sensor_desc)
for sensor_desc in CHLORINATOR_SENSOR_TYPES
# if sensorDesc in CHLORINATOR_SENSOR_TYPES
]
async_add_entities(entities)


async def add_heater_sensors(hass, added_entities, async_add_entities, coordinator):
"""Setup Heater sensors if enabled"""

new_entities = []
for sensor_type, sensor_desc in HEATER_SENSOR_TYPES.items():
unique_id = f"hchlor_{sensor_type}".lower()
if unique_id not in added_entities:
new_entities.append(HeaterSensor(coordinator, sensor_desc))
added_entities.add(unique_id)

if new_entities:
async_add_entities(new_entities)


class ChlorinatorSensor(
CoordinatorEntity[ChlorinatorDataUpdateCoordinator], SensorEntity
):
Expand Down

0 comments on commit 9abebb3

Please sign in to comment.