
    Mg!                         S r SSKJr  SSKJrJr  SSKJr  SSKrSSKJr  SSK	r	\" SS9\" S	5       " S
 S5      5       5       r
g)zYeelight IoT plugin.

This plugin controls all the yeelight
smart bulbs in the user's smart home.

Typical usage example:

>yeelight
>bedroom on

@Author: Leonidha Mara @leonidhaMara & Emmanouil Manousakis @manousakis01
@Date: 21st June 2022
    )Fore)pluginrequire)discover_bulbsN)ListT)networkyeelightc                       \ rS rSrS rS rS rS rS rS r	S r
S	\4S
 jrS rS\4S jrS\4S jrS\4S jrS\4S jrS\4S jrS\S\4S jrS\4S jrSrg)Yeelight   c                 0    Xl         U R                  5         g )N)jarvismain)selfr   ss      >/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/yeelight.py__call__Yeelight.__call__   s    		    c                      0 U l         SU l        g )Nexit)discovered_bulbsexit_msg)r   s    r   __init__Yeelight.__init__   s     "r   c                 `   U R                   R                  S[        R                  S9  U R	                  5         U R                  5       (       a  U R                  5          U R                  5         U R                   R                  S[        R                  S9R                  5       nU R                  US   5      (       a  g US   R                  5       S:X  aS  U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        GOU R                   HW  nU R                  U   S	   R                  5       US   R                  5       :X  a  Un  OUS   R                  5       S
:X  d  MU  S nMY     US   R                  5       S:X  ax  [        W5      [        :X  aT  [         R"                  " U5      R%                  5         U R                   R                  SU R                  U   S	    S35        OU R'                  5         OUS   R                  5       S:X  aw  [        W5      [        :X  aT  [         R"                  " U5      R)                  5         U R                   R                  SU R                  U   S	    S35        OU R+                  5         GMQ  )NzTo exit enter word 'exit'colorz!Command (Use 'help' for details):r   helpz[ bulb name | all][status]zA-The first argument is the name of the bulb or the keyword "all".zK-The second argument specify turning on or off the light or all the lights.nameall   onBulb  is on.off is off.)r   sayr   YELLOWdiscoverfind_unknown_bulbs
name_bulbsdisplay_condinputGREENsplitis_exit_inputlowerr   typestrr	   Bulbturn_onpower_on_allturn_offpower_off_all)r   cmdiips       r   r   Yeelight.main"   s   34;;G""$$OO ++##34:: $ GGLuw !!#a&))1v||~' <=WYac ..A,,Q/7==?3q6<<>QQ50! / q6<<>T)Bx3 b)113#D$9$9"$=f$E#FgNP ))+V\\^u,Bx3 b)224#D$9$9"$=f$E#FhOQ **,U r   c           	          [        U R                  5       H[  u  pU R                  R                  SU R                  U   S    S[        R
                  " U5      R                  5       S    35        M]     g)zE
Display the status of all the lights that are connected to the Lan.
zName: r    z   Status: powerN)	enumerater   r   r(   r	   r5   get_properties)r   vr<   s      r   r-   Yeelight.display_condV   sg     t445EAKKOO..r26:;;VXHYHhHhHjkrHsFtuw 6r   c                 $   U R                    H  n[        R                  " U5      R                  5         U R                   U   S   nU(       a!  U R                  R                  SU S35        Ma  U R                  R                  SU S35        M     g)z
Turn on all the lights
r    r$   r%   N)r   r	   r5   r6   r   r(   r   r<   r    s      r   r7   Yeelight.power_on_all^   ss     ''BMM"%%'((,V4D%vW 56%t7 34 (r   c                 $   U R                    H  n[        R                  " U5      R                  5         U R                   U   S   nU(       a!  U R                  R                  SU S35        Ma  U R                  R                  SU S35        M     g)z
Turn off all the lights
r    r$   r'   N)r   r	   r5   r8   r   r(   rE   s      r   r9   Yeelight.power_off_allj   ss     ''BMM"&&(((,V4D%vX 67%t8 45 (r   c                     [        5       nU HA  nUS   S   SS.U R                  US   '   SUS   S   ;   d  M,  SU R                  US      S	'   MC     g
)zr
Filter the respond and keep only the necessary attributes of the light.
Ip , name (if exists) and rgb capability
capabilitiesr    F)r    has_rgbr<   set_rgbsupportTrK   N)r   r   )r   
discoveredbulbs      r   r*   Yeelight.discoverv   sg    
 $%
