Skip to content

fix: call detect_bulb() in *_percentage() methods before reading value_max#714

Merged
jasonacox merged 2 commits into
jasonacox:masterfrom
jasonacox-sam:fix/brightness-percentage-before-detect
Jun 28, 2026
Merged

fix: call detect_bulb() in *_percentage() methods before reading value_max#714
jasonacox merged 2 commits into
jasonacox:masterfrom
jasonacox-sam:fix/brightness-percentage-before-detect

Conversation

@jasonacox-sam

Copy link
Copy Markdown
Collaborator

Fixes #713

Problem

When set_brightness_percentage(), set_white_percentage(), or set_colourtemp_percentage() is called before bulb detection has run, self.dpset['value_max'] is still -1 (the unconfigured default). The computed raw value is wrong, which causes the < 0 fallback in set_brightness() to fire and pin the device to full brightness regardless of the requested percentage.

Fix

Add a if not self.bulb_configured: self.detect_bulb(nowait=nowait) guard at the top of each _percentage() method, before the value_max multiplication. This mirrors the guard already present in set_brightness(), set_white(), and other lower-level methods.

Affected Methods

  • set_brightness_percentage()
  • set_white_percentage()
  • set_colourtemp_percentage()

Testing

Workaround confirmed by @andreidiaconescu18 in #713 — calling d.status() first (which triggers detect_bulb()) makes set_brightness_percentage(75) work correctly. This PR makes that detection automatic.

— Sam ⚙️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an initialization-order bug in BulbDevice where percentage-based setters could compute incorrect raw DPS values when called before bulb detection populated dpset['value_max'], leading to unintended full-brightness behavior (Issue #713).

Changes:

  • Add a bulb_configured guard that calls detect_bulb() at the start of set_brightness_percentage().
  • Add the same guard to set_white_percentage() and set_colourtemp_percentage() to ensure value_max is available before percentage conversion.
  • Align percentage setters’ behavior with existing lower-level setters that already ensure bulb detection occurs before using bulb-specific ranges.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tinytuya/BulbDevice.py
Comment on lines +596 to +598
if not self.bulb_configured:
self.detect_bulb(nowait=nowait)

Comment thread tinytuya/BulbDevice.py
Comment on lines +662 to +663
if not self.bulb_configured:
self.detect_bulb(nowait=nowait)
Comment thread tinytuya/BulbDevice.py
Comment on lines +712 to +713
if not self.bulb_configured:
self.detect_bulb(nowait=nowait)
Comment thread tinytuya/BulbDevice.py
Comment on lines +662 to +663
if not self.bulb_configured:
self.detect_bulb(nowait=nowait)
@jasonacox

Copy link
Copy Markdown
Owner

@jasonacox-sam Please review and address the copilot code review items if they have merit.

…thods

When set_brightness_percentage(), set_white_percentage(), or
set_colourtemp_percentage() is called before bulb detection has run,
self.dpset['value_max'] is still -1 (the unconfigured default). The
computed raw value is then wrong — always -1 — which triggers the
fallback in set_brightness() and pins the device to full brightness
regardless of the requested percentage.

Fix: call detect_bulb() if bulb_configured is False before computing
the raw value from value_max. This mirrors the guard already present
in set_brightness(), set_white(), and other lower-level methods.

Fixes jasonacox#713
Address Copilot review: detect_bulb() can exit without configuring
when nowait=True with no cached status, leaving value_max=-1 and
producing incorrect raw values. Now raises RuntimeError like state()
and bulb_has_capability() already do.

Also adds test_percentage_raises_when_unconfigured to verify the guard.
@jasonacox-sam jasonacox-sam force-pushed the fix/brightness-percentage-before-detect branch from d088cc8 to 3841d1b Compare June 28, 2026 09:56
@jasonacox-sam

Copy link
Copy Markdown
Collaborator Author

@jasonacox — Reviewed all four Copilot items. They have merit: detect_bulb() can exit without configuring when nowait=True and there's no cached status, leaving value_max at its default of -1. The existing state() and bulb_has_capability() methods already guard against this with a post-detect bulb_configured check — the *_percentage() methods were missing the same guard.

Fixed in 3841d1b:

  • Added post-detect RuntimeError guard to set_brightness_percentage(), set_white_percentage(), and set_colourtemp_percentage() — matching the pattern already used by state() and bulb_has_capability()
  • Added test_percentage_raises_when_unconfigured regression test that verifies all three methods raise RuntimeError when called before detection with nowait=True and no cached status

All 23 tests passing. — Sam ⚙️

@jasonacox jasonacox merged commit 2e4bb75 into jasonacox:master Jun 28, 2026
17 checks passed
@jasonacox

Copy link
Copy Markdown
Owner

Thanks, Sam! Added update to bundle with future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set_brightness_percentage() - not working properly

3 participants