Add Select entity to Snoo (#140638)

This commit is contained in:
Luke Lashley 2025-03-16 09:00:43 -04:00 committed by GitHub
parent 011a076155
commit 4e0985e1a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 172 additions and 1 deletions

View File

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

View File

@ -0,0 +1,78 @@
"""Support for Snoo Select."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from python_snoo.containers import SnooData, SnooDevice, SnooLevels
from python_snoo.exceptions import SnooCommandException
from python_snoo.snoo import Snoo
from homeassistant.components.select import SelectEntity, SelectEntityDescription
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 SnooSelectEntityDescription(SelectEntityDescription):
"""Describes a Snoo Select."""
value_fn: Callable[[SnooData], str]
set_value_fn: Callable[[Snoo, SnooDevice, str], Awaitable[None]]
SELECT_DESCRIPTIONS: list[SnooSelectEntityDescription] = [
SnooSelectEntityDescription(
key="intensity",
translation_key="intensity",
value_fn=lambda data: data.state_machine.level.name,
set_value_fn=lambda snoo_api, device, state: snoo_api.set_level(
device, SnooLevels[state]
),
options=[level.name for level in SnooLevels],
),
]
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(
SnooSelect(coordinator, description)
for coordinator in coordinators.values()
for description in SELECT_DESCRIPTIONS
)
class SnooSelect(SnooDescriptionEntity, SelectEntity):
"""A sensor using Snoo coordinator."""
entity_description: SnooSelectEntityDescription
@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
return self.entity_description.value_fn(self.coordinator.data)
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
try:
await self.entity_description.set_value_fn(
self.coordinator.snoo, self.device, option
)
except SnooCommandException as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="select_failed",
translation_placeholders={"name": str(self.name), "option": option},
) from err

View File

@ -21,6 +21,11 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"exceptions": {
"select_failed": {
"message": "Error while updating {name} to {option}"
}
},
"entity": {
"sensor": {
"state": {
@ -39,6 +44,19 @@
"time_left": {
"name": "Time left"
}
},
"select": {
"intensity": {
"name": "Intensity",
"state": {
"baseline": "[%key:component::snoo::entity::sensor::state::state::baseline%]",
"level1": "[%key:component::snoo::entity::sensor::state::state::level1%]",
"level2": "[%key:component::snoo::entity::sensor::state::state::level2%]",
"level3": "[%key:component::snoo::entity::sensor::state::state::level3%]",
"level4": "[%key:component::snoo::entity::sensor::state::state::level4%]",
"stop": "[%key:component::snoo::entity::sensor::state::state::stop%]"
}
}
}
}
}

View File

@ -0,0 +1,75 @@
"""Test Snoo Selects."""
import copy
from unittest.mock import AsyncMock
import pytest
from python_snoo.containers import SnooDevice, SnooLevels, SnooStates
from homeassistant.components.select import SERVICE_SELECT_OPTION
from homeassistant.components.snoo.select import SnooCommandException
from homeassistant.const import 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_select(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
"""Test select and check test values are correctly set."""
await async_init_integration(hass)
assert len(hass.states.async_all("select")) == 1
assert hass.states.get("select.test_snoo_intensity").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("select")) == 1
assert hass.states.get("select.test_snoo_intensity").state == "stop"
async def test_update_success(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
"""Test changing values for select entities."""
await async_init_integration(hass)
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
assert hass.states.get("select.test_snoo_intensity").state == "stop"
async def update_level(device: SnooDevice, level: SnooStates, _hold: bool = False):
new_data = copy.deepcopy(MOCK_SNOO_DATA)
new_data.state_machine.level = SnooLevels(level.value)
find_update_callback(bypass_api, device.serialNumber)(new_data)
bypass_api.set_level.side_effect = update_level
await hass.services.async_call(
"select",
SERVICE_SELECT_OPTION,
service_data={"option": "level1"},
blocking=True,
target={"entity_id": "select.test_snoo_intensity"},
)
assert bypass_api.set_level.assert_called_once
assert hass.states.get("select.test_snoo_intensity").state == "level1"
async def test_update_failed(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
"""Test failing to change values for select entities."""
await async_init_integration(hass)
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
assert hass.states.get("select.test_snoo_intensity").state == "stop"
bypass_api.set_level.side_effect = SnooCommandException
with pytest.raises(
HomeAssistantError, match="Error while updating Intensity to level1"
):
await hass.services.async_call(
"select",
SERVICE_SELECT_OPTION,
service_data={"option": "level1"},
blocking=True,
target={"entity_id": "select.test_snoo_intensity"},
)
assert bypass_api.set_level.assert_called_once
assert hass.states.get("select.test_snoo_intensity").state == "stop"