D^,V41HD!!$t*-D0;;?C%%d4j1)<	 r   upper_boundc                    SnUS:  aU  SnU(       dL   U R                   R                  S5      nU R                  [        U5      U5      (       a  [        e U(       d  ML  [        U5      $ ! [         aU    U R                  U5      (       a  U R                  s $ U R                   R                  SU S3[        R                  S9  Sn Nrf = f)zS
Get user input and validate it.
Input must be a number that corresponds to an IP.
r   Nz0Choose the number that corresponds to the bulb: zPlease select a number (0 - )r   )
r   r.   is_valid_inputint
ValueErrorr1   r   r(   r   r)   )r   rQ   
input_codes      r   get_bulb_numberYeelight.get_bulb_number   s    
 
?J 
&!%!2!2J"MJ**3z?KHH(( I	 !j : " &))*55#}},KKOO6{m1ET[[ $ Z!%J&s   AA) ),C.CCc                 >   SnU(       dB   U R                   R                  S5      nU R                  U5      (       d  [        e U(       d  MB  U$ ! [         aD    U R	                  U5      (       a  U R
                  s $ U R                   R                  S5        Sn NXf = f)zR
Get user input and validate it.
Bulb name must not be empty or an existing name.
 zPlease enter a name: zBChosen name is empty or exists. Also tou should use one-word names)r   r.   is_available_namerV   r1   r   r(   )r   
input_names     r   validate_nameYeelight.validate_name   s    
 
 ![[../FG
--j99$$ : *    %%j11==( de
	 s   7A ,B<BBreturnc                    U R                  5       n U R                  R                  [        U5       S35        [	        U5       H%  u  p#U R                  R                  U SU 35        M'     U(       d)  U R                  R                  S[
        R                  S9  g	U R                  [        U5      S-
  5      nU R                  U5      (       a  U R                  $ U R                  R                  S[
        R                  S9   [        S5       H{  n[        R                  " X   5      R                  5         [        R                   " S5        [        R                  " X   5      R#                  5         [        R                   " S5        M}     U R%                  5       n[        R                  " X   5      R'                  U5        X`R(                  X      S'   UR+                  U5        GM  )
z
This method is for naming the user's smart bulbs,
to be able to controll them with comfort.
All bulbs should have a name for the user to be
able to controll them.
z unknown bulb(s) found!z. zAll bulbs are named!r   r"   zThe chosen bulb will flash now.   r    N)r+   r   r(   lenr@   r   r/   rX   r1   r   r)   ranger	   r5   r8   timesleepr6   r^   set_namer   pop)r   unknown_bulbscountrO   r.   r;   r    s          r   r,   Yeelight.name_bulbs   s    //1KKOOs=122IJK(75'D6 23  8  6djjI,,S-?!-CD%%e,,==(KKOO9 $ N	 #1X m&:;DDF

1 m&:;CCE

1	 &
  --/DMM-"67@@FJN))-*>?G!%%e,5 r   c                 j    [        U5      [        :X  a   UR                  5       U R                  :X  a  gg g NT)r3   r4   r2   r   )r   r.   s     r   r1   Yeelight.is_exit_input   s+    K35;;=DMM#A $Br   c                 Z    [        U5      [        :X  a  U R                  X5      (       a  gg g rm   )r3   rU   is_out_of_range)r   r.   rQ   s      r   rT   Yeelight.is_valid_input   s*    ;#$"6"6u"J"J #Kr   c                     US:w  am  [        U5      [        :X  aY  UR                  5       S:w  aD  U R                  U5      (       a-  [	        UR                  5       R                  5       5      S:X  a  gg g g g g )Nr[   r   r"   T)r3   r4   r2   is_valid_namerc   stripr0   )r   r    s     r   r\   Yeelight.is_available_name   sx    BJ4:,1GDL^L^_cLdLdilmqmwmwmymm  nB  jC  GH  jH jHLd1G,Jr   c                 Z    U R                    H  nU R                   U   S   U:X  d  M    g   g)Nr    FTr   )r   r    rO   s      r   rs   Yeelight.is_valid_name   s2    ))D$$T*62d: * r   xc                      X:  d  US::  a  S$ S$ )Nr   TF )r   ry   rQ   s      r   rp   Yeelight.is_out_of_range   s    AF??r   c                 t    U R                    Vs/ s H  oR                   U   S   S:X  d  M  UPM     sn$ s  snf )Nr    r[   rw   )r   r<   s     r   r+   Yeelight.find_unknown_bulbs   s9    !22^2r6K6KB6OPV6W[]6]2^^^s   55)r   r   r   N)__name__
__module____qualname____firstlineno__r   r   r   r-   r7   r9   r*   rU   rX   r^   r,   boolr1   rT   r\   rs   rp   r   r+   __static_attributes__r{   r   r   r   r      s    2-hw
5
6
D3 .&"-C "-Hd D  
T @ @d @_D _r   r   )__doc__coloramar   r   r   r	   r   typingr   re   r   r{   r   r   <module>r      sO     " #    	
O_ O_  O_r   