8000 Fix BulbDevice for non-bulb devices by uzlonewolf · Pull Request #620 · jasonacox/tinytuya · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix BulbDevice for non-bulb devices #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file 8000
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,48 @@ def test_set_brightness_C(self):
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)

def test_set_bulb_type(self):
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False, "2": 90}} # tell it which commands to support and which DPs need updating
d.set_bulb_type('C') # tell it which commands to support
d.set_version(3.1)
d._send_receive = MagicMock(return_value={})

# act
d.turn_on()

# gather results
result_cmd, result_payload = get_results_from_mock(d)

# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}

# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)

def test_not_a_bulb(self):
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False}} # tell it which commands to support and which DPs need updating
#d.set_bulb_type('C') # tell it which commands to support
d.set_version(3.1)
d._send_receive = MagicMock(return_value={})

# act
d.turn_on()

# gather results
result_cmd, result_payload = get_results_from_mock(d)

# expectations
expected_cmd = tinytuya.CONTROL
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}

# assert
self.assertEqual(result_cmd, expected_cmd)
self.assertDictEqual(result_payload, expected_payload)


if __name__ == '__main__':
unittest.main()
26 changes: 14 additions & 12 deletions tinytuya/BulbDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ def __init__(self, *args, **kwargs):
self.has_brightness = None
self.has_colourtemp = None
self.has_colour = None
self.old_retry = None
self.old_sendwait = None
self.old_persist = None
self.have_old_musicmode = False
self.tried_status = False
self.dpset = {
'switch': None,
'mode': None,
Expand All @@ -166,6 +163,7 @@ def __init__(self, *args, **kwargs):

def status(self, nowait=False):
result = super(BulbDevice, self).status(nowait=nowait)
self.tried_status = True
if result and (not self.bulb_configured) and ('dps' in result):
self.detect_bulb(result, nowait=nowait)
return result
Expand Down Expand Up @@ -406,10 +404,13 @@ def _set_values_check( self, check_values, nowait=False ):

def turn_onoff(self, on, switch=0, nowait=False):
"""Turn the device on or off"""
if switch == 0:
if not self.bulb_has_capability( 'switch', nowait=nowait ):
return error_json(ERR_FUNCTION, 'Could not detect bulb switch DP.')
return self.set_status(on, self.dpset['switch'], nowait=nowait)
if not switch:
if not self.tried_status:
self.detect_bulb( nowait=nowait )
# some people may use BulbDevice as the default even for non-bulb
# devices, so default to '1' if we can't detect it
switch = self.dpset['switch'] if self.dpset['switch'] else 1
return self.set_status(on, switch, nowait=nowait)

def turn_on(self, switch=0, nowait=False):
"""Turn the device on"""
Expand Down Expand Up @@ -842,8 +843,8 @@ def detect_bulb(self, response=None, nowait=False):
log.debug('No cached status, but nowait set! detect_bulb() exiting without detecting bulb!')
else:
response = self.status()
# return here as self.status() will call us again
return
# return here as self.status() will call us again
return
if response and 'dps' in response and isinstance(response['dps'], dict):
# Try to determine type of BulbDevice Type based on DPS indexes
# 1+2 or 20+21 are required per https://developer.tuya.com/en/docs/iot/product-function-definition?id=K9tp155s4th6b
Expand Down Expand Up @@ -884,8 +885,6 @@ def detect_bulb(self, response=None, nowait=False):
elif not self.bulb_configured:
# response has no dps
log.debug("No DPs in response, cannot detect bulb type!")
#self.bulb_type = default
#self.assume_bulb_attribs()

def set_bulb_type(self, bulb_type=None, mapping=None):
self.bulb_type = bulb_type
Expand All @@ -897,6 +896,9 @@ def set_bulb_capabilities(self, mapping):
else:
default_dpset = {}

if not isinstance( mapping, dict ):
mapping = {}

for k in self.dpset:
if k in mapping:
self.dpset[k] = mapping[k]
Expand Down
2 changes: 1 addition & 1 deletion tinytuya/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
# Colorama terminal color capability for all platforms
init()

version_tuple = (1, 17, 0) # Major, Minor, Patch
version_tuple = (1, 17, 1) # Major, Minor, Patch
version = __version__ = "%d.%d.%d" % version_tuple
__author__ = "jasonacox"

Expand Down
Loading
0