From 6ebda9322ddb170493d685ff0c374cdfa7c2fd88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Diego=20Rodr=C3=ADguez=20Royo?= Date: Sun, 23 Feb 2025 13:54:02 +0100 Subject: [PATCH] Fetch allowed values for select entities at Home Connect (#139103) Fetch allowed values for enum settings --- .../components/home_connect/select.py | 30 +++++++--- tests/components/home_connect/test_select.py | 57 +++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/home_connect/select.py b/homeassistant/components/home_connect/select.py index e4d50b0d5e9..d5657387358 100644 --- a/homeassistant/components/home_connect/select.py +++ b/homeassistant/components/home_connect/select.py @@ -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.""" diff --git a/tests/components/home_connect/test_select.py b/tests/components/home_connect/test_select.py index d98dbd8e5f6..22ece365e6b 100644 --- a/tests/components/home_connect/test_select.py +++ b/tests/components/home_connect/test_select.py @@ -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"), [