
    j)                     j    d dl Z d dlZddlmZmZ ddlmZ  e j                  e      Z	 G d de      Z
y)    N   )XenonDevicemerge_dps_results)command_typesc                   R    e Zd ZddZd ZddZddZddZddZddZ	dd	Z
dd
Zy)Devicec                     t        |t              rt        |      }| j                  t        j
                  ||i      }| j                  ||       }t        j                  d|       |S )z
        Set status of the device to 'on' or 'off'.

        Args:
            on(bool):  True for 'on', False for 'off'.
            switch(int): The switch to set
            nowait(bool): True to send without waiting for response.
        getresponsezset_status received data=%r)	
isinstanceintstrgenerate_payloadCTCONTROL_send_receivelogdebug)selfonswitchnowaitpayloaddatas         A/DATA/.local/lib/python3.12/site-packages/tinytuya/core/Device.py
set_statuszDevice.set_status   s\     fc"[F''

VRLA!!'F
!D		/6    c                     | j                  t        j                        }| j                  |d      }t        j                  d|       |S )zE
        Request AP_CONFIG Product Info from device. [BETA]

        r   zproduct received data=%r)r   r   	AP_CONFIGr   r   r   )r   r   r   s      r   productzDevice.product$   s>     ''5!!'1-		,d3r   c                     | j                  t        j                        }| j                  |d|       }t        j                  d|       |S )z
        Send a keep-alive HEART_BEAT command to keep the TCP connection open.

        Devices only send an empty-payload response, so no need to wait for it.

        Args:
            nowait(bool): True to send without waiting for response.
        r   r
   zheartbeat received data=%r)r   r   
HEART_BEATr   r   r   )r   r   r   r   s       r   	heartbeatzDevice.heartbeat/   sE     ''6!!'1v:!G		.5r   Nc                     |dg}t         j                  d| j                         | j                  t        j
                  |      }| j                  |d|       }t         j                  d|       |S )z
        Request device to update index.

        Args:
            index(array): list of dps to update (ex. [4, 5, 6, 18, 19, 20])
            nowait(bool): True to send without waiting for response.
        r   z"updatedps() entry (dev_type is %s)r   r
   zupdatedps received data=%r)r   r   dev_typer   r   	UPDATEDPSr   )r   indexr   r   r   s        r   	updatedpszDevice.updatedps>   sf     =CE		6F''e<!!'1v:!G		.5r   c                     t        |t              rt        |      }| j                  t        j
                  ||i      }| j                  ||       }|S )z
        Set int value of any index.

        Args:
            index(int): index to set
            value(int): new value for the index
            nowait(bool): True to send without waiting for response.
        r
   )r   r   r   r   r   r   r   )r   r'   valuer   r   r   s         r   	set_valuezDevice.set_valueP   sM     eS!JE''

UENC!!'F
!Dr   c                    |r| j                   dkD  r:t        |      | j                   kD  r"d}|D ]  }| j                  |||   |      } |S i }|D ]  }||   |t        |      <    | j	                  t
        j                  |      }| j                  ||       S | j                   dkD  rht        |      | j                   kD  rPi }|D ]G  }|s t        |      rt        j                  d       | j                  |||   |      }t        ||       I |S i }|D ]  }||   |t        |      <    | j	                  t
        j                  |      }| j                  ||       }|ryd|v rut        |      dkD  rgt        t        |            }| j                  |||   |      }	||= |	r7d|	vr3d| _         |	}|D ]%  }| j                  |||   |      }	t        ||	       ' |S )z
        Set multiple indexes at the same time

        Args:
            data(dict): array of index/value pairs to set
            nowait(bool): True to send without waiting for response.
        r   N)r   r
   r   Err)max_simultaneous_dpslenr+   r   r   r   r   r   booltimesleepr   nextiter)
r   r   r   retkoutr   resultfirst_dpress
             r   set_multiple_valueszDevice.set_multiple_valuesc   s    ((1,TT=V=V1VA..DGF.CC 
 A"&q'CAK //

C@))'F
)LL$$q(SY9R9R-RCDIJJqM476B!#v.	 
 J Aq'CAK  ''

C8##Gf*#Fevo#c(Q,D#K(H..3x=.HCHuC',-)A..CF6.BC%fc2  r   c                 (    | j                  d||      S )zTurn the device onTr   r   r   r   s      r   turn_onzDevice.turn_on   s    tVV44r   c                 (    | j                  d||      S )zTurn the device offFr=   r>   s      r   turn_offzDevice.turn_off   s    uff55r   c                 h   |dk(  r`| j                         }d|v r4|d   }t        |j                               }|j                          |d   }nt        j                  d|       |S | j                  t        j                  ||i      }| j                  ||       }t        j                  d|       |S )z
        Set a timer.

        Args:
            num_secs(int): Number of seconds
            dps_id(int): DPS Index for Timer
            nowait(bool): True to send without waiting for response.
        r   dpszset_timer received error=%rr
   zset_timer received data=%r)
statuslistkeyssortr   r   r   r   r   r   )	r   num_secsdps_idr   rE   devicesdevices_numbersr   r   s	            r   	set_timerzDevice.set_timer   s     Q;[[]F -"&w||~"6$$&(,		7@''

VX4FG!!'F
!D		.5r   )r   F)T)NF)F)r   F)__name__
__module____qualname__r   r    r#   r(   r+   r;   r?   rA   rM    r   r   r   r      s0    &	$&6p56r   r   )loggingr1   r   r    r   r   	getLoggerrN   r   r   rQ   r   r   <module>rU      s5      7 ! g!p[ pr   