Skip to content

Commit

Permalink
feat: experimental spillover switch, ref: #67
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptk committed Nov 17, 2023
1 parent a052a80 commit 6e65bcb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"recommendations": ["esbenp.prettier-vscode", "ms-python.python"]
"recommendations": [
"ms-python.python",
"ms-python.black-formatter",
"ms-python.mypy-type-checker",
"esbenp.prettier-vscode",
]
}
9 changes: 4 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"editor.rulers": [140],
// Please keep this file in sync with settings in home-assistant/.devcontainer/devcontainer.json
"python.formatting.provider": "black",
// https://code.visualstudio.com/docs/python/testing#_pytest-configuration-settings
"python.testing.pytestEnabled": false,
"python.linting.mypyEnabled": true,
// "python.analysis.typeCheckingMode": "basic"
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
}
65 changes: 63 additions & 2 deletions custom_components/omnilogic_local/switch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar, cast

from pyomnilogic_local.models.telemetry import TelemetryFilter
from pyomnilogic_local.types import (
BodyOfWaterType,
FilterState,
FilterValvePosition,
OmniType,
PumpState,
RelayFunction,
Expand All @@ -18,19 +21,22 @@
from .const import DOMAIN, KEY_COORDINATOR
from .entity import OmniLogicEntity
from .types.entity_index import (
EntityIndexBodyOfWater,
EntityIndexChlorinator,
EntityIndexFilter,
EntityIndexPump,
EntityIndexRelay,
EntityIndexValveActuator,
)
from .utils import get_entities_of_hass_type
from .utils import get_entities_of_hass_type, get_entities_of_omni_types

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .coordinator import OmniLogicCoordinator


_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -77,6 +83,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
)
entities.append(OmniLogicChlorinatorSwitchEntity(coordinator=coordinator, context=system_id))

# Add switches for spillover into pools if supported
all_bows = get_entities_of_omni_types(coordinator.data, [OmniType.BOW])
for system_id, bow in all_bows.items():
match bow.msp_config.type:
case BodyOfWaterType.POOL:
if bow.msp_config.supports_spillover == "yes":
_LOGGER.debug(
"Configuring switch for spillover with ID: %s, Name: %s",
bow.msp_config.system_id,
bow.msp_config.name,
)
entities.append(OmniLogicSpilloverSwitchEntity(coordinator=coordinator, context=system_id))

async_add_entities(entities)


Expand Down Expand Up @@ -281,3 +300,45 @@ async def async_turn_off(self, **kwargs: Any) -> None:
_LOGGER.debug("turning off chlorinator ID: %s", self.system_id)
await self.coordinator.omni_api.async_set_chlorinator_enable(self.bow_id, False)
self.set_telemetry({"enable": False})


class OmniLogicSpilloverSwitchEntity(OmniLogicEntity[EntityIndexBodyOfWater], SwitchEntity):
"""An entity using CoordinatorEntity.
The CoordinatorEntity class provides:
should_poll
async_update
async_added_to_hass
available
"""

_attr_name = "Spillover"

def __init__(self, coordinator: OmniLogicCoordinator, context: int) -> None:
super().__init__(coordinator, context)
# This is all a little gross, and it means that we can only support one filter system per BoW, but I believe that is a limitation of
# the Omni system anyway. They can have multiple pumps, but only one "filter"
all_filters = get_entities_of_omni_types(coordinator.data, [OmniType.FILTER])
bow_filter = {
system_id: bow_filter for (system_id, bow_filter) in all_filters.items() if bow_filter.msp_config.bow_id == self.bow_id
}
self.filter_system_id = list(bow_filter.keys())[0]

@property
def icon(self) -> str | None:
return "mdi:toggle-switch-variant" if self.is_on else "mdi:toggle-switch-variant-off"

@property
def is_on(self) -> bool | None:
return cast(TelemetryFilter, self.get_telemetry_by_systemid(self.filter_system_id)).valve_position == FilterValvePosition.SPILLOVER

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
_LOGGER.debug("turning on spillover ID: %s", self.system_id)
await self.coordinator.omni_api.async_set_spillover(self.bow_id, 75)

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
_LOGGER.debug("turning off spillover ID: %s", self.system_id)
await self.coordinator.omni_api.async_set_spillover(self.bow_id, 0)

0 comments on commit 6e65bcb

Please sign in to comment.