Skip to content

Fix brightness calculation when using brightness_step_pct #143786

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

Conversation

andreaskoelsch
Copy link
Contributor

@andreaskoelsch andreaskoelsch commented Apr 27, 2025

Proposed change

Fixed brightness calculation when using percentage steps. Before, occasionally, rounding error could occur. The issue is described in #143559.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@andreaskoelsch andreaskoelsch requested a review from a team as a code owner April 27, 2025 21:36
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andreaskoelsch

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft April 27, 2025 21:36
@home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (light) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of light can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign light Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@abmantis
Copy link
Contributor

Could you also add/update the tests to cover the case that was causing the issue (the 1% increments)?

@andreaskoelsch
Copy link
Contributor Author

I will look into it. Might take a week or two till I find time for it.

@MartinHjelmare MartinHjelmare changed the title Fixed brightness calculation when using brightness_step_pct (#143559) Fix brightness calculation when using brightness_step_pct Apr 28, 2025
@@ -442,7 +442,8 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's normally not a good practice to round before the final result. It's not clear why that is appropriate here. I've read the issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be use the helpers?

def brightness_to_value(low_high_range: tuple[float, float], brightness: int) -> float:
"""Given a brightness_scale convert a brightness to a single value.
Do not include 0 if the light is off for value 0.
Given a brightness low_high_range of (1,100) this function
will return:
255: 100.0
127: ~49.8039
10: ~3.9216
"""
return scale_to_ranged_value((1, 255), low_high_range, brightness)
def value_to_brightness(low_high_range: tuple[float, float], value: float) -> int:
"""Given a brightness_scale convert a single value to a brightness.
Do not include 0 if the light is off for value 0.
Given a brightness low_high_range of (1,100) this function
will return:
100: 255
50: 128
4: 10
The value will be clamped between 1..255 to ensure valid value.
"""
return min(
255,
max(1, round(scale_to_ranged_value(low_high_range, (1, 255), value))),
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's normally not a good practice to round before the final result. It's not clear why that is appropriate here. I've read the issue.

The problem is that the brightness is saved as [0, 255] value, and when you trigger a percentage-based brightness step, there can be a rounding error, which can accumulate if you do it multiple times. To not accumulate this rounding error, the percentage based steps should happen in the percentage domain instead of the brightness domain, i.e. the brightness has to be converted to the percentage domain (first rounding, equalize the potential rounding error from before), then do the stepping, then convert back to brightness domain (second rounding).

May be use the helpers?

Ohh, didn't see them as they were not used before either. Also, I don't think it would improve readability, as it's consistently not used in the file (cf.

params[ATTR_BRIGHTNESS] = round(255 * brightness_pct / 100)
). However, if you wish, I can of course change it.

EDIT: Also worth noting, this rounding error accumulation does not only happen for small steps, but also for larger steps.

  • 3 * 1% = 4%
  • 3 * 10% = 31%
  • 3 * 30% = 91%

@MartinHjelmare MartinHjelmare marked this pull request as draft April 28, 2025 15:14
@andreaskoelsch
Copy link
Contributor Author

andreaskoelsch commented Apr 29, 2025

I tried to set up an env for writing tests on a Ubuntu VM, but I'm stuck here (#142304). Guess, I'll wait for a fix for this issue before I continue working on this PR.

EDIT: Managed to get it running using a dev container

@andreaskoelsch andreaskoelsch force-pushed the fix_brightness_calculation branch from d18ca97 to ef1a6ed Compare April 29, 2025 22:11
@andreaskoelsch andreaskoelsch marked this pull request as ready for review April 29, 2025 22:12
@andreaskoelsch
Copy link
Contributor Author

Added a test that reduces the brightness 3 times by 10% starting from 100%. The expected result is 70%. Without the proposed code change, this test will fail, because brightness would end up at 69%.

Copy link
Contributor

@abmantis abmantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change, otherwise LGTM

@home-assistant home-assistant bot marked this pull request as draft April 30, 2025 16:01
@andreaskoelsch andreaskoelsch force-pushed the fix_brightness_calculation branch from ef1a6ed to ced0eea Compare April 30, 2025 18:53
@andreaskoelsch andreaskoelsch marked this pull request as ready for review April 30, 2025 18:54
@home-assistant home-assistant bot requested a review from abmantis April 30, 2025 18:54
@andreaskoelsch andreaskoelsch force-pushed the fix_brightness_calculation branch from ced0eea to 857eb89 Compare April 30, 2025 19:05
@abmantis abmantis added this to the 2025.5.0 milestone May 1, 2025
@abmantis
Copy link
Contributor

abmantis commented May 1, 2025

Thanks @andreaskoelsch !

@abmantis abmantis merged commit 4e8d68a into home-assistant:dev May 1, 2025
44 checks passed
@andreaskoelsch andreaskoelsch deleted the fix_brightness_calculation branch May 2, 2025 18:25
@github-actions github-actions bot locked and limited conversation to collaborators May 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

light.turn_on brightness_step_pct skips values
5 participants