Fix brightness calculation when using brightness_step_pct (#143786)

This commit is contained in:
Andreas Kölsch 2025-05-02 00:07:52 +02:00 committed by GitHub
parent 883ab44437
commit 4e8d68a2ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 17 deletions

View File

@ -442,7 +442,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
brightness += params.pop(ATTR_BRIGHTNESS_STEP)
else:
brightness += round(params.pop(ATTR_BRIGHTNESS_STEP_PCT) / 100 * 255)
brightness_pct = round(brightness / 255 * 100)
brightness = round(
(brightness_pct + params.pop(ATTR_BRIGHTNESS_STEP_PCT)) / 100 * 255
)
params[ATTR_BRIGHTNESS] = max(0, min(255, brightness))

View File

@ -958,21 +958,6 @@ async def test_light_brightness_step(hass: HomeAssistant) -> None:
_, data = entity1.last_call("turn_on")
assert data["brightness"] == 40 # 50 - 10
await hass.services.async_call(
"light",
"turn_on",
{
"entity_id": [entity0.entity_id, entity1.entity_id],
"brightness_step_pct": 10,
},
blocking=True,
)
_, data = entity0.last_call("turn_on")
assert data["brightness"] == 116 # 90 + (255 * 0.10)
_, data = entity1.last_call("turn_on")
assert data["brightness"] == 66 # 40 + (255 * 0.10)
await hass.services.async_call(
"light",
"turn_on",
@ -983,7 +968,49 @@ async def test_light_brightness_step(hass: HomeAssistant) -> None:
blocking=True,
)
assert entity0.state == "off" # 126 - 126; brightness is 0, light should turn off
assert entity0.state == "off" # 40 - 126; brightness is 0, light should turn off
async def test_light_brightness_step_pct(hass: HomeAssistant) -> None:
"""Test that percentage based brightness steps work as expected."""
entity = MockLight("Test_0", STATE_ON)
setup_test_component_platform(hass, light.DOMAIN, [entity])
entity.supported_features = light.SUPPORT_BRIGHTNESS
# Set color modes to none to trigger backwards compatibility in LightEntity
entity.supported_color_modes = None
entity.color_mode = None
entity.brightness = 255
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done()
state = hass.states.get(entity.entity_id)
assert state is not None
assert state.attributes["brightness"] == 255 # 100%
def reduce_brightness_by_ten_percent():
return hass.services.async_call(
"light",
"turn_on",
{
"entity_id": [entity.entity_id],
"brightness_step_pct": -10,
},
blocking=True,
)
await reduce_brightness_by_ten_percent()
_, data = entity.last_call("turn_on")
assert round(data["brightness"] / 2.55) == 90 # 100% - 10% = 90%
await reduce_brightness_by_ten_percent()
_, data = entity.last_call("turn_on")
assert round(data["brightness"] / 2.55) == 80 # 90% - 10% = 80%
await reduce_brightness_by_ten_percent()
_, data = entity.last_call("turn_on")
assert round(data["brightness"] / 2.55) == 70 # 80% - 10% = 70%
@pytest.mark.usefixtures("enable_custom_integrations")