Skip to content

fix(light): complete brightness range clamping#5195

Merged
make-all merged 3 commits into
make-all:mainfrom
Zuz666:fix/brightness-range-low-values
May 31, 2026
Merged

fix(light): complete brightness range clamping#5195
make-all merged 3 commits into
make-all:mainfrom
Zuz666:fix/brightness-range-low-values

Conversation

@Zuz666

@Zuz666 Zuz666 commented May 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Complete the brightness range clamp added in c33c67b by applying the same conversion path to ATTR_WHITE brightness.
  • Extract the HA-to-DP brightness conversion into one helper so ATTR_BRIGHTNESS and ATTR_WHITE cannot drift apart.
  • Add regression coverage for small ranges, offset ranges, wide ranges, midpoint values, and max values through both code paths.

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

  • uv run ruff check custom_components tests
  • uv run ruff check --select I custom_components tests
  • uv run ruff format --check custom_components tests
  • uv run yamllint custom_components/tuya_local/devices
  • Docker: tests/test_light.py -v -> 18 passed
  • Docker: full pytest -> 193 passed

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
Zuz666 marked this pull request as ready for review May 31, 2026 18:47
@make-all
make-all merged commit bc720ba into make-all:main May 31, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in Tuya Local May 31, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants