Skip to content

[pull] dev from home-assistant:dev #648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,10 @@ async def async_handle_light_on_service( # noqa: C901
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))

Expand Down
59 changes: 43 additions & 16 deletions tests/components/light/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand Down