From 56db748a05333a27919331cb61164fbb0dabc62f Mon Sep 17 00:00:00 2001 From: Scott Phillips Date: Sat, 6 Jan 2024 07:55:19 +1100 Subject: [PATCH 1/2] Current working code --- custom_components/balboa/climate.py | 4 +- custom_components/balboa/const.py | 7 +++- custom_components/balboa/fan.py | 61 +++++++++++++++++------------ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/custom_components/balboa/climate.py b/custom_components/balboa/climate.py index 4b1a03c..870f0fe 100644 --- a/custom_components/balboa/climate.py +++ b/custom_components/balboa/climate.py @@ -166,7 +166,7 @@ def preset_mode(self): return self._client.get_heatmode(True) @property - def device_state_attributes(self): + def extra_state_attributes(self): """Return device specific state attributes.""" return { "time": f"{self._client.time_hour:02d}:{self._client.time_minute:02d}", @@ -216,4 +216,4 @@ def get_temp_unit(self): """Return the balboa equivalent temperature unit of the system.""" if self.hass.config.units.temperature_unit == TEMP_CELSIUS: return self._client.TSCALE_C - return self._client.TSCALE_F + return self._client.TSCALE_F \ No newline at end of file diff --git a/custom_components/balboa/const.py b/custom_components/balboa/const.py index 00a814f..7fcd580 100644 --- a/custom_components/balboa/const.py +++ b/custom_components/balboa/const.py @@ -9,7 +9,6 @@ HVAC_MODE_HEAT, HVAC_MODE_OFF, ) -from homeassistant.components.fan import SPEED_HIGH, SPEED_LOW, SPEED_OFF _LOGGER = logging.getLogger(__name__) @@ -19,7 +18,6 @@ CLIMATE_SUPPORTED_MODES = [HVAC_MODE_HEAT, HVAC_MODE_OFF] CONF_SYNC_TIME = "sync_time" DEFAULT_SYNC_TIME = False -FAN_SUPPORTED_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_HIGH] PLATFORMS = ["binary_sensor", "climate", "fan", "switch"] SPA = "spa" UNSUB = "unsub" @@ -32,3 +30,8 @@ MISTER = "Mister" PUMP = "Pump" TEMP_RANGE = "Temp Range" + +FAN_PRESET_MODE_OFF = "Off" +FAN_PRESET_MODE_LOW = "Low" +FAN_PRESET_MODE_HIGH = "High" +FAN_SUPPORTED_PRESET_MODES = [FAN_PRESET_MODE_OFF, FAN_PRESET_MODE_LOW, FAN_PRESET_MODE_HIGH] \ No newline at end of file diff --git a/custom_components/balboa/fan.py b/custom_components/balboa/fan.py index 4445764..d20a122 100644 --- a/custom_components/balboa/fan.py +++ b/custom_components/balboa/fan.py @@ -1,14 +1,12 @@ """Support for Balboa Spa Pumps.""" from homeassistant.components.fan import ( - SPEED_LOW, - SPEED_OFF, - SUPPORT_SET_SPEED, + SUPPORT_PRESET_MODE, FanEntity, ) from . import BalboaEntity -from .const import _LOGGER, DOMAIN, FAN_SUPPORTED_SPEEDS, PUMP, SPA - +from .const import _LOGGER, DOMAIN, FAN_PRESET_MODE_OFF, FAN_PRESET_MODE_LOW, FAN_SUPPORTED_PRESET_MODES, PUMP, SPA +from typing import Any, final async def async_setup_entry(hass, entry, async_add_entities): """Set up the spa's pumps as FAN entities.""" @@ -28,29 +26,40 @@ class BalboaSpaPump(BalboaEntity, FanEntity): def __init__(self, hass, entry, key, states): """Initialize the pump.""" super().__init__(hass, entry, PUMP, key) - self._speed_list = FAN_SUPPORTED_SPEEDS if states > 1 else None - self._supported_features = SUPPORT_SET_SPEED if states > 1 else 0 + self._preset_modes = FAN_SUPPORTED_PRESET_MODES if states > 1 else None + self._supported_features = SUPPORT_PRESET_MODE if states > 1 else 0 - async def async_set_speed(self, speed: str) -> None: + async def async_set_preset_mode(self, preset_mode: str) -> None: """Set speed of pump.""" - setto = FAN_SUPPORTED_SPEEDS.index(speed) - _LOGGER.debug(f"set {self.name} speed to {speed}") + setto = FAN_SUPPORTED_PRESET_MODES.index(preset_mode) + _LOGGER.debug(f"set {self.name} speed to {preset_mode}") await self._client.change_pump(self._num - 1, setto) - async def async_turn_on(self, speed: str = None, **kwargs) -> None: - """Turn on pump.""" - if speed is None: - speed = SPEED_LOW - await self.async_set_speed(speed) + # async def async_turn_on(self, preset_mode: str = None, **kwargs) -> None: + # """Turn on pump.""" + # if preset_mode is None: + # preset_mode = FAN_PRESET_MODE_LOW + # await self.async_set_preset_mode(preset_mode) + + async def async_turn_on( + self, + percentage: int | None = None, + preset_mode: str | None = None, + **kwargs: Any, + ) -> None: + """Turn on pump.""" + if preset_mode is None: + preset_mode = FAN_PRESET_MODE_LOW + await self.async_set_preset_mode(preset_mode) async def async_turn_off(self, **kwargs) -> None: """Turn off pump.""" - await self.async_set_speed(SPEED_OFF) + await self.async_set_preset_mode(FAN_PRESET_MODE_OFF) @property - def speed_list(self) -> list: - """Get the list of available speeds.""" - return self._speed_list + def preset_modes(self) -> list: + """Get the list of available preset modes.""" + return self._preset_modes @property def supported_features(self) -> int: @@ -58,13 +67,13 @@ def supported_features(self) -> int: return self._supported_features @property - def speed(self) -> str: - """Return the current speed.""" + def preset_mode(self) -> str: + """Get the active preset mode.""" pstate = self._client.get_pump(self._num - 1) - _LOGGER.debug(f"{self.name} speed is {FAN_SUPPORTED_SPEEDS[pstate]}") - if pstate >= len(FAN_SUPPORTED_SPEEDS) or pstate < 0: - return SPEED_OFF - return FAN_SUPPORTED_SPEEDS[pstate] + _LOGGER.debug(f"{self.name} speed is {FAN_SUPPORTED_PRESET_MODES[pstate]}") + if pstate >= len(FAN_SUPPORTED_PRESET_MODES) or pstate < 0: + return FAN_PRESET_MODE_OFF + return FAN_SUPPORTED_PRESET_MODES[pstate] @property def is_on(self): @@ -75,4 +84,4 @@ def is_on(self): @property def icon(self): """Return the icon to use in the frontend, if any.""" - return "mdi:hydro-power" + return "mdi:hydro-power" \ No newline at end of file From cb944e58d15c657e541b3fa36e47e0f7bb5425f1 Mon Sep 17 00:00:00 2001 From: Scott Phillips Date: Sat, 6 Jan 2024 10:12:20 +1100 Subject: [PATCH 2/2] Fix up deprecated constants --- custom_components/balboa/binary_sensor.py | 4 ++-- custom_components/balboa/climate.py | 29 ++++++++++------------- custom_components/balboa/const.py | 6 ++--- custom_components/balboa/switch.py | 7 ++++-- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/custom_components/balboa/binary_sensor.py b/custom_components/balboa/binary_sensor.py index e786658..d322f7c 100644 --- a/custom_components/balboa/binary_sensor.py +++ b/custom_components/balboa/binary_sensor.py @@ -1,7 +1,7 @@ """Support for Balboa Spa binary sensors.""" from homeassistant.components.binary_sensor import ( - DEVICE_CLASS_MOVING, BinarySensorEntity, + BinarySensorDeviceClass, ) from . import BalboaEntity @@ -44,7 +44,7 @@ def is_on(self) -> bool: @property def device_class(self): """Return the class of this device, from component DEVICE_CLASSES.""" - return DEVICE_CLASS_MOVING + return BinarySensorDeviceClass.MOVING @property def icon(self): diff --git a/custom_components/balboa/climate.py b/custom_components/balboa/climate.py index 870f0fe..38d5681 100644 --- a/custom_components/balboa/climate.py +++ b/custom_components/balboa/climate.py @@ -4,18 +4,13 @@ from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate.const import ( - CURRENT_HVAC_HEAT, - CURRENT_HVAC_IDLE, FAN_HIGH, FAN_LOW, FAN_MEDIUM, FAN_OFF, - HVAC_MODE_AUTO, - HVAC_MODE_HEAT, - HVAC_MODE_OFF, - SUPPORT_FAN_MODE, - SUPPORT_PRESET_MODE, - SUPPORT_TARGET_TEMPERATURE, + ClimateEntityFeature, + HVACAction, + HVACMode, ) from homeassistant.const import ( ATTR_TEMPERATURE, @@ -45,10 +40,10 @@ class BalboaSpaClimate(BalboaEntity, ClimateEntity): @property def supported_features(self): """Return the list of supported features.""" - features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE + features = ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE if self._client.have_blower(): - features |= SUPPORT_FAN_MODE + features |= ClimateEntityFeature.FAN_MODE return features @@ -56,7 +51,7 @@ def supported_features(self): def hvac_modes(self) -> List[str]: """Return the list of supported HVAC modes.""" if self._client.get_heatmode() == self._client.HEATMODE_RNR: - return [*CLIMATE_SUPPORTED_MODES, HVAC_MODE_AUTO] + return [*CLIMATE_SUPPORTED_MODES, HVACMode.AUTO] else: return CLIMATE_SUPPORTED_MODES @@ -65,19 +60,19 @@ def hvac_mode(self) -> str: """Return the current HVAC mode.""" mode = self._client.get_heatmode() if mode == self._client.HEATMODE_READY: - return HVAC_MODE_HEAT + return HVACMode.HEAT elif mode == self._client.HEATMODE_RNR: - return HVAC_MODE_AUTO + return HVACMode.AUTO else: - return HVAC_MODE_OFF + return HVACMode.OFF @property def hvac_action(self) -> str: """Return the current operation mode.""" state = self._client.get_heatstate() if state >= self._client.ON: - return CURRENT_HVAC_HEAT - return CURRENT_HVAC_IDLE + return HVACAction.HEATING + return HVACAction.IDLE @property def fan_modes(self) -> List[str]: @@ -207,7 +202,7 @@ async def async_set_hvac_mode(self, hvac_mode): AUTO = Ready in Rest (can't be set, only reported) HEAT = Ready """ - if hvac_mode == HVAC_MODE_HEAT: + if hvac_mode == HVACMode.HEAT: await self._client.change_heatmode(self._client.HEATMODE_READY) else: await self._client.change_heatmode(self._client.HEATMODE_REST) diff --git a/custom_components/balboa/const.py b/custom_components/balboa/const.py index 7fcd580..2564c40 100644 --- a/custom_components/balboa/const.py +++ b/custom_components/balboa/const.py @@ -6,8 +6,8 @@ FAN_LOW, FAN_MEDIUM, FAN_OFF, - HVAC_MODE_HEAT, - HVAC_MODE_OFF, + HVACAction, + HVACMode, ) _LOGGER = logging.getLogger(__name__) @@ -15,7 +15,7 @@ DOMAIN = "balboa" CLIMATE_SUPPORTED_FANSTATES = [FAN_OFF, FAN_LOW, FAN_MEDIUM, FAN_HIGH] -CLIMATE_SUPPORTED_MODES = [HVAC_MODE_HEAT, HVAC_MODE_OFF] +CLIMATE_SUPPORTED_MODES = [HVACMode.HEAT, HVACMode.OFF] CONF_SYNC_TIME = "sync_time" DEFAULT_SYNC_TIME = False PLATFORMS = ["binary_sensor", "climate", "fan", "switch"] diff --git a/custom_components/balboa/switch.py b/custom_components/balboa/switch.py index b00ff78..bbba0c2 100644 --- a/custom_components/balboa/switch.py +++ b/custom_components/balboa/switch.py @@ -1,5 +1,8 @@ """Support for Balboa Spa switches.""" -from homeassistant.components.switch import DEVICE_CLASS_SWITCH, SwitchEntity +from homeassistant.components.switch import ( + SwitchEntity, + SwitchDeviceClass +) from . import BalboaEntity from .const import _LOGGER, AUX, DOMAIN, LIGHT, MISTER, SPA, TEMP_RANGE @@ -60,7 +63,7 @@ def is_on(self) -> bool: @property def device_class(self): """Return the class of this device, from component DEVICE_CLASSES.""" - return DEVICE_CLASS_SWITCH + return SwitchDeviceClass.SWITCH @property def icon(self):