Skip to content

Commit 3b6c0f6

Browse files
authored
Merge pull request #620 from uzlonewolf/bulb-rewrite
Fix BulbDevice for non-bulb devices
2 parents 01095a1 + b2c6331 commit 3b6c0f6

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

tests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,48 @@ def test_set_brightness_C(self):
212212
self.assertEqual(result_cmd, expected_cmd)
213213
self.assertDictEqual(result_payload, expected_payload)
214214

215+
def test_set_bulb_type(self):
216+
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
217+
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False, "2": 90}} # tell it which commands to support and which DPs need updating
218+
d.set_bulb_type('C') # tell it which commands to support
219+
d.set_version(3.1)
220+
d._send_receive = MagicMock(return_value={})
221+
222+
# act
223+
d.turn_on()
224+
225+
# gather results
226+
result_cmd, result_payload = get_results_from_mock(d)
227+
228+
# expectations
229+
expected_cmd = tinytuya.CONTROL
230+
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
231+
232+
# assert
233+
self.assertEqual(result_cmd, expected_cmd)
234+
self.assertDictEqual(result_payload, expected_payload)
235+
236+
def test_not_a_bulb(self):
237+
d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY)
238+
d.status = lambda nowait=False: {"devId":"DEVICE_ID","dps":{"1": False}} # tell it which commands to support and which DPs need updating
239+
#d.set_bulb_type('C') # tell it which commands to support
240+
d.set_version(3.1)
241+
d._send_receive = MagicMock(return_value={})
242+
243+
# act
244+
d.turn_on()
245+
246+
# gather results
247+
result_cmd, result_payload = get_results_from_mock(d)
248+
249+
# expectations
250+
expected_cmd = tinytuya.CONTROL
251+
expected_payload = {"dps":{'1': True}, "devId": "DEVICE_ID_HERE","uid": "DEVICE_ID_HERE", "t": ""}
252+
253+
# assert
254+
self.assertEqual(result_cmd, expected_cmd)
255+
self.assertDictEqual(result_payload, expected_payload)
256+
215257

216258
if __name__ == '__main__':
217259
unittest.main()

tinytuya/BulbDevice.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ def __init__(self, *args, **kwargs):
140140
self.has_brightness = None
141141
self.has_colourtemp = None
142142
self.has_colour = None
143-
self.old_retry = None
144-
self.old_sendwait = None
145-
self.old_persist = None
146-
self.have_old_musicmode = False
143+
self.tried_status = False
147144
self.dpset = {
148145
'switch': None,
149146
'mode': None,
@@ -166,6 +163,7 @@ def __init__(self, *args, **kwargs):
166163

167164
def status(self, nowait=False):
168165
result = super(BulbDevice, self).status(nowait=nowait)
166+
self.tried_status = True
169167
if result and (not self.bulb_configured) and ('dps' in result):
170168
self.detect_bulb(result, nowait=nowait)
171169
return result
@@ -406,10 +404,13 @@ def _set_values_check( self, check_values, nowait=False ):
406404

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

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

890889
def set_bulb_type(self, bulb_type=None, mapping=None):
891890
self.bulb_type = bulb_type
@@ -897,6 +896,9 @@ def set_bulb_capabilities(self, mapping):
897896
else:
898897
default_dpset = {}
899898

899+
if not isinstance( mapping, dict ):
900+
mapping = {}
901+
900902
for k in self.dpset:
901903
if k in mapping:
902904
self.dpset[k] = mapping[k]

tinytuya/core/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
# Colorama terminal color capability for all platforms
9494
init()
9595

96-
version_tuple = (1, 17, 0) # Major, Minor, Patch
96+
version_tuple = (1, 17, 1) # Major, Minor, Patch
9797
version = __version__ = "%d.%d.%d" % version_tuple
9898
__author__ = "jasonacox"
9999

0 commit comments

Comments
 (0)