Add snoo switches (#140748)

* Add snoo switches

* change naming

* change wording
This commit is contained in:
Luke Lashley 2025-03-16 17:24:49 -04:00 committed by GitHub
parent a40bb2790e
commit 15e983e997
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 213 additions and 1 deletions

View File

@ -17,7 +17,12 @@ from .coordinator import SnooConfigEntry, SnooCoordinator
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SELECT, Platform.SENSOR]
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
]
async def async_setup_entry(hass: HomeAssistant, entry: SnooConfigEntry) -> bool:

View File

@ -24,6 +24,12 @@
"exceptions": {
"select_failed": {
"message": "Error while updating {name} to {option}"
},
"switch_on_failed": {
"message": "Turning {name} on failed"
},
"switch_off_failed": {
"message": "Turning {name} off failed"
}
},
"entity": {
@ -66,6 +72,14 @@
"stop": "[%key:component::snoo::entity::sensor::state::state::stop%]"
}
}
},
"switch": {
"sticky_white_noise": {
"name": "Sleepytime sounds"
},
"hold": {
"name": "Level lock"
}
}
}
}

View File

@ -0,0 +1,105 @@
"""Support for Snoo Switches."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from python_snoo.containers import SnooData, SnooDevice
from python_snoo.exceptions import SnooCommandException
from python_snoo.snoo import Snoo
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN
from .coordinator import SnooConfigEntry
from .entity import SnooDescriptionEntity
@dataclass(frozen=True, kw_only=True)
class SnooSwitchEntityDescription(SwitchEntityDescription):
"""Describes a Snoo sensor."""
value_fn: Callable[[SnooData], bool]
set_value_fn: Callable[[Snoo, SnooDevice, SnooData, bool], Awaitable[None]]
BINARY_SENSOR_DESCRIPTIONS: list[SnooSwitchEntityDescription] = [
SnooSwitchEntityDescription(
key="sticky_white_noise",
translation_key="sticky_white_noise",
value_fn=lambda data: data.state_machine.sticky_white_noise == "on",
set_value_fn=lambda snoo_api, device, _, state: snoo_api.set_sticky_white_noise(
device, state
),
),
SnooSwitchEntityDescription(
key="hold",
translation_key="hold",
value_fn=lambda data: data.state_machine.hold == "on",
set_value_fn=lambda snoo_api, device, data, state: snoo_api.set_level(
device, data.state_machine.level, state
),
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: SnooConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Snoo device."""
coordinators = entry.runtime_data
async_add_entities(
SnooSwitch(coordinator, description)
for coordinator in coordinators.values()
for description in BINARY_SENSOR_DESCRIPTIONS
)
class SnooSwitch(SnooDescriptionEntity, SwitchEntity):
"""A switch using Snoo coordinator."""
entity_description: SnooSwitchEntityDescription
@property
def is_on(self) -> bool | None:
"""Return True if entity is on."""
return self.entity_description.value_fn(self.coordinator.data)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
try:
await self.entity_description.set_value_fn(
self.coordinator.snoo,
self.coordinator.device,
self.coordinator.data,
True,
)
except SnooCommandException as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="switch_on_failed",
translation_placeholders={"name": str(self.name), "status": "on"},
) from err
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
try:
await self.entity_description.set_value_fn(
self.coordinator.snoo,
self.coordinator.device,
self.coordinator.data,
False,
)
except SnooCommandException as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="switch_off_failed",
translation_placeholders={"name": str(self.name), "status": "off"},
) from err

View File

@ -0,0 +1,88 @@
"""Test Snoo Switches."""
import copy
from unittest.mock import AsyncMock
import pytest
from python_snoo.containers import SnooDevice
from python_snoo.exceptions import SnooCommandException
from homeassistant.components.switch import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from . import async_init_integration, find_update_callback
from .const import MOCK_SNOO_DATA
async def test_switch(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
"""Test switch and check test values are correctly set."""
await async_init_integration(hass)
assert len(hass.states.async_all("switch")) == 2
assert hass.states.get("switch.test_snoo_level_lock").state == STATE_UNAVAILABLE
assert (
hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_UNAVAILABLE
)
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
await hass.async_block_till_done()
assert len(hass.states.async_all("switch")) == 2
assert hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_OFF
assert hass.states.get("switch.test_snoo_level_lock").state == STATE_OFF
async def test_update_success(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
"""Test changing values for switch entities."""
await async_init_integration(hass)
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
assert hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_OFF
async def set_sticky_white_noise(device: SnooDevice, state: bool):
new_data = copy.deepcopy(MOCK_SNOO_DATA)
new_data.state_machine.sticky_white_noise = "off" if not state else "on"
find_update_callback(bypass_api, device.serialNumber)(new_data)
bypass_api.set_sticky_white_noise.side_effect = set_sticky_white_noise
await hass.services.async_call(
"switch",
SERVICE_TOGGLE,
blocking=True,
target={"entity_id": "switch.test_snoo_sleepytime_sounds"},
)
assert bypass_api.set_sticky_white_noise.assert_called_once
assert hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_ON
@pytest.mark.parametrize(
("command", "error_str"),
[
(SERVICE_TURN_ON, "Turning Sleepytime sounds on failed"),
(SERVICE_TURN_OFF, "Turning Sleepytime sounds off failed"),
],
)
async def test_update_failed(
hass: HomeAssistant, bypass_api: AsyncMock, command: str, error_str: str
) -> None:
"""Test failing to change values for switch entities."""
await async_init_integration(hass)
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
assert hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_OFF
bypass_api.set_sticky_white_noise.side_effect = SnooCommandException
with pytest.raises(HomeAssistantError, match=error_str):
await hass.services.async_call(
"switch",
command,
blocking=True,
target={"entity_id": "switch.test_snoo_sleepytime_sounds"},
)
assert bypass_api.set_level.assert_called_once
assert hass.states.get("switch.test_snoo_sleepytime_sounds").state == STATE_OFF