-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
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
Fix brightness calculation when using brightness_step_pct #143786
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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!
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Could you also add/update the tests to cover the case that was causing the issue (the 1% increments)? |
I will look into it. Might take a week or two till I find time for it. |
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
core/homeassistant/util/color.py
Lines 752 to 784 in 9802167
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))), | |
) |
There was a problem hiding this comment.
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) |
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%
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 |
d18ca97
to
ef1a6ed
Compare
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%. |
There was a problem hiding this 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
ef1a6ed
to
ced0eea
Compare
ced0eea
to
857eb89
Compare
Thanks @andreaskoelsch ! |
Proposed change
Fixed brightness calculation when using percentage steps. Before, occasionally, rounding error could occur. The issue is described in #143559.
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: