@@ -197,6 +197,17 @@ def _get_listinfo_elements(self, entity_type):
197197        _LOGGER .debug (dom )
198198        return  dom .findall ("*" )
199199
200+     def  wait_device_txbusy (self , ain , retries = 10 ):
201+         """Wait for device to finish command execution.""" 
202+         for  _  in  range (retries ):
203+             plain  =  self .get_device_infos (ain )
204+             dom  =  ElementTree .fromstring (plain )
205+             txbusy  =  dom .findall ("txbusy" )
206+             if  txbusy [0 ].text  ==  "0" :
207+                 return  True 
208+             time .sleep (0.2 )
209+         return  False 
210+ 
200211    def  get_device_elements (self ):
201212        """Get the DOM elements for the device list.""" 
202213        return  self ._get_listinfo_elements ("device" )
@@ -223,6 +234,10 @@ def get_device_by_ain(self, ain):
223234        """Return a device specified by the AIN.""" 
224235        return  self .get_devices_as_dict ()[ain ]
225236
237+     def  get_device_infos (self , ain ):
238+         """Get the device infos.""" 
239+         return  self ._aha_request ("getdeviceinfos" , ain = ain )
240+ 
226241    def  get_device_present (self , ain ):
227242        """Get the device presence.""" 
228243        return  self ._aha_request ("getswitchpresent" , ain = ain , rf = bool )
@@ -235,17 +250,23 @@ def get_switch_state(self, ain):
235250        """Get the switch state.""" 
236251        return  self ._aha_request ("getswitchstate" , ain = ain , rf = bool )
237252
238-     def  set_switch_state_on (self , ain ):
253+     def  set_switch_state_on (self , ain ,  wait = False ):
239254        """Set the switch to on state.""" 
240-         return  self ._aha_request ("setswitchon" , ain = ain , rf = bool )
255+         result  =  self ._aha_request ("setswitchon" , ain = ain , rf = bool )
256+         wait  and  self .wait_device_txbusy (ain )
257+         return  result 
241258
242-     def  set_switch_state_off (self , ain ):
259+     def  set_switch_state_off (self , ain ,  wait = False ):
243260        """Set the switch to off state.""" 
244-         return  self ._aha_request ("setswitchoff" , ain = ain , rf = bool )
261+         result  =  self ._aha_request ("setswitchoff" , ain = ain , rf = bool )
262+         wait  and  self .wait_device_txbusy (ain )
263+         return  result 
245264
246-     def  set_switch_state_toggle (self , ain ):
265+     def  set_switch_state_toggle (self , ain ,  wait = False ):
247266        """Toggle the switch state.""" 
248-         return  self ._aha_request ("setswitchtoggle" , ain = ain , rf = bool )
267+         result  =  self ._aha_request ("setswitchtoggle" , ain = ain , rf = bool )
268+         wait  and  self .wait_device_txbusy (ain )
269+         return  result 
249270
250271    def  get_switch_power (self , ain ):
251272        """Get the switch power consumption.""" 
@@ -267,7 +288,7 @@ def get_target_temperature(self, ain):
267288        """Get the thermostate target temperature.""" 
268289        return  self ._get_temperature (ain , "gethkrtsoll" )
269290
270-     def  set_target_temperature (self , ain , temperature ):
291+     def  set_target_temperature (self , ain , temperature ,  wait = False ):
271292        """Set the thermostate target temperature.""" 
272293        temp  =  int (16  +  ((float (temperature ) -  8 ) *  2 ))
273294
@@ -277,20 +298,23 @@ def set_target_temperature(self, ain, temperature):
277298            temp  =  254 
278299
279300        self ._aha_request ("sethkrtsoll" , ain = ain , param = {"param" : temp })
301+         wait  and  self .wait_device_txbusy (ain )
280302
281-     def  set_window_open (self , ain , seconds ):
303+     def  set_window_open (self , ain , seconds ,  wait = False ):
282304        """Set the thermostate target temperature.""" 
283305        endtimestamp  =  int (time .time () +  seconds )
284306
285307        self ._aha_request (
286308            "sethkrwindowopen" , ain = ain , param = {"endtimestamp" : endtimestamp }
287309        )
310+         wait  and  self .wait_device_txbusy (ain )
288311
289-     def  set_boost_mode (self , ain , seconds ):
312+     def  set_boost_mode (self , ain , seconds ,  wait = False ):
290313        """Set the thermostate to boost mode.""" 
291314        endtimestamp  =  int (time .time () +  seconds )
292315
293316        self ._aha_request ("sethkrboost" , ain = ain , param = {"endtimestamp" : endtimestamp })
317+         wait  and  self .wait_device_txbusy (ain )
294318
295319    def  get_comfort_temperature (self , ain ):
296320        """Get the thermostate comfort temperature.""" 
@@ -307,35 +331,40 @@ def get_device_statistics(self, ain):
307331
308332    # Lightbulb-related commands 
309333
310-     def  set_state_off (self , ain ):
334+     def  set_state_off (self , ain ,  wait = False ):
311335        """Set the switch/actuator/lightbulb to on state.""" 
312336        self ._aha_request ("setsimpleonoff" , ain = ain , param = {"onoff" : 0 })
337+         wait  and  self .wait_device_txbusy (ain )
313338
314-     def  set_state_on (self , ain ):
339+     def  set_state_on (self , ain ,  wait = False ):
315340        """Set the switch/actuator/lightbulb to on state.""" 
316341        self ._aha_request ("setsimpleonoff" , ain = ain , param = {"onoff" : 1 })
342+         wait  and  self .wait_device_txbusy (ain )
317343
318-     def  set_state_toggle (self , ain ):
344+     def  set_state_toggle (self , ain ,  wait = False ):
319345        """Toggle the switch/actuator/lightbulb state.""" 
320346        self ._aha_request ("setsimpleonoff" , ain = ain , param = {"onoff" : 2 })
347+         wait  and  self .wait_device_txbusy (ain )
321348
322-     def  set_level (self , ain , level ):
349+     def  set_level (self , ain , level ,  wait = False ):
323350        """Set level/brightness/height in interval [0,255].""" 
324351        if  level  <  0 :
325352            level  =  0   # 0% 
326353        elif  level  >  255 :
327354            level  =  255   # 100 % 
328355
329356        self ._aha_request ("setlevel" , ain = ain , param = {"level" : int (level )})
357+         wait  and  self .wait_device_txbusy (ain )
330358
331-     def  set_level_percentage (self , ain , level ):
359+     def  set_level_percentage (self , ain , level ,  wait = False ):
332360        """Set level/brightness/height in interval [0,100].""" 
333361        if  level  <  0 :
334362            level  =  0 
335363        elif  level  >  100 :
336364            level  =  100 
337365
338366        self ._aha_request ("setlevelpercentage" , ain = ain , param = {"level" : int (level )})
367+         wait  and  self .wait_device_txbusy (ain )
339368
340369    def  _get_colordefaults (self , ain ):
341370        plain  =  self ._aha_request ("getcolordefaults" , ain = ain )
@@ -353,7 +382,7 @@ def get_colors(self, ain):
353382            colors [name ] =  values 
354383        return  colors 
355384
356-     def  set_color (self , ain , hsv , duration = 0 , mapped = True ):
385+     def  set_color (self , ain , hsv , duration = 0 , mapped = True ,  wait = False ):
357386        """Set hue and saturation. 
358387
359388        hsv: HUE colorspace element obtained from get_colors() 
@@ -369,6 +398,7 @@ def set_color(self, ain, hsv, duration=0, mapped=True):
369398        else :
370399            # undocumented API method for free color selection 
371400            self ._aha_request ("setunmappedcolor" , ain = ain , param = params )
401+         wait  and  self .wait_device_txbusy (ain )
372402
373403    def  get_color_temps (self , ain ):
374404        """Get temperatures supported by this lightbulb.""" 
@@ -378,31 +408,35 @@ def get_color_temps(self, ain):
378408            temperatures .append (temp .get ("value" ))
379409        return  temperatures 
380410
381-     def  set_color_temp (self , ain , temperature , duration = 0 ):
411+     def  set_color_temp (self , ain , temperature , duration = 0 ,  wait = False ):
382412        """Set color temperature. 
383413
384414        temperature: temperature element obtained from get_temperatures() 
385415        duration: Speed of change in seconds, 0 = instant 
386416        """ 
387417        params  =  {"temperature" : int (temperature ), "duration" : int (duration ) *  10 }
388418        self ._aha_request ("setcolortemperature" , ain = ain , param = params )
419+         wait  and  self .wait_device_txbusy (ain )
389420
390421    # blinds 
391422    # states: open, close, stop 
392423    def  _set_blind_state (self , ain , state ):
393424        self ._aha_request ("setblind" , ain = ain , param = {"target" : state })
394425
395-     def  set_blind_open (self , ain ):
426+     def  set_blind_open (self , ain ,  wait = False ):
396427        """Set the blind state to open.""" 
397428        self ._set_blind_state (ain , "open" )
429+         wait  and  self .wait_device_txbusy (ain )
398430
399-     def  set_blind_close (self , ain ):
431+     def  set_blind_close (self , ain ,  wait = False ):
400432        """Set the blind state to close.""" 
401433        self ._set_blind_state (ain , "close" )
434+         wait  and  self .wait_device_txbusy (ain )
402435
403-     def  set_blind_stop (self , ain ):
436+     def  set_blind_stop (self , ain ,  wait = False ):
404437        """Set the blind state to stop.""" 
405438        self ._set_blind_state (ain , "stop" )
439+         wait  and  self .wait_device_txbusy (ain )
406440
407441    # Template-related commands 
408442
0 commit comments