fix(light): complete brightness range clamping#5195
Merged
Conversation
The previous workaround (c33c67b) only covered the regular brightness path. The white brightness path (ATTR_WHITE) had the same issue. It also used duplicated logic that was susceptible to future drift. Extract the brightness range scaling algorithm into a helper method and apply it to both ATTR_BRIGHTNESS and ATTR_WHITE paths. This ensures both paths preserve the intended behavior (snapping bright==1 to the minimum) while safely clamping values that fall below the target range. Test coverage expanded significantly to cover both paths and all edge cases (wide ranges, proportional mid-values, upper bounds).
Zuz666
marked this pull request as ready for review
May 31, 2026 18:47
6 tasks
btoddb
pushed a commit
to btoddb/tuya-local
that referenced
this pull request
Jun 5, 2026
* fix(light): complete brightness_to_value range clamping The previous workaround (c33c67b) only covered the regular brightness path. The white brightness path (ATTR_WHITE) had the same issue. It also used duplicated logic that was susceptible to future drift. Extract the brightness range scaling algorithm into a helper method and apply it to both ATTR_BRIGHTNESS and ATTR_WHITE paths. This ensures both paths preserve the intended behavior (snapping bright==1 to the minimum) while safely clamping values that fall below the target range. Test coverage expanded significantly to cover both paths and all edge cases (wide ranges, proportional mid-values, upper bounds). * refactor(light): clarify brightness DP helper name * refactor(light): clarify HA brightness helper naming --------- Co-authored-by: Yuris Auzins <zuz666@users.noreply.github.com>
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.
Summary
Problem
Home Assistant's color_util.brightness_to_value() can return a value below the configured device range minimum for low HA brightness values. For example, brightness_to_value((1, 6), 20) returns 0.470588, which later rounds to 0 and fails validation against the 1..6 DP range.
The workaround in c33c67b fixes this for regular brightness, but the white brightness path still used the old conversion logic. This PR applies the same behavior to both paths and keeps the existing bright == 1 behavior that makes the physical device minimum reachable on wide ranges.
Related discussion: #5123
Test Plan