
    j4                     b    d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m	Z	  G d de	      Z
y)	a
  
 A community-contributed Python module to add support for Tuya WiFi smart universal RF remote controller

 This module attempts to provide everything needed so there is no need to import the base tinytuya module

 Module Author: uzlonewolf (https://github.com/uzlonewolf)
 Based on IRRemoteControlDevice by Alexey 'Cluster' Avdyukhin (https://github.com/clusterm)

 Local Control Classes
    RFRemoteControlDevice(..., version=3.3)
        This class uses a default version of 3.3
        See OutletDevice() for the other constructor arguments

    Functions:
        rf = RFRemoteControlDevice(..., control_type=None)
            -> will immediately connect to the device to try and detect the control type if control_type is not provided
               control_type=1 for older devices using DPS 201/202
               control_type=2 for newer devices using DPS 1-13

        rf.send_command( mode, data={} )
            -> sends a command to the device
               IRRemoteControlDevice.send_command() is used when mode is not 'rf_study', 'rfstudy_exit', 'rfstudy_send',
                   'rf_shortstudy', 'rfshortstudy_exit', or 'send_cmd'

        rf.rf_study_start( freq=0, short=False )
        rf.rf_study_end( freq=0, short=False )
            -> start or end a study session
               freq=0 auto-detects the frequency, or it can be specified as i.e. freq="433.92" or freq="315"
               when sort=True, 'rf_shortstudy' is used instead of 'rf_study'

        rf.rf_receive_button( freq=0, timeout )
            -> call this method and press button on real remote control to read its code in Base64 format
               freq - 0 to auto-detect
               timeout - maximum time to wait for button press

        rf.rf_send_button( base64_code, times=6, delay=0, intervals=0 )
            -> send a learned (raw base64-encoded) button press

        rf.rf_send_key( keys, cmt_bank, system_bank, frequency_bank, datarate_bank, baseband_bank, tx_bank, mode=8, freq=0, rate=0 )
            -> send pre-defined key(s)
               The *_bank values are directly copied from CMOSTEK's RFPDK software (select chip "CMT2300A")
              'keys' can be:
                  a dict containing 'code', 'delay', 'intervals', and  'times'
                  a single hex string
                  a list or tuple containing dicts or hex strings

        RFRemoteControlDevice.rf_print_button( base64_code )
            -> prints and returns the JSON dict as a string from a base64-encoded learned button
                the base64 string is base64 decoded but not JSON parsed

        RFRemoteControlDevice.rf_decode_button( base64_code )
            -> returns the JSON dict as a dict from a base64-encoded learned button
                the base64 string is base64 decoded and then JSON parsed

    N   )logCONTROL   )IRRemoteControlDevicec                   l     e Zd Zi f fd	Zd	dZd	dZd
dZddZddZe	dd       Z
e	d        Z xZS )RFRemoteControlDevicec                    |dv rd|vs|d   sd|d<   d|vs|d   sd|d<   d|vs|d   sd|d<   |dk(  rd	|vrd
|d	<   d|vrd
|d<   |d   }t         j                  |d|d   dt        |d         d	|d	   d|d   d|i}t        dd      D ](  }d|z  }||v st	        ||         }d|vr||d<   |||<   * n!t         j                  |d|d   d|d   d|d   i}| j                  t         j                  t        j                  |      d       y |dk(  rI||t         j                  <   | j                  t         j                  t        j                  |      d       y t        t         | +  ||       y )N)rf_studyrfstudy_exitrfstudy_sendrf_shortstudyrfshortstudy_exitrf_typesub_2gfreq0ver2r   moder   ratefeqr   
   key%d	study_feqT)nowaitsend_cmd)r	   NSDP_CONTROLintrangedict	set_value
DP_SEND_IRjsondumpssupersend_command)	selfr   datar   commandikkey_data	__class__s	           S/DATA/.local/lib/python3.12/site-packages/tinytuya/Contrib/RFRemoteControlDevice.pyr'   z"RFRemoteControlDevice.send_commandF   s   ff$DO"*YT!f"VD U!U~%
 %#$DL%#$DL5k1>>iQUV_Q`bgilmqrxmyiz  }C  EI  JP  EQ  SY  [_  `f  [g  in  ps  u2A!ADy#'Q= 0.1HUO%-
 ( 2>>iQUV_Q`bmostzo{  ~C  EI  JO  EP  RNN1<<djj>QZ^N`Z7;D&334NN1<<djj>NW[N]';T4I    c                 N    dt        |      i}|rdnd}| j                  ||       y )Nr   r   r   strr'   r(   r   shortr)   cmds        r/   rf_study_startz$RFRemoteControlDevice.rf_study_starti   s)    T$!&oJ3&r0   c                 N    dt        |      i}|rdnd}| j                  ||       y )Nr   r   r   r2   r4   s        r/   rf_study_endz"RFRemoteControlDevice.rf_study_endo   s*    T$%*!3&r0   c                    t        j                  d       | j                          | j                  |       d }d }d}| j                  }t        j
                         |z   }| j                  }| j                  d       	 |t        j
                         kD  rQt        t        j
                         |z
        }	|	dk  rd}	| j                  |	       t        j                  d       | j                  d       }|d k(  rt        j                  d       nt        |      t        k7  sd|vrt        j                  d	|       |}n| j                  |d   v r+t        j                  d
