From d088cc88e34cb7cf7f04be847195bf0e5123c77c Mon Sep 17 00:00:00 2001 From: Sam Cox Date: Sat, 13 Jun 2026 02:47:17 -0700 Subject: [PATCH] fix: call detect_bulb() before reading value_max in *_percentage() methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #713 --- tinytuya/BulbDevice.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tinytuya/BulbDevice.py b/tinytuya/BulbDevice.py index 48deb79f..5d209e66 100644 --- a/tinytuya/BulbDevice.py +++ b/tinytuya/BulbDevice.py @@ -593,6 +593,9 @@ def set_white_percentage(self, brightness=100, colourtemp=0, nowait=False): if err: raise ValueError( 'set_white_percentage: %s percentage needs to be between 0 and 100.' % err[1:]) + if not self.bulb_configured: + self.detect_bulb(nowait=nowait) + b = int(self.dpset['value_max'] * brightness // 100) c = int(self.dpset['value_max'] * colourtemp // 100) @@ -656,6 +659,8 @@ def set_brightness_percentage(self, brightness=100, nowait=False): """ if not 0 <= brightness <= 100: raise ValueError('set_brightness_percentage: The brightness needs to be between 0 and 100.') + if not self.bulb_configured: + self.detect_bulb(nowait=nowait) b = int(self.dpset['value_max'] * brightness // 100) return self.set_brightness(b, nowait=nowait) @@ -704,6 +709,8 @@ def set_colourtemp_percentage(self, colourtemp=100, nowait=False): """ if not 0 <= colourtemp <= 100: raise ValueError( 'set_colourtemp_percentage: Colourtemp percentage needs to be between 0 and 100.') + if not self.bulb_configured: + self.detect_bulb(nowait=nowait) c = int(self.dpset['value_max'] * colourtemp // 100) return self.set_colourtemp( c, nowait=nowait )