From e76b483067a045c33df8135f8e42283e88feee85 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Fri, 16 May 2025 10:36:58 +0200 Subject: [PATCH] Add lamp capability to SmartThings (#144918) --- .../components/smartthings/icons.json | 7 ++ .../components/smartthings/select.py | 36 +++++- .../components/smartthings/strings.json | 11 ++ .../device_status/da_ks_range_0101x.json | 4 +- .../smartthings/snapshots/test_select.ambr | 112 ++++++++++++++++++ tests/components/smartthings/test_select.py | 33 ++++++ 6 files changed, 199 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/smartthings/icons.json b/homeassistant/components/smartthings/icons.json index 51978590e2e..394035aafb6 100644 --- a/homeassistant/components/smartthings/icons.json +++ b/homeassistant/components/smartthings/icons.json @@ -41,6 +41,13 @@ "stop": "mdi:stop" } }, + "lamp": { + "default": "mdi:lightbulb", + "state": { + "on": "mdi:lightbulb-on", + "off": "mdi:lightbulb-off" + } + }, "detergent_amount": { "default": "mdi:car-coolant-level" }, diff --git a/homeassistant/components/smartthings/select.py b/homeassistant/components/smartthings/select.py index 16051cb08f1..4fcd7fd080f 100644 --- a/homeassistant/components/smartthings/select.py +++ b/homeassistant/components/smartthings/select.py @@ -16,6 +16,10 @@ from . import FullDevice, SmartThingsConfigEntry from .const import MAIN from .entity import SmartThingsEntity +LAMP_TO_HA = { + "extraHigh": "extra_high", +} + @dataclass(frozen=True, kw_only=True) class SmartThingsSelectDescription(SelectEntityDescription): @@ -26,6 +30,7 @@ class SmartThingsSelectDescription(SelectEntityDescription): options_attribute: Attribute status_attribute: Attribute command: Command + options_map: dict[str, str] | None = None default_options: list[str] | None = None @@ -75,6 +80,15 @@ CAPABILITIES_TO_SELECT: dict[Capability | str, SmartThingsSelectDescription] = { command=Command.SET_AMOUNT, entity_category=EntityCategory.CONFIG, ), + Capability.SAMSUNG_CE_LAMP: SmartThingsSelectDescription( + key=Capability.SAMSUNG_CE_LAMP, + translation_key="lamp", + options_attribute=Attribute.SUPPORTED_BRIGHTNESS_LEVEL, + status_attribute=Attribute.BRIGHTNESS_LEVEL, + command=Command.SET_BRIGHTNESS_LEVEL, + options_map=LAMP_TO_HA, + entity_category=EntityCategory.CONFIG, + ), } @@ -117,20 +131,29 @@ class SmartThingsSelectEntity(SmartThingsEntity, SelectEntity): @property def options(self) -> list[str]: """Return the list of options.""" - return ( + options: list[str] = ( self.get_attribute_value( self.entity_description.key, self.entity_description.options_attribute ) or self.entity_description.default_options or [] ) + if self.entity_description.options_map: + options = [ + self.entity_description.options_map.get(option, option) + for option in options + ] + return options @property def current_option(self) -> str | None: """Return the current option.""" - return self.get_attribute_value( + option = self.get_attribute_value( self.entity_description.key, self.entity_description.status_attribute ) + if self.entity_description.options_map: + option = self.entity_description.options_map.get(option) + return option async def async_select_option(self, option: str) -> None: """Select an option.""" @@ -144,6 +167,15 @@ class SmartThingsSelectEntity(SmartThingsEntity, SelectEntity): raise ServiceValidationError( "Can only be updated when remote control is enabled" ) + if self.entity_description.options_map: + option = next( + ( + key + for key, value in self.entity_description.options_map.items() + if value == option + ), + option, + ) await self.execute_device_command( self.entity_description.key, self.entity_description.command, diff --git a/homeassistant/components/smartthings/strings.json b/homeassistant/components/smartthings/strings.json index 50cb864e7d7..66bb97e4f40 100644 --- a/homeassistant/components/smartthings/strings.json +++ b/homeassistant/components/smartthings/strings.json @@ -115,6 +115,17 @@ "stop": "[%key:common::state::stopped%]" } }, + "lamp": { + "name": "Lamp", + "state": { + "off": "[%key:common::state::off%]", + "on": "[%key:common::state::on%]", + "low": "Low", + "mid": "Mid", + "high": "High", + "extra_high": "Extra high" + } + }, "detergent_amount": { "name": "Detergent dispense amount", "state": { diff --git a/tests/components/smartthings/fixtures/device_status/da_ks_range_0101x.json b/tests/components/smartthings/fixtures/device_status/da_ks_range_0101x.json index 6d15aa4696d..09c5a13613a 100644 --- a/tests/components/smartthings/fixtures/device_status/da_ks_range_0101x.json +++ b/tests/components/smartthings/fixtures/device_status/da_ks_range_0101x.json @@ -669,11 +669,11 @@ }, "samsungce.lamp": { "brightnessLevel": { - "value": "off", + "value": "extraHigh", "timestamp": "2025-03-13T21:23:27.659Z" }, "supportedBrightnessLevel": { - "value": ["off", "high"], + "value": ["off", "extraHigh"], "timestamp": "2025-03-13T21:23:27.659Z" } }, diff --git a/tests/components/smartthings/snapshots/test_select.ambr b/tests/components/smartthings/snapshots/test_select.ambr index 17d8e10d230..b2c3234847e 100644 --- a/tests/components/smartthings/snapshots/test_select.ambr +++ b/tests/components/smartthings/snapshots/test_select.ambr @@ -1,4 +1,116 @@ # serializer version: 1 +# name: test_all_entities[da_ks_oven_01061][select.oven_lamp-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'high', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': , + 'entity_id': 'select.oven_lamp', + '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': 'Lamp', + 'platform': 'smartthings', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'lamp', + 'unique_id': '9447959a-0dfa-6b27-d40d-650da525c53f_main_samsungce.lamp_brightnessLevel_brightnessLevel', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[da_ks_oven_01061][select.oven_lamp-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Oven Lamp', + 'options': list([ + 'off', + 'high', + ]), + }), + 'context': , + 'entity_id': 'select.oven_lamp', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_all_entities[da_ks_range_0101x][select.vulcan_lamp-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'extra_high', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': , + 'entity_id': 'select.vulcan_lamp', + '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': 'Lamp', + 'platform': 'smartthings', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'lamp', + 'unique_id': '2c3cbaa0-1899-5ddc-7b58-9d657bd48f18_main_samsungce.lamp_brightnessLevel_brightnessLevel', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[da_ks_range_0101x][select.vulcan_lamp-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Vulcan Lamp', + 'options': list([ + 'off', + 'extra_high', + ]), + }), + 'context': , + 'entity_id': 'select.vulcan_lamp', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'extra_high', + }) +# --- # name: test_all_entities[da_wm_dw_000001][select.dishwasher-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/smartthings/test_select.py b/tests/components/smartthings/test_select.py index ce3bea08ca2..da27565ead5 100644 --- a/tests/components/smartthings/test_select.py +++ b/tests/components/smartthings/test_select.py @@ -9,6 +9,7 @@ from syrupy import SnapshotAssertion from homeassistant.components.select import ( ATTR_OPTION, + ATTR_OPTIONS, DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) @@ -95,6 +96,38 @@ async def test_select_option( ) +@pytest.mark.parametrize("device_fixture", ["da_ks_range_0101x"]) +async def test_select_option_map( + hass: HomeAssistant, + devices: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test state update.""" + await setup_integration(hass, mock_config_entry) + + state = hass.states.get("select.vulcan_lamp") + assert state + assert state.state == "extra_high" + assert state.attributes[ATTR_OPTIONS] == [ + "off", + "extra_high", + ] + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + {ATTR_ENTITY_ID: "select.vulcan_lamp", ATTR_OPTION: "extra_high"}, + blocking=True, + ) + devices.execute_device_command.assert_called_once_with( + "2c3cbaa0-1899-5ddc-7b58-9d657bd48f18", + Capability.SAMSUNG_CE_LAMP, + Command.SET_BRIGHTNESS_LEVEL, + MAIN, + argument="extraHigh", + ) + + @pytest.mark.parametrize("device_fixture", ["da_wm_wd_000001"]) async def test_select_option_without_remote_control( hass: HomeAssistant,