
    j4                        d Z ddlmZ ddlZddlmZ ddlZddlZ	 ddlmZ dZ	e	xs ej                  j                  d       Z	 eZe	r e        ej"                  Zej$                  Zej&                  Zej(                  Zej*                  Zej,                  Zej.                  Zdd	Zed
k(  r		  e        yy# e
$ r dZ	Y w xY w# e$ r Y w xY w# e$ r Y yw xY w)ax  
TinyTuya Setup Wizard Tuya based WiFi smart devices

Author: Jason A. Cox
For more information see https://github.com/jasonacox/tinytuya

Description
    Setup Wizard will prompt the user for Tuya IoT Developer credentials and will gather all
    registered Device IDs and their Local KEYs.  It will save the credentials and the device
    data in the tinytuya.json and devices.json configuration files respectively. The Wizard
    will then optionally scan the local devices for status.

    HOW to set up your Tuya IoT Developer account: iot.tuya.com:
    https://github.com/jasonacox/tinytuya#get-the-tuya-device-local-key

Credits
* Tuya API Documentation
    https://developer.tuya.com/en/docs/iot/open-api/api-list/api?id=K989ru6gtvspg
* TuyaAPI https://github.com/codetheweb/tuyapi by codetheweb and blackrozes
    The TuyAPI/CLI wizard inspired and informed this python version.
    )print_functionN)datetime)initTFwinc                 `   t         }d}	i }
d|
d<   d|
d<   d|
d<   d|
d<   d}|rd|v r|d   rd	}|d   }	 t        |      5 }t        j                  |      }d
d
d
       |	D ]  }s||v s||   s||   |
|<    	 |r|	D ]  }||v s||   s||   |
|<   d	} |s|	D ]
  }|
|   r	d} t	        j
                  t              }|sg }| xr t        } t	        j                  |       \	  }}}}}}}}}t        |dz   |z   dt        j                  z  z   |z          t        d       |r|
d   dk7  r|
d   dk7  r|
d   dk7  rwd	}d|
vs|
d   sdn|
d   }t        d|z   dz   |z   d|
d   d|
d   d|d|
d   z          t        d       |s,t        |dz   |z   dz         }|dd j                         dk(  rd}|r|r|st        d       t        |dz   |z   dz   |z   dz   |z         |
d<   t        |dz   |z   dz   |z   dz   |z         |
d<   t        |dz   |z   dz   |z   dz   |z         |
d<   t        d|z   d z   |z   d!z   d"z   d#z   d$z   d%z   d&z   d'z          t        |dz   |z   d(z   |z   d)z   |z         |
d<   t        j                  |
d*+      }t        |d,      5 }|j                  |       d
d
d
       t        |d-z   |z          t        ||z          |r|}nd|
v r|
d   r|
d   j                         j                         d.k(  rfd|
d<   t        d/       t        j                   j#                  d	d	dd	d0      }|D ]  }t        d1|z         ||
d<    n |
d   st        d2|z   d3z          y
t	        j$                  dVi |
} | j&                  rd4| j&                  v r| j&                  d4   nd5}!t        d2|z   d6z   |z   |!z          d7t)        |!      j                         v sd8t)        |!      v rt        |d9z   |z   d:z          t        d;       t        d<       y
|rd=}nt        |d>z   |z   dz         }t+        |dd j                         dk(         }"| j-                  d	||"?      }t/        |      t0        k7  rud4|v r|d4   nd5}!t        d2|z   d6z   |z   |!z          d7t)        |!      j                         v sd8t)        |!      v rt        |d9z   |z   d:z          t        d;       t        d@       y
|D ]  }dA|v r|dA   r|dA   |dB<   |dA= dC|v s|dC   s"dB|v r|dB   r,d|dB<   dD|v s6|dD   s<dE|vrd|dE<   d	}#|D ]6  }$dE|$vs|$dE   |dE   k(  rdD|$v s|$dD   s|dD   |$dD   k(  s*dC|$vs|$dC   r4|$}# n |#s|#dE   |dB<    t        d2|z   dFz   |z          t        j                  |d*+      }%t        |%       t        |dGz   |z   dHz   t        z          t        t        d,      5 }|j                  |%       d
d
d
       t        |dIt3        |      z  z          |st        |dGz   |z   dJz   t4        z          t4        dK j6                  t9        j:                         j=                         t        j                  dL| j>                  d<   	 t        t4        d,      5 }|j                  t        j                  | j>                  d*+             d
d
d
       |rd}n|rd=}nt        |dNz   |z   dz         }|j                         jA                  d      dk  r>tB        t        j                   _!        t        j                   jE                  || |d|O      }&i }'d}#|&D ]9  }(dP|(v s|(dP   s|(dP   })dQ|(v r
|(dQ   r|(dQ   nd}*dR|(v r
|(dR   r|(dR   nd}+|*|+f|'|)<   ; tG        t3        |            D ]9  }||   dE   })|)|'v s|'|)   d   ||   dQ<   |'|)   d   ||   dR<   |'|)   d   s5|#dz  }#; |#rmt        j                  |d*+      }%t        |dGz   |z   dSz   t        z          t        t        d,      5 }|j                  |%       d
d
d
       t        |dT|#z  z          t        dU       y
# 1 sw Y   xY w#  Y xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w#  t        d2|z   dMz   |z          Y xY w# 1 sw Y   ~xY w)Wa  
    TinyTuya Setup Wizard Tuya based WiFi smart devices

    Parameter:
        color = True or False, print output in color [Default: True]
        retries = Number of retries to find IP address of Tuya Devices
        forcescan = True or False, force network scan for device IP addresses

    Description
        Setup Wizard will prompt user for Tuya IoT Developer credentials and will gather all of
        the Device IDs and their Local KEYs.  It will save the credentials and the device
        data in the tinytuya.json and devices.json configuration files respectively.

        HOW to set up your Tuya IoT Developer account: iot.tuya.com:
        https://github.com/jasonacox/tinytuya#get-the-tuya-device-local-key

    Credits
    * Tuya API Documentation
        https://developer.tuya.com/en/docs/iot/open-api/api-list/api?id=K989ru6gtvspg
    * TuyaAPI https://github.com/codetheweb/tuyapi by codetheweb and blackrozes
        The TuyAPI/CLI wizard inspired and informed this python version.
    )apiKey	apiSecret	apiRegionapiDeviceID r   r	   r
   r   TfileFNzTinyTuya Setup Wizardz [%s]z<None>z    zExisting settings:z
        API Key=z 
        Secret=z
        DeviceID=z
        Region=z    Use existing credentials z(Y/n): r      nz
    Enter zAPI Keyz from tuya.com: z
API Secretzany Device IDzV currently registered in Tuya App (used to pull full list) or 'scan' to scan for one: z
      zRegion Listz)
        cn	China Data Center (alias: AY)z8
        us	US - Western America Data Center (alias: AZ)z:
        us-e	US - Eastern America Data Center (alias: UE)z&
        eu	Central Europe Data Centerz4
        eu-w	Western Europe Data Center (alias: WE)z
        in	India Data Centerz"
        sg	Singapore Data Center
zYour Regionz/ (Options: cn, us, us-e, eu, eu-w, in, or sg):    )indentwz 
>> Configuration Data Saved to scanz 
Scanning to find a Device ID...)verbosepollbyID
show_timer
maxdevicesz
Scan found Device ID %rz

zAScan failed to detect a device, please enter a Device ID manuallyPayloadzUnknown ErrorzError from Tuya server: 
permission1010zHint: z9This may indicate your Tuya IoT subscription has expired.zJ      Visit https://iot.tuya.com to check and renew your IoT Core service.zCheck API Key and Secretyz
Download DP Name mappings? )oldlistinclude_mapzCheck DeviceID and Region
gateway_idparentsubkeyidzDevice Listing
z
>> zSaving list to z    %d registered devices savedz$Saving raw TuyaPlatform response to zFull raw list of Tuya devices.)namedescriptionaccountdatetinytuyazUnable to save raw filez
Poll local devices? )colorscantimesnapshot	forcescangwIdipversionzSaving IP addresses to z     %d device IP addresses foundz
Done.
 )$
CONFIGFILEopenjsonloadr(   load_devicefile
DEVICEFILE
HAVE_COLOR	termcolorprintr/   inputlowerdumpswritestripscannerdevicesClouderrorstrbool
getdevicestypelistlenRAWFILEr   r   now	isoformatgetdevices_rawfindSNAPSHOTFILEpoll_and_displayrange),r)   retriesr,   nocloud
assume_yesdiscovercredentials	skip_pollconfig_fileconfig_keysconfigneedconfigsffile_configkold_devicesboldsubboldnormaldimalertalertdimcyanredyellowr   answerjson_objectoutfiletuyadevicesdevdevidclouderrr   foundr    outputresultiplistitmgwidr.   vers,                                               </DATA/.local/lib/python3.12/site-packages/tinytuya/wizard.pywizardrx   <   s0
   0 KEKFF8F;F;F=Kv,V1D!&)	+!))A,K AqK/KN'Nq	  AKKN'Nq	# 
 A!9"  **:6K jEGOGYGYZ_G`DT7FC$V	$(
(3
.H<L<L1M
MPV
VW	"Ix(B.6+3F"3L;2%#0#>f]F[hbhivbwfw!55;h!4kk"$$ 	% 	b	7%DDvMPYYZFa{  "c)"Wb	 <!7$!>!JW!T!3"46<"= >x#Gl$:T$AL$PSZ$Z$6%79?%@ A{ %g&2'359':<K'LNU'V&~' BH'H !I} 	j7"]2S8;<JK MM 9	9
 GG 00 66 	7 $Gl$:T$AM$QT[$[$U%VX^%_ `{ jj2+s#wMM+& $d88;FGcK !F"vm'<AVA\A\A^AdAdAfjpAp$&F=!68""**ED]bop*rC2U:<(-}%  -(ftm&iik*&* ;;,5,D%++i(/C&4-"<<sBSHIs3x~~//6SX3EdXo+.iijbc,- F7%DDvMPYYZFq 1 1 3s :<< &&{P[&]$,5,D+i(/C&4-"<<sBSHIs3x~~//6SX3EdXo+.iijbc-. 3<  #L 1HL!C<CJ33x= CM |E
s? "CI)F6)VD\SY-F 6%=SZ6RW==X_dlr_r  {A  BG  {H & * $)$KCM; @ 
&4-,
,s
23ZZA.F	&M 
$.6
!$5
5

BC	j#	'f 
	#1C4DD
DEdWnv%(NNQXXY;||LLN,,. (((
V$	Dgs#wdjj)=)=aHI $ 	w!99FBYNO||~3!#(4%!!22KuW^im  zC2  EC}V6{"&#+#d)SY(1S(8S^c)nQS "Cyt  K(*Aq>$'Dv~'-d|AAt$,24LOAy)$<?EQJE + ZZA6F$.6),EE
RSj#&'f% '#:UBBC	+
s 
x $#B 
	 $#	D&4-";;cAC< '&s|   a aa %a *a 0
a a!a.8b 1a;9b b$aa a!a+.a8;b b b!$b-__main__)TNFFFTNN)__doc__
__future__r   r3   r   r(   syscoloramar   HAVE_COLORAMAImportErrorplatform
startswithr7   	raw_inputr:   	NameErrorr6   rN   r1   rI   DEFAULT_NETWORK
TCPTIMEOUTTCPPORTrx   __name__KeyboardInterruptr0       rw   <module>r      s	  , &    
M @#,,"9"9%"@@
	E
 F   
$$  



 **  



@F z {  M  		r  s5   B7 C .C 7C CCCCC