diff --git a/custom_components/astralpool_halo_chlorinator/binary_sensor.py b/custom_components/astralpool_halo_chlorinator/binary_sensor.py index ded5128..023d657 100644 --- a/custom_components/astralpool_halo_chlorinator/binary_sensor.py +++ b/custom_components/astralpool_halo_chlorinator/binary_sensor.py @@ -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, @@ -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 = [ @@ -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.""" diff --git a/custom_components/astralpool_halo_chlorinator/coordinator.py b/custom_components/astralpool_halo_chlorinator/coordinator.py index fde0186..877d118 100644 --- a/custom_components/astralpool_halo_chlorinator/coordinator.py +++ b/custom_components/astralpool_halo_chlorinator/coordinator.py @@ -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): @@ -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"]) diff --git a/custom_components/astralpool_halo_chlorinator/manifest.json b/custom_components/astralpool_halo_chlorinator/manifest.json index 161497c..e6b918d 100644 --- a/custom_components/astralpool_halo_chlorinator/manifest.json +++ b/custom_components/astralpool_halo_chlorinator/manifest.json @@ -21,5 +21,5 @@ "bluetooth-data-tools>=0.4.0", "pychlorinator>=0.2.9" ], - "version": "0.1.5" + "version": "0.1.6" } \ No newline at end of file diff --git a/custom_components/astralpool_halo_chlorinator/sensor.py b/custom_components/astralpool_halo_chlorinator/sensor.py index 03154ad..5bbae4c 100644 --- a/custom_components/astralpool_halo_chlorinator/sensor.py +++ b/custom_components/astralpool_halo_chlorinator/sensor.py @@ -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 @@ -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, @@ -153,6 +154,7 @@ native_unit_of_measurement="%", device_class=None, state_class=SensorStateClass.MEASUREMENT, + entity_category=EntityCategory.DIAGNOSTIC, ), } @@ -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, @@ -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 ):