Add switch to toggle filter cycle 2 on balboa spas (#138605)

pull/138615/head
Nathan Spencer 2025-02-15 12:20:33 -07:00 committed by GitHub
parent 78337a6846
commit 0a78f2725d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 158 additions and 0 deletions

View File

@ -24,6 +24,7 @@ PLATFORMS = [
Platform.FAN,
Platform.LIGHT,
Platform.SELECT,
Platform.SWITCH,
Platform.TIME,
]

View File

@ -79,6 +79,11 @@
}
}
},
"switch": {
"filter_cycle_2_enabled": {
"name": "Filter cycle 2 enabled"
}
},
"time": {
"filter_cycle_start": {
"name": "Filter cycle {index} start"

View File

@ -0,0 +1,48 @@
"""Support for Balboa switches."""
from __future__ import annotations
from typing import Any
from pybalboa import SpaClient
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import BalboaConfigEntry
from .entity import BalboaEntity
async def async_setup_entry(
hass: HomeAssistant,
entry: BalboaConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the spa's switches."""
spa = entry.runtime_data
async_add_entities([BalboaSwitchEntity(spa)])
class BalboaSwitchEntity(BalboaEntity, SwitchEntity):
"""Representation of a Balboa switch entity."""
def __init__(self, spa: SpaClient) -> None:
"""Initialize a Balboa switch entity."""
super().__init__(spa, "filter_cycle_2_enabled")
self._attr_entity_category = EntityCategory.CONFIG
self._attr_translation_key = "filter_cycle_2_enabled"
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
return self._client.filter_cycle_2_enabled
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
await self._client.configure_filter_cycle(2, enabled=True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
await self._client.configure_filter_cycle(2, enabled=False)

View File

@ -52,6 +52,7 @@ def client_fixture() -> Generator[MagicMock]:
client.filter_cycle_1_start = time(8, 0)
client.filter_cycle_1_end = time(9, 0)
client.filter_cycle_2_running = False
client.filter_cycle_2_enabled = True
client.filter_cycle_2_start = time(19, 0)
client.filter_cycle_2_end = time(21, 30)
client.temperature_unit = 1

View File

@ -0,0 +1,48 @@
# serializer version: 1
# name: test_switches[switch.fakespa_filter_cycle_2_enabled-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.fakespa_filter_cycle_2_enabled',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Filter cycle 2 enabled',
'platform': 'balboa',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'filter_cycle_2_enabled',
'unique_id': 'FakeSpa-filter_cycle_2_enabled-c0ffee',
'unit_of_measurement': None,
})
# ---
# name: test_switches[switch.fakespa_filter_cycle_2_enabled-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'FakeSpa Filter cycle 2 enabled',
}),
'context': <ANY>,
'entity_id': 'switch.fakespa_filter_cycle_2_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -0,0 +1,55 @@
"""Tests of the switches of the balboa integration."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
from syrupy import SnapshotAssertion
from homeassistant.const import STATE_OFF, STATE_ON, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.common import snapshot_platform
from tests.components.switch import common
ENTITY_SWITCH = "switch.fakespa_filter_cycle_2_enabled"
async def test_switches(
hass: HomeAssistant,
client: MagicMock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test spa switches."""
with patch("homeassistant.components.balboa.PLATFORMS", [Platform.SWITCH]):
entry = await init_integration(hass)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
async def test_switch(hass: HomeAssistant, client: MagicMock) -> None:
"""Test spa filter cycle enabled switch."""
await init_integration(hass)
# check if the initial state is on
state = hass.states.get(ENTITY_SWITCH)
assert state.state == STATE_ON
# test calling turn off
await common.async_turn_off(hass, ENTITY_SWITCH)
client.configure_filter_cycle.assert_called_with(2, enabled=False)
setattr(client, "filter_cycle_2_enabled", False)
client.emit("")
await hass.async_block_till_done()
state = hass.states.get(ENTITY_SWITCH)
assert state.state == STATE_OFF
# test calling turn on
await common.async_turn_on(hass, ENTITY_SWITCH)
client.configure_filter_cycle.assert_called_with(2, enabled=True)