@@ -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 ]
0 commit comments