Skip to content

Commit

Permalink
Added color_mode for lights, added ClimateEntityFeature/TURN_ON/OFF, …
Browse files Browse the repository at this point in the history
…changed strenum import
  • Loading branch information
David Naylor committed Apr 8, 2024
1 parent 4c1705c commit 7f315bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
21 changes: 17 additions & 4 deletions custom_components/njspc_ha/bodies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Platform for body integration."""

from __future__ import annotations

from typing import Any
Expand Down Expand Up @@ -26,7 +27,7 @@
)
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorDeviceClass
BinarySensorDeviceClass,
)

from .entity import PoolEquipmentEntity
Expand Down Expand Up @@ -423,6 +424,8 @@ def icon(self) -> str:
class BodyHeater(PoolEquipmentEntity, ClimateEntity):
"""Climate entity for njsPC-HA"""

_enable_turn_on_off_backwards_compatibility = False

# When thinking about heaters for pool/spa you need to not think of it so much
# like a home thermostat. While a home thermostat can be made to work
# the difference becomes a struggle to overcome.
Expand Down Expand Up @@ -456,6 +459,7 @@ def __init__(self, coordinator, body, heatmodes, units, has_cooling) -> None:
self.heat_mode = None
self.heat_status = None
self.body_temperature = None

if "setPoint" in body:
self.heat_setpoint = body["setPoint"]
if "coolSetpoint" in body:
Expand Down Expand Up @@ -627,10 +631,18 @@ def hvac_action(self) -> HVACAction:
def supported_features(self) -> ClimateEntityFeature:
if len(self._heatmodes) <= 2 and self._has_cooling is True:
# only 1 heater that supports cooling
return ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
return (
ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
elif len(self._heatmodes) <= 2:
# only 1 heater that doesn't support cooling
return ClimateEntityFeature.TARGET_TEMPERATURE
return (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
elif self._has_cooling is True:
# multiple heaters that support cooling
return (
Expand Down Expand Up @@ -662,6 +674,7 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
njspc_value = next(
(k for k, v in self._heatmodes.items() if v.lower() == "off"), None
)

else:
njspc_value = next(
(k for k, v in self._heatmodes.items() if v.lower() != "off"), None
Expand Down Expand Up @@ -747,7 +760,7 @@ def icon(self) -> str:
if self._value is True:
return "mdi:arrow-down-drop-circle"
return "mdi:arrow-up-drop-circle-outline"

@property
def device_class(self) -> BinarySensorDeviceClass | None:
return BinarySensorDeviceClass.DOOR
6 changes: 3 additions & 3 deletions custom_components/njspc_ha/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Constants for the njsPC-HA integration."""

from homeassistant.backports.enum import StrEnum
import enum


DOMAIN = "njspc_ha"
Expand Down Expand Up @@ -65,7 +65,7 @@
MAX_FLOW = "maxFlow"


class PoolEquipmentClass(StrEnum):
class PoolEquipmentClass(enum.StrEnum):
"""Class for pool equipment."""

CONTROL_PANEL = "control_panel"
Expand Down Expand Up @@ -111,7 +111,7 @@ class PoolEquipmentClass(StrEnum):
"""A pool equipment filter"""


class PoolEquipmentModel(StrEnum):
class PoolEquipmentModel(enum.StrEnum):
"""Model descriptions for pool equipment."""

CONTROL_PANEL = "Control Panel"
Expand Down
45 changes: 35 additions & 10 deletions custom_components/njspc_ha/light.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
"""Platform for light integration."""

from __future__ import annotations

from typing import Any

from .entity import PoolEquipmentEntity
from homeassistant.components.light import ATTR_EFFECT, LightEntity, LightEntityFeature
from homeassistant.components.light import (
ATTR_EFFECT,
ColorMode,
LightEntity,
LightEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import (
PoolEquipmentClass,
API_CIRCUIT_SETSTATE,
API_CIRCUIT_SETTHEME,
API_LIGHTGROUP_SETSTATE,
Expand All @@ -19,7 +23,10 @@
EVENT_AVAILABILITY,
EVENT_CIRCUIT,
EVENT_LIGHTGROUP,
PoolEquipmentClass,
)
from .entity import PoolEquipmentEntity


async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -63,6 +70,7 @@ async def async_setup_entry(
if new_devices:
async_add_entities(new_devices)


class CircuitLight(PoolEquipmentEntity, LightEntity):
"""Light entity for njsPC-HA."""

Expand All @@ -76,11 +84,19 @@ def __init__(
"""Initialize the light."""
match equipment_class:
case PoolEquipmentClass.LIGHT:
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.LIGHT, data=circuit)
super().__init__(
coordinator=coordinator,
equipment_class=PoolEquipmentClass.LIGHT,
data=circuit,
)
self._event = EVENT_CIRCUIT
self._command = API_CIRCUIT_SETSTATE
case PoolEquipmentClass.LIGHT_GROUP:
super().__init__(coordinator=coordinator, equipment_class=PoolEquipmentClass.LIGHT_GROUP, data=circuit)
super().__init__(
coordinator=coordinator,
equipment_class=PoolEquipmentClass.LIGHT_GROUP,
data=circuit,
)
self._event = EVENT_LIGHTGROUP
self._command = API_LIGHTGROUP_SETSTATE
self._lightthemes = lightthemes
Expand Down Expand Up @@ -173,7 +189,7 @@ def effect(self) -> str:

@property
def effect_list(self) -> list[str]:
"""Get list of effects"""
"""Get list of effects."""
if len(self._lightthemes) > 0:
_effects = []
for effect in self._lightthemes.values():
Expand All @@ -183,8 +199,17 @@ def effect_list(self) -> list[str]:

@property
def supported_features(self) -> LightEntityFeature:
"""See if light has effects"""

"""See if light has effects."""
if len(self._lightthemes) > 0:
return LightEntityFeature.EFFECT
return 0
return LightEntityFeature(0)

@property
def color_mode(self) -> ColorMode:
"""Get color mode, always ONOFF for njs-PC."""
return ColorMode.ONOFF

@property
def supported_color_modes(self) -> set[ColorMode]:
"""Color mode list, only ONOFF for njs-PC."""
return {ColorMode.ONOFF}

0 comments on commit 7f315bc

Please sign in to comment.