mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fetch allowed values for select entities at Home Connect (#139103)
Fetch allowed values for enum settings
This commit is contained in:
parent
4ca39636e2
commit
6ebda9322d
@ -1,6 +1,7 @@
|
||||
"""Provides a select platform for Home Connect."""
|
||||
|
||||
from collections.abc import Callable, Coroutine
|
||||
import contextlib
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, cast
|
||||
|
||||
@ -423,13 +424,6 @@ class HomeConnectSelectEntity(HomeConnectEntity, SelectEntity):
|
||||
appliance,
|
||||
desc,
|
||||
)
|
||||
setting = appliance.settings.get(cast(SettingKey, desc.key))
|
||||
if setting and setting.constraints and setting.constraints.allowed_values:
|
||||
self._attr_options = [
|
||||
desc.values_translation_key[option]
|
||||
for option in setting.constraints.allowed_values
|
||||
if option in desc.values_translation_key
|
||||
]
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select new option."""
|
||||
@ -459,6 +453,28 @@ class HomeConnectSelectEntity(HomeConnectEntity, SelectEntity):
|
||||
data.value
|
||||
)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""When entity is added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
setting = self.appliance.settings.get(cast(SettingKey, self.bsh_key))
|
||||
if (
|
||||
not setting
|
||||
or not setting.constraints
|
||||
or not setting.constraints.allowed_values
|
||||
):
|
||||
with contextlib.suppress(HomeConnectError):
|
||||
setting = await self.coordinator.client.get_setting(
|
||||
self.appliance.info.ha_id,
|
||||
setting_key=cast(SettingKey, self.bsh_key),
|
||||
)
|
||||
|
||||
if setting and setting.constraints and setting.constraints.allowed_values:
|
||||
self._attr_options = [
|
||||
self.entity_description.values_translation_key[option]
|
||||
for option in setting.constraints.allowed_values
|
||||
if option in self.entity_description.values_translation_key
|
||||
]
|
||||
|
||||
|
||||
class HomeConnectSelectOptionEntity(HomeConnectOptionEntity, SelectEntity):
|
||||
"""Select option class for Home Connect."""
|
||||
|
@ -509,6 +509,63 @@ async def test_select_functionality(
|
||||
assert hass.states.is_state(entity_id, value_to_set)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("appliance_ha_id", ["Hood"], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
"entity_id",
|
||||
"test_setting_key",
|
||||
"allowed_values",
|
||||
"expected_options",
|
||||
),
|
||||
[
|
||||
(
|
||||
"select.hood_ambient_light_color",
|
||||
SettingKey.BSH_COMMON_AMBIENT_LIGHT_COLOR,
|
||||
[f"BSH.Common.EnumType.AmbientLightColor.Color{i}" for i in range(50)],
|
||||
{str(i) for i in range(1, 50)},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_fetch_allowed_values(
|
||||
appliance_ha_id: str,
|
||||
entity_id: str,
|
||||
test_setting_key: SettingKey,
|
||||
allowed_values: list[str | None],
|
||||
expected_options: set[str],
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
integration_setup: Callable[[MagicMock], Awaitable[bool]],
|
||||
setup_credentials: None,
|
||||
client: MagicMock,
|
||||
) -> None:
|
||||
"""Test fetch allowed values."""
|
||||
original_get_setting_side_effect = client.get_setting
|
||||
|
||||
async def get_setting_side_effect(
|
||||
ha_id: str, setting_key: SettingKey
|
||||
) -> GetSetting:
|
||||
if ha_id != appliance_ha_id or setting_key != test_setting_key:
|
||||
return await original_get_setting_side_effect(ha_id, setting_key)
|
||||
return GetSetting(
|
||||
key=test_setting_key,
|
||||
raw_key=test_setting_key.value,
|
||||
value="", # Not important
|
||||
constraints=SettingConstraints(
|
||||
allowed_values=allowed_values,
|
||||
),
|
||||
)
|
||||
|
||||
client.get_setting = AsyncMock(side_effect=get_setting_side_effect)
|
||||
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert await integration_setup(client)
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_state = hass.states.get(entity_id)
|
||||
assert entity_state
|
||||
assert set(entity_state.attributes[ATTR_OPTIONS]) == expected_options
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "setting_key", "allowed_value", "value_to_set", "mock_attr"),
|
||||
[
|
||||
|
Loading…
x
Reference in New Issue
Block a user