fix(ha): round home_status brightness readback to nearest percent#596
Merged
matedev01 merged 3 commits intoJul 6, 2026
Merged
Conversation
`summarize_state` reports a light's brightness with integer
`value * 100 / 255`, which truncates toward zero. The write path
(`normalize_brightness`) rounds when mapping a percent onto the 0-255
scale, so the two directions disagree: a light set to 75% is stored as
191/255 and read back as "74%"; 99% (stored 252) reads back as "98%";
15% (stored 38) as "14%". Home Assistant's own brightness_pct rounds,
so the spoken status also disagreed with the HA UI.
Round the readback to the nearest percent so home_status matches both
the write path and HA. Blast radius is the single brightness-format
expression; no other caller or domain is touched.
## Real Behavior Proof
- [x] Added regression test `brightness_readback_rounds_to_nearest_percent`;
fail-before printed `got: Living room lamp is on, brightness 74%` for a
75% light, pass-after is green.
- [x] `cargo fmt --check` clean
- [x] `cargo clippy --all-targets -- -D warnings` clean
- [x] `cargo test` full suite: 0 failed (lib 807 passed / 4 ignored)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Author
|
Hi @matedev01 — flagging #596 in case it slipped the queue. It has been green across all checks and mergeable, and I just rebased it onto latest |
matedev01
approved these changes
Jul 6, 2026
matedev01
left a comment
Contributor
There was a problem hiding this comment.
LGTM — correct fix (.round() matches both HA's brightness_pct and normalize_brightness), regression test covers 191->75%, 252->99%, 255->100% boundary. HA 35/35, BFCL 96%.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
home_statusreports a light's brightness one percent too low.summarize_stateformats it with integervalue * 100 / 255, which truncates toward zero, while the write path (normalize_brightness) rounds a percent onto the 0-255 scale. The two directions disagree, and so does Home Assistant's ownbrightness_pct(which rounds):Fix
Round the readback to the nearest percent so
home_statusmatches both the write path and HA. Blast radius is the single brightness-format expression — no other caller or domain is touched.Real Behavior Proof
brightness_readback_rounds_to_nearest_percent. Fail-before:expected rounded 75%, got: Living room lamp is on, brightness 74%. Pass-after: green.cargo fmt --checkcleancargo clippy --all-targets -- -D warningscleancargo testfull suite: 0 failed (lib 807 passed / 4 ignored)