|       |d   | j                     }d}nm| j                  |d   v r+t        j                  d|       |d   | j                     }d}n1t        j                  d|       |}|t        j
                         kD  rQ| j                  |       |r| j!                  |       | j                  |       |s| j                  d       |S # | j                  |       w xY w)NzReceiving button)r   FTr   zWaiting for button...TimeoutdpszUnexpected response: %rzResponse (type 1): %rzResponse (type 2): %rzUnknown DPS in response: %r)r   debugr9   r7   connection_timeouttimesocketPersistentset_socketPersistentroundset_socketTimeout_send_receivetyper!   DP_LEARNED_IDinfoDP_LEARNED_REPORTrf_print_button)
r(   r   timeoutresponseresponse_codefoundold_timeoutend_at_timeold_persisttimeos
             r/   rf_receive_buttonz'RFRemoteControlDevice.rf_receive_buttonu   s   		$%$( --iikG+++!!4)"	0		+diikK7819ae&&u-		12--d3t#IIi((^t+uH/DII7B$,M''8E?:HH5xA$,UOD4F4F$GM E++x>HH5xA$,UOD4J4J$KM E II;XF$,M; 		+B "";/  -1 	&%%u. "";/s   ?E'H3 3Ic                     t        j                  d|z          | j                  |       | j                  |      }||||d}d|i}|rd|v r|d   |d<   | j	                  d|      S )NzSending Learned RF Button: )codetimesdelay	intervalskey1r   r   )r   r=   rI   rf_decode_buttonr'   )r(   base64_coderU   rV   rW   bdatarX   r)   s           r/   rf_send_buttonz$RFRemoteControlDevice.rf_send_button   sv    		0;>?k+%%{4$uuS\^ ~U5\tE{  .$88r0   c                    t        |      dk7  st        |      t        t        fvrt	        d      t        |      dk7  st        |      t        t        fvrt	        d      t        |      dk7  st        |      t        t        fvrt	        d      t        |      dk7  st        |      t        t        fvrt	        d      t        |      dk7  st        |      t        t        fvrt	        d	      t        |      d
k7  st        |      t        t        fvrt	        d      t        |      t
        k(  rd|i}nt        |      t        k(  r
d|ddddi}nxt        |      t        t        fv rSd}i }|D ]I  }d|z  }t        |      t
        k(  r|||<    t        |      t        k(  r|dddd||<   =t	        d|z         nt	        d|z        dddd}|D ]4  }|D ]-  }|||   vst        ||   |         t        k7  s#||   ||   |<   / 6 d|d<   ||d<   |	|d<   |
|d<   ||||||d|d<   t        j                  d|       | j                  d|      S )a  
        'keys' can be:
          a dict containing 'code', 'delay', 'intervals', and  'times'
          a single hex string
          a list or tuple containing dicts or hex strings

        The *_bank values are directly copied from CMOSTEK's RFPDK software (select chip "CMT2300A")
        Example:
          Baseband "b":  [2,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,0,0,0,96,255,0,0,31,16]
          CMT "c":       [0,102,236,28,240,128,20,8,145,2,2,208]
          System "s":    [174,224,53,0,0,244,16,226,66,32,0,129]
          Data Rate "d": [63,30,128,204,0,0,0,0,0,0,0,41,192,218,33,75,5,0,80,45,0,1,5,5]
          TX "t":        [81,154,12,0,12,176,0,31,4,63,127]
          Frequency "f": [66,113,206,28,66,91,28,28]
           z#CMT Bank list/tuple size must be 12z&System Bank list/tuple size must be 12   z(Frequency Bank list/tuple size must be 8   z)Data Rate Bank list/tuple size must be 24   z(Baseband Bank list/tuple size must be 29   z"TX Bank list/tuple size must be 11rX   r      )rT   rV   rW   rU   r   r   z,rf_send_key(): Unknown data type for key: %rz-rf_send_key(): Unknown data type for keys: %r)rV   rW   rU   r   r   r   r   r   )csfdbtcfgzSending Keys: %rr   )lenrE   listtuple
ValueErrorr!   r3   r   r   rG   r'   )r(   keyscmt_banksystem_bankfrequency_bankdatarate_bankbaseband_banktx_bankr   r   r   r)   r+   r,   kkeydefaultrg   s                    r/   rf_send_keyz!RFRemoteControlDevice.rf_send_key   sl     x=B$x.u"ECEE{r!T+%6tUm%KFHH~!#tN';D%='PHJJ}#tM':4-'OIKK}#tM':4-'OHJJw<2gtUm!CBDD :T#D$Z3taaRSUWD$ZD%=(AD{7d?!"DJ!W^+,qqST!VDJ$&TWX&XZZ  MPTTVVQ<ADG#tDGAJ'73'>!(DGAJ  
 #YVUV%KnS`gt  |C  EU$d,  *d44r0   c                     |st         }	 t        j                  |       }|j                  d|       |S #  |j                  d|        Y y xY w)NzLearned button: %sz#Failed to decode learned button: %r)r   base64	b64decoder=   )rZ   use_logjstrs      r/   rI   z%RFRemoteControlDevice.rf_print_button  sK    #	##[2DMM/7K	MM@+Os	   (3 A	c                 j    	 t        j                  |       }t        j                  |      }|S #  Y y xY wN)rz   r{   r$   loads)rZ   r}   jdatas      r/   rY   z&RFRemoteControlDevice.rf_decode_button  s4    	##[2DJJ&EL	s   +. 2)r   F)r      )   r   r   )r_   r   r   r   )__name__
__module____qualname__r'   r7   r9   rR   r\   rx   staticmethodrI   rY   __classcell__)r.   s   @r/   r	   r	   E   sP    ') !JF''=~9>5@ 	 	  r0   r	   )__doc__rz   r$   loggingstructr?   corer   r   r   r	    r0   r/   <module>r      s1   6p       8P1 Pr0   