diff --git a/homeassistant/components/balboa/__init__.py b/homeassistant/components/balboa/__init__.py index 78bf6f7cda7..207826d136e 100644 --- a/homeassistant/components/balboa/__init__.py +++ b/homeassistant/components/balboa/__init__.py @@ -24,6 +24,7 @@ PLATFORMS = [ Platform.FAN, Platform.LIGHT, Platform.SELECT, + Platform.SWITCH, Platform.TIME, ] diff --git a/homeassistant/components/balboa/strings.json b/homeassistant/components/balboa/strings.json index 0262f26f4bd..9779984b182 100644 --- a/homeassistant/components/balboa/strings.json +++ b/homeassistant/components/balboa/strings.json @@ -79,6 +79,11 @@ } } }, + "switch": { + "filter_cycle_2_enabled": { + "name": "Filter cycle 2 enabled" + } + }, "time": { "filter_cycle_start": { "name": "Filter cycle {index} start" diff --git a/homeassistant/components/balboa/switch.py b/homeassistant/components/balboa/switch.py new file mode 100644 index 00000000000..c8c947f499d --- /dev/null +++ b/homeassistant/components/balboa/switch.py @@ -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) diff --git a/tests/components/balboa/conftest.py b/tests/components/balboa/conftest.py index 3a3561f15cf..90f8fdc3d6e 100644 --- a/tests/components/balboa/conftest.py +++ b/tests/components/balboa/conftest.py @@ -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 diff --git a/tests/components/balboa/snapshots/test_switch.ambr b/tests/components/balboa/snapshots/test_switch.ambr new file mode 100644 index 00000000000..ad63fcdf387 --- /dev/null +++ b/tests/components/balboa/snapshots/test_switch.ambr @@ -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': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.fakespa_filter_cycle_2_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + 'entity_id': 'switch.fakespa_filter_cycle_2_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/balboa/test_switch.py b/tests/components/balboa/test_switch.py new file mode 100644 index 00000000000..4b6bae172f4 --- /dev/null +++ b/tests/components/balboa/test_switch.py @@ -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)