Skip to content

Commit

Permalink
Add Heater Mode selector
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNagy committed Feb 10, 2024
1 parent d7c3b98 commit 0a32e48
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
8 changes: 7 additions & 1 deletion custom_components/astralpool_halo_chlorinator/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, hass: HomeAssistant, chlorinator: HaloChlorinatorAPI) -> None
self.added_entities = set()
self.add_heater_sensor_callback = None
self.add_heater_binary_sensor_callback = None
self.add_heater_select_callback = None

async def _async_update_data(self):
"""Fetch data from API endpoint."""
Expand All @@ -57,13 +58,18 @@ async def _async_update_data(self):
_LOGGER.debug("HeaterEnabled : %s", data["HeaterEnabled"])
if (
self.add_heater_sensor_callback
and self.add_heater_binary_sensor_callback is not None
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()
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"])

elif self._data_age >= 15: # 15 polling events = 5 minutes
self.data = {}
_LOGGER.error("Failed _gatherdata, giving up: %s", self._data_age)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"iot_class": "local_polling",
"requirements": [
"bluetooth-data-tools>=0.4.0",
"pychlorinator>=0.2.8"
"pychlorinator>=0.2.9"
],
"version": "0.1.0"
}
69 changes: 69 additions & 0 deletions custom_components/astralpool_halo_chlorinator/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,24 @@ async def async_setup_entry(
) -> None:
"""Set up Chlorinator from a config entry."""
data: ChlorinatorData = hass.data[DOMAIN][entry.entry_id]
coordinator = data.coordinator
entities = [
ChlorinatorModeSelect(data.coordinator),
# HeaterModeSelect(data.coordinator),
# ChlorinatorSpeedSelect(data.coordinator),
]

async def add_heater_select_callback(): # Callback renamed and no parameters
unique_id = (
"hchlor_heater_mode_select" # Example unique ID for HeaterModeSelect
)
if unique_id not in coordinator.added_entities:
async_add_entities([HeaterModeSelect(coordinator)], update_before_add=True)
coordinator.added_entities.add(unique_id) # Mark as added

coordinator.add_heater_select_callback = add_heater_select_callback
await coordinator.async_config_entry_first_refresh()

async_add_entities(entities)


Expand Down Expand Up @@ -104,6 +118,61 @@ async def async_select_option(self, option: str) -> None:
await self.coordinator.async_request_refresh()


class HeaterModeSelect(
CoordinatorEntity[ChlorinatorDataUpdateCoordinator], SelectEntity
):
"""Representation of a Clorinator Select entity."""

_attr_icon = "mdi:power"
_attr_options = ["Off", "On"]
_attr_name = "Heater Mode"
_attr_unique_id = "HCHLOR_heater_onoff_select"

def __init__(
self,
coordinator: ChlorinatorDataUpdateCoordinator,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)

@property
def device_info(self) -> DeviceInfo | None:
return {
"identifiers": {(DOMAIN, "HCHLOR")},
"name": "HCHLOR",
"model": "Halo Chlor",
"manufacturer": "Astral Pool",
}

@property
def current_option(self):
mode = self.coordinator.data.get("HeaterMode")

if mode is halo_parsers.HeaterStateCharacteristic.HeaterModeValues.Off:
return "Off"
# elif mode is halo_parsers.HeaterStateCharacteristic.HeaterModeValues.Auto:
# return "Auto"
elif mode is halo_parsers.HeaterStateCharacteristic.HeaterModeValues.On:
return "On"

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
action: halo_parsers.HeaterAppActions.NoAction
if option == "Off":
action = halo_parsers.HeaterAppActions.HeaterOff
# elif option == "Auto":
# action = halo_parsers.HeaterAppActions.Auto
elif option == "On":
action = halo_parsers.HeaterAppActions.HeaterOn
else:
action = halo_parsers.HeaterAppActions.NoAction

_LOGGER.debug("Select Heater entity state changed to %s", action)
await self.coordinator.chlorinator.async_write_heater_action(action)
await asyncio.sleep(1)
await self.coordinator.async_request_refresh()


class ChlorinatorSpeedSelect(
CoordinatorEntity[ChlorinatorDataUpdateCoordinator], SelectEntity
):
Expand Down

0 comments on commit 0a32e48

Please sign in to comment.