From 2ffcc70544f2fddd2f1ba3a7db2b217b3e41fb11 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Mon, 15 Jun 2026 05:19:55 -0400 Subject: [PATCH] Add brightness_pct to set_level action (#173285) --- homeassistant/components/template/light.py | 4 +++- tests/components/template/test_light.py | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/template/light.py b/homeassistant/components/template/light.py index 41ce19a0e44..31b0da63355 100644 --- a/homeassistant/components/template/light.py +++ b/homeassistant/components/template/light.py @@ -519,7 +519,9 @@ class AbstractTemplateLight(AbstractTemplateEntity, LightEntity): common_params = {} if ATTR_BRIGHTNESS in kwargs: - common_params["brightness"] = kwargs[ATTR_BRIGHTNESS] + brightness = kwargs[ATTR_BRIGHTNESS] + common_params["brightness"] = brightness + common_params["brightness_pct"] = round(brightness / 255 * 100) if ATTR_TRANSITION in kwargs and self._supports_transition is True: common_params["transition"] = kwargs[ATTR_TRANSITION] diff --git a/tests/components/template/test_light.py b/tests/components/template/test_light.py index d5a7c892e6c..3cf6ed61cd0 100644 --- a/tests/components/template/test_light.py +++ b/tests/components/template/test_light.py @@ -8,6 +8,7 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components import light, template from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP_KELVIN, ATTR_EFFECT, ATTR_HS_COLOR, @@ -67,7 +68,10 @@ ON_OFF_ACTIONS = { } -BRIGHTNESS_DATA = {"brightness": "{{ brightness }}"} +BRIGHTNESS_DATA = { + "brightness": "{{ brightness }}", + "brightness_pct": "{{ brightness_pct }}", +} SET_LEVEL_ACTION = make_test_action("set_level", BRIGHTNESS_DATA) ON_OFF_SET_LEVEL_ACTIONS = { **ON_OFF_ACTIONS, @@ -670,9 +674,13 @@ async def test_off_action_optimistic( "style", [ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) +@pytest.mark.parametrize( + ("brightness", "brightness_pct"), + [(2, 1), (255, 100), (124, 49), (1, 0), (254, 100)], +) @pytest.mark.usefixtures("setup_state_light") async def test_level_action_no_template( - hass: HomeAssistant, calls: list[ServiceCall] + hass: HomeAssistant, brightness: int, brightness_pct: int, calls: list[ServiceCall] ) -> None: """Test setting brightness with optimistic template.""" state = hass.states.get(TEST_LIGHT.entity_id) @@ -682,14 +690,14 @@ async def test_level_action_no_template( hass, calls, SERVICE_TURN_ON, - {ATTR_BRIGHTNESS: 124}, - {ATTR_BRIGHTNESS: 124}, + {ATTR_BRIGHTNESS: brightness}, + {ATTR_BRIGHTNESS: brightness, ATTR_BRIGHTNESS_PCT: brightness_pct}, "set_level", ) state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON - assert state.attributes["brightness"] == 124 + assert state.attributes["brightness"] == brightness assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0