Skip to content

Commit

Permalink
Further tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk committed Jan 9, 2024
1 parent e70c8fe commit 3dc2df9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
33 changes: 12 additions & 21 deletions custom_components/bestway/bestway/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BestwayDeviceType,
BestwayUserToken,
HydrojetBubbles,
HydrojetFilter,
HydrojetHeat,
)

Expand Down Expand Up @@ -315,23 +316,24 @@ async def hydrojet_spa_set_power(self, device_id: str, power: bool) -> None:
cached_state.attrs["heat"] = False
cached_state.attrs["wave"] = HydrojetBubbles.OFF

async def hydrojet_spa_set_filter(self, device_id: str, filtering: bool) -> None:
async def hydrojet_spa_set_filter(
self, device_id: str, filtering: HydrojetFilter
) -> None:
"""Turn the filter pump on/off on a spa device."""
if (cached_state := self._state_cache.get(device_id)) is None:
raise BestwayException(f"Device '{device_id}' is not recognised")

_LOGGER.debug("Setting filter mode to %s", "ON" if filtering else "OFF")
api_value = 2 if filtering else 0
await self._do_control_post(device_id, filter=2 if filtering else 0)
await self._do_control_post(device_id, filter=filtering)
cached_state.timestamp = int(time())
cached_state.attrs["filter"] = api_value
if filtering:
cached_state.attrs["filter"] = filtering
if filtering == HydrojetFilter.ON:
cached_state.attrs["power"] = True
else:
cached_state.attrs["wave"] = HydrojetBubbles.OFF
cached_state.attrs["heat"] = False

async def hydrojet_spa_set_heat(self, device_id: str, heat: bool) -> None:
async def hydrojet_spa_set_heat(self, device_id: str, heat: HydrojetHeat) -> None:
"""
Turn the heater on/off on a Hydrojet spa device.
Expand All @@ -341,13 +343,12 @@ async def hydrojet_spa_set_heat(self, device_id: str, heat: bool) -> None:
raise BestwayException(f"Device '{device_id}' is not recognised")

_LOGGER.debug("Setting heater mode to %s", "ON" if heat else "OFF")
api_value = HydrojetHeat.ON if heat else 0
await self._do_control_post(device_id, heat=api_value)
await self._do_control_post(device_id, heat=heat)
cached_state.timestamp = int(time())
cached_state.attrs["heat"] = api_value
if heat:
cached_state.attrs["heat"] = heat
if heat == HydrojetHeat.ON:
cached_state.attrs["power"] = True
cached_state.attrs["filter"] = True
cached_state.attrs["filter"] = HydrojetFilter.ON

async def hydrojet_spa_set_target_temp(
self, device_id: str, target_temp: int
Expand All @@ -361,16 +362,6 @@ async def hydrojet_spa_set_target_temp(
cached_state.timestamp = int(time())
cached_state.attrs["Tset"] = target_temp

async def hydrojet_spa_set_locked(self, device_id: str, locked: bool) -> None:
"""Lock or unlock the physical control panel on a spa device."""
if (cached_state := self._state_cache.get(device_id)) is None:
raise BestwayException(f"Device '{device_id}' is not recognised")

_LOGGER.debug("Setting lock state to %s", "ON" if locked else "OFF")
await self._do_control_post(device_id, bit6=1 if locked else 0)
cached_state.timestamp = int(time())
cached_state.attrs["bit6"] = locked

async def hydrojet_spa_set_bubbles(
self, device_id: str, bubbles: HydrojetBubbles
) -> None:
Expand Down
9 changes: 8 additions & 1 deletion custom_components/bestway/bestway/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ class TemperatureUnit(Enum):
FAHRENHEIT = auto()


class HydrojetFilter(IntEnum):
"""Airjet_V01/Hydrojet filter values."""

OFF = 0
ON = 2


class HydrojetHeat(IntEnum):
"""Hydrojet heater values."""
"""Airjet_V01/Hydrojet heater values."""

OFF = 0
ON = 3
Expand Down
12 changes: 9 additions & 3 deletions custom_components/bestway/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import BestwayUpdateCoordinator
from .bestway.model import BestwayDeviceType
from .bestway.model import BestwayDeviceType, HydrojetHeat
from .const import DOMAIN
from .entity import BestwayEntity

Expand Down Expand Up @@ -216,8 +216,14 @@ def max_temp(self) -> float:

async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
should_heat = hvac_mode == HVACMode.HEAT
await self.coordinator.api.hydrojet_spa_set_heat(self.device_id, should_heat)
if hvac_mode == HVACMode.HEAT:
await self.coordinator.api.hydrojet_spa_set_heat(
self.device_id, HydrojetHeat.ON
)
else:
await self.coordinator.api.hydrojet_spa_set_heat(
self.device_id, HydrojetHeat.OFF
)
await self.coordinator.async_refresh()

async def async_set_temperature(self, **kwargs: Any) -> None:
Expand Down
31 changes: 7 additions & 24 deletions custom_components/bestway/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from . import BestwayUpdateCoordinator
from .bestway.api import BestwayApi
from .bestway.model import BestwayDeviceStatus, BestwayDeviceType
from .bestway.model import BestwayDeviceStatus, BestwayDeviceType, HydrojetFilter
from .const import DOMAIN, Icon
from .entity import BestwayEntity

Expand Down Expand Up @@ -91,17 +91,12 @@ class BestwaySwitchEntityDescription(SwitchEntityDescription, SwitchFunctionsMix
name="Spa Filter",
icon=Icon.FILTER,
value_fn=lambda s: bool(s.attrs["filter"] == 2),
turn_on_fn=lambda api, device_id: api.hydrojet_spa_set_filter(device_id, True),
turn_off_fn=lambda api, device_id: api.hydrojet_spa_set_filter(device_id, False),
)

_HYDROJET_LOCK_SWITCH = BestwaySwitchEntityDescription(
key="spa_locked",
name="Spa Locked",
icon=Icon.LOCK,
value_fn=lambda s: bool(s.attrs["bit6"]),
turn_on_fn=lambda api, device_id: api.hydrojet_spa_set_locked(device_id, True),
turn_off_fn=lambda api, device_id: api.hydrojet_spa_set_locked(device_id, False),
turn_on_fn=lambda api, device_id: api.hydrojet_spa_set_filter(
device_id, HydrojetFilter.ON
),
turn_off_fn=lambda api, device_id: api.hydrojet_spa_set_filter(
device_id, HydrojetFilter.OFF
),
)

_POOL_FILTER_SWITCH_TYPES = [
Expand Down Expand Up @@ -175,12 +170,6 @@ async def async_setup_entry(
device_id,
_AIRJET_V01_SPA_BUBBLES_SWITCH,
),
SpaSwitch(
coordinator,
config_entry,
device_id,
_HYDROJET_LOCK_SWITCH,
),
]
)

Expand All @@ -199,12 +188,6 @@ async def async_setup_entry(
device_id,
_HYDROJET_SPA_FILTER_SWITCH,
),
SpaSwitch(
coordinator,
config_entry,
device_id,
_HYDROJET_LOCK_SWITCH,
),
]
)

Expand Down

0 comments on commit 3dc2df9

Please sign in to comment.