
    j                     J    d Z ddlZddlmZmZ ddlmZmZmZ  G d de      Zy)a  
 Python module to interface with Tuya WiFi smart devices

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

 Local Control Classes
    BulbDevice(...)
        See OutletDevice() for constructor arguments

 Functions
    BulbDevice Class methods
        rgb_to_hexvalue(r, g, b, hexformat):
        hsv_to_hexvalue(h, s, v, hexformat):
        hexvalue_to_rgb(hexvalue, hexformat=None):
        hexvalue_to_hsv(hexvalue, hexformat=None):

    BulbDevice
        set_mode(self, mode="white", nowait=False):
        set_scene(self, scene, scene_data=None, nowait=False):
        set_timer(self, num_secs, nowait=False):
        set_musicmode(self, transition, modify_settings=True, nowait=False):
        unset_musicmode( self ):
        set_music_colour( self, red, green, blue, brightness=None, colourtemp=None, transition=None, nowait=False ):
        set_colour(r, g, b, nowait):
        set_hsv(h, s, v, nowait):
        set_white_percentage(brightness=100, colourtemp=0, nowait):
        set_brightness_percentage(brightness=100, nowait):
        set_colourtemp_percentage(colourtemp=100, nowait):
        result = get_value(self, feature, state=None, nowait=False):
        result = get_mode(self, state=None, nowait=False):
        result = get_brightness_percentage(self, state=None, nowait=False):
        result = get_colourtemp_percentage(self, state=None, nowait=False):
        (r, g, b) = colour_rgb():
        (h,s,v) = colour_hsv()
        result = state():
        bool = bulb_has_capability( self, feature, nowait=False ):
        detect_bulb(self, response=None, nowait=False):
        set_bulb_type(self, bulb_type=None, mapping=None):
        set_bulb_capabilities(self, mapping):

    Inherited
        Every device function from core.py
    N   )Devicelog)
error_jsonERR_JSONERR_FUNCTIONc                       e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZi Zddddddddddddded<   ddd d!d"ddd#d$d%d&d'ded(<   ddddddddddddded)<   ddddddddddddded*<   ddgZd+d,d+d-Zd.d/d+d-Zd0d1d.d-Zd2d3dd-Zd4d5dd-Zd.d6dd-Zdd7dd-Zdd8dd-Zd9Z  fd:Z!da fd;	Z"e#d<        Z$e#dbd=       Z%e#d>        Z&e#dcd?       Z'e#dbd@       Z(e#dcdA       Z)e#dbdB       Z*dadCZ+dddDZ,dddEZ-dddFZ.dedGZ/dfdHZ0dddIZ1dgdJZ2dadKZ3dadLZ4dhdMZ5didNZ6djdOZ7dadPZ8djdQZ9dadRZ:dfdSZ;dfdTZ<dfdUZ=dfdVZ>dfdWZ?dfdXZ@dfdYZAdfdZZBdfd[ZCdad\ZDdad]ZEdfd^ZFdkd_ZGd` ZH xZIS )l
BulbDevicez3
    Represents a Tuya based Smart Light/Bulb.
    whitecolourscenemusicscene_1scene_2scene_3scene_4mode
brightness
colourtemp
scene_datatimerr   r                  N            rgb8switchr   r   r   r   r   r   r   r   	value_min	value_maxvalue_hexformatA                     
     hsv16BCNone120)r'   r2   r3   221322423524252627dpsc                     d| _         d | _        d | _        d | _        d | _        d| _        d d d d d d d d d dddd| _        d|vs|d   sd |d<   t        t        | &  |i | y )NFr1   r"   version)
bulb_configured	bulb_typehas_brightnesshas_colourtemp
has_colourtried_statusdpsetsuperr
   __init__)selfargskwargs	__class__s      @/DATA/.local/lib/python3.12/site-packages/tinytuya/BulbDevice.pyrN   zBulbDevice.__init__   s    $""!&

  F"&*; $F9j$($9&9    c                     t         t        |   |      }d| _        |r#| j                  sd|v r| j                  ||       |S )NnowaitTrB   )rM   r
   statusrK   rF   detect_bulb)rO   rW   resultrR   s      rS   rX   zBulbDevice.status   sH    z4/v/> 4//evoVF3rT   c                    d}d| cxk  rdk  sn |dz  }d|cxk  rdk  sn |dz  }d|cxk  rdk  sn |dz  }|rt        d|dd	 z        t        j                  | dz  |dz  |dz        }|d
k(  rBd| ||fz  }|dt        |d   dz        t        |d   dz        t        |d   dz        fz  z  }|S |dk(  r7dt        |d   dz        t        |d   dz        t        |d   dz        fz  }|S t        d      )a  
        Convert an RGB value to the hex representation expected by Tuya Bulb.

        While r, g and b are just hexadecimal values of the corresponding
        Red, Green and Blue values, the h, s and v values (which are values
        between 0 and 1) are scaled:
            hexformat="rgb8": 360 (h) and 255 (s and v)
            hexformat="hsv16": 360 (h) and 1000 (s and v)

        Args:
            r(int): Value for the colour red as int from 0-255.
            g(int): Value for the colour green as int from 0-255.
            b(int): Value for the colour blue as int from 0-255.
            hexformat(str): Selects the return format
                "rgb8": rrggbb0hhhssvv
                "hsv16": hhhhssssvvvv
         r        o@z/redz/greenz/bluez@rgb_to_hexvalue: The value for %s needs to be between 0 and 255.r   Nr!   z%02x%02x%02xz%04x%02x%02xh  r    r   r1   %04x%04x%04xr0   z;rgb_to_hexvalue: hexformat must be either "rgb8" or "hsv16")
ValueErrorcolorsys
rgb_to_hsvint)rgb	hexformaterrhsvhexvalues          rS   rgb_to_hexvaluezBulbDevice.rgb_to_hexvalue   sB   & A6MCA8OCA7NC_befgfhbiijj!!!e)QYE	B%Aq	1H#c!fsl*;SQ#=NPSTWXYTZ]`T`Pa)bbbH  '!%SVc\):CA<NPSTWXYTZ]aTaPb(ccH  Z[[rT   c                 n    |dk(  rd}n|dk(  rd}nt        d|z        t        j                  | |||      S )Nr'   r!   r2   r1   z8Unsupported bulb type %r - unable to determine hexvalue.)r`   r
   rk   )rd   re   rf   bulbrg   s        rS   _rgb_to_hexvaluezBulbDevice._rgb_to_hexvalue   sE    3;IS[I WZ^^__))!Q9==rT   c                    d}d| cxk  rdk  sn |dz  }d|cxk  rdk  sn |dz  }d|cxk  rdk  sn |dz  }|rt        d|dd	 z        |d
k(  r<t        j                  | ||      \  }}}t        j	                  |dz  |dz  |dz  |      S |dk(  r.dt        | dz        t        |dz        t        |dz        fz  }|S t        d      )a  
        Convert an HSV value to the hex representation expected by Tuya Bulb.

        Args:
            h(float): colour Hue as float from 0-1
            s(float): colour Saturation as float from 0-1
            v(float): colour Value as float from 0-1
            hexformat(str): Selects the return format
                "rgb8": rrggbb0hhhssvv
                "hsv16": hhhhssssvvvv
        r\   r   g      ?z/Huez/Saturationz/Valuez>hsv_to_hexvalue: The value for %s needs to be between 0 and 1.r   Nr!   r]   r1   r_   r^   r0   z;hsv_to_hexvalue: hexformat must be either "rgb8" or "hsv16")r`   ra   
hsv_to_rgbr
   rk   rc   )	hsvrg   rh   rd   re   rf   rj   s	            rS   hsv_to_hexvaluezBulbDevice.hsv_to_hexvalue   s     A}}6MCA}}= CA}}8OC^adefegahhii ++Aq!4IQ1--q5y!e)QYPY[['!%QWs1t8}c!d(m(TTHOZ[[rT   c                 d   t        |       }|s |dk(  s|dk(  rd}n|dk(  rd}nt        d      |dk(  r>|dk  rt        d      t        | dd	 d
      }t        | d	d d
      }t        | dd d
      }n|dk(  r|dk  rt        d      t        t        | dd d
      dz        }t        t        | dd d
      dz        }t        t        | dd d
      dz        }t	        j
                  |||      }	t        |	d   dz        }t        |	d   dz        }t        |	d	   dz        }nt        d      |||fS )a  
        Converts the hexvalue used by Tuya for colour representation into
        an RGB value.

        Args:
            hexvalue(string): The hex representation generated by BulbDevice.rgb_to_hexvalue()
            hexformat(str or None):
                "rgb8": The hex is in rrggbb0hhhssvv format
                "hsv16": The hex is in hhhhssssvvvv format
                None: Try to auto-detect the format
        r      r!      r1   zPUnable to detect hexvalue format. Value string must have 6, 12 or 14 hex digits.z.RGB value string must have 6 or 14 hex digits.r   r      r   )HSV value string must have 12 hex digits.     v@r        @@r    r   z:hexvalue_to_rgb: hexformat must be None, "rgb8" or "hsv16")lenr`   rc   floatra   rp   )
rj   rg   hexvalue_lenrd   re   rf   rq   rr   rs   rgbs
             rS   hexvalue_to_rgbzBulbDevice.hexvalue_to_rgb  sc    8}q LB$6"	##	 !!stta !QRRHQqM2&AHQqM2&AHQqM2&A'!b  !LMMc(1Q-,u45Ac(1Q-,v56Ac(1R."-67A%%aA.CCFSL!ACFSL!ACFSL!AYZZ1ayrT   c                 R    |dk(  rd}n
|dk(  rd}nd }t         j                  | |      S Nr'   r!   r2   r1   )r
   r   rj   rm   rg   s      rS   _hexvalue_to_rgbzBulbDevice._hexvalue_to_rgbC  4    3;IS[I I))(I>>rT   c                 >   t        |       }|s |dk(  s|dk(  rd}n|dk(  rd}nt        d      |dk(  r|dk  rt        d      |dk  rDt        j                  | d      }t	        j
                  |d   d	z  |d
   d	z  |d   d	z        \  }}}nt        | dd d      dz  }t        | dd d      d	z  }t        | dd d      d	z  }nW|dk(  rG|dk  rt        d      t        | dd d      dz  }t        | dd d      dz  }t        | dd d      dz  }nt        d      |||fS )a  
        Converts the hexvalue used by Tuya for colour representation into
        an HSV value.

        Args:
            hexvalue(string): The hex representation generated by BulbDevice.rgb_to_hexvalue()
            hexformat(str or None):
                "rgb8": The hex is in rrggbb0hhhssvv format
                "hsv16": The hex is in hhhhssssvvvv format
                None: Try to auto-detect the format
        r   rv   r!   rw   r1   zRUnable to detetect hexvalue format. Value string must have 6, 12 or 14 hex digits.z3RGB[HSV] value string must have 6 or 14 hex digits.r   r]   r   r   r   r/   rx   rz   ry   r   r   r{   z:hexvalue_to_hsv: hexformat must be None, "rgb8" or "hsv16")r|   r`   r
   r   ra   rb   rc   )rj   rg   r~   r   rq   rr   rs   s          rS   hexvalue_to_hsvzBulbDevice.hexvalue_to_hsvN  si    8}q LB$6"	##	 !!uvva !VWWb  006B"--c!func!funcRSfW\n]1a 2+e3B,u4B,u4'!b  !LMMHQqM2&.AHQqM2&/AHQrNB'&0AYZZ1ayrT   c                 R    |dk(  rd}n
|dk(  rd}nd }t         j                  | |      S r   )r
   r   r   s      rS   _hexvalue_to_hsvzBulbDevice._hexvalue_to_hsv~  r   rT   c                    i }| j                  |      }|r\d|v rX|d   rS|D ]N  }| j                  |   }|r||d   vs|d   |   ||   k7  r	||   ||<   2|r5t        j                  d|||          P |rt        j                  d|       n\|D ]A  }| j                  |   r||   || j                  |   <   (t        j                  d|||          C t        j                  d|       | j	                  ||      S )NrV   rB   z3Device does not support capability, skipping: %r:%rzOnly sending changed DPs: %rz7No DPs have changed, sending full update to refresh: %r)cached_statusrL   r   debugset_multiple_values)rO   check_valuesrW   
dps_valuesstatekdps          rS   _set_values_checkzBulbDevice._set_values_check  s   
 ""&"1Ue^e!ZZ]BeEl2eR8HLYZO8[%1!_JrNIISUVXdefXgh " II4jA "::a=0<QJtzz!}-IISUVXdefXgh	 "
 IIOQ[\''F'DDrT   c                     |s>| j                   s| j                  |       | j                  d   r| j                  d   nd}| j                  |||      S )zTurn the device on or offrV   r#   r   )rK   rY   rL   
set_status)rO   onr#   rW   s       rS   
turn_onoffzBulbDevice.turn_onoff  sQ    $$   1 .2ZZ-ATZZ)qFr6&99rT   c                 *    | j                  d||      S )zTurn the device onTr#   rW   r   rO   r#   rW   s      rS   turn_onzBulbDevice.turn_on  s    VFDDrT   c                 *    | j                  d||      S )zTurn the device offFr   r   r   s      rS   turn_offzBulbDevice.turn_off  s    fVEErT   c                 x    | j                  d|      st        t        d      S |dd}| j                  ||      S )z
        Set bulb mode

        Args:
            mode(string): white,colour,scene,music
            nowait(bool): True to send without waiting for response.
        r   rV   z#Bulb does not support mode setting.T)r   r#   )bulb_has_capabilityr   r   r   )rO   r   rW   r   s       rS   set_modezBulbDevice.set_mode  sL     '''@l,QRR 

 %%|F%DDrT   c                 (   | j                  d|      st        t        d      S | j                  d   r| j                  d   | j                  d   k(  rMd|cxk  rdk  st	        d       t	        d      | j                  d   | j
                  d	z   t        |      z   i}nbd
t        |      z  }|| j
                  d}|rC| j                  d   du s| j                  d   | j                  d   k(  r|dxx   |z  cc<   n||d<   | j                  ||      S )z
        Set to scene mode

        Args:
            scene(int): Value for the scene as int from 1-4 (Type A bulbs) or 1-N (Type B bulbs).
            nowait(bool): True to send without waiting for response.
        r   rV   z(set_scene: Bulb does not support scenes.r   r   r   r   z;set_scene: The value for scene needs to be between 1 and 4._%02x)r   r   T)	r   r   r   rL   r`   DPS_MODE_SCENEstrrc   r   )rO   r   r   rW   r   s        rS   	set_scenezBulbDevice.set_scene  s$    '''Al,VWW 

<(djj.F$**U[J\.\O!O !^__ $ !^__

6"D$7$7#$=E
$JJ SZ'E++J
 JJ|,4$**\:RVZV`V`ahVi:iw':5'/9J|,%%z&%BBrT   c                     |r| j                  |||      S | j                  d|      st        t        d      S | j                  | j                  d   ||      S )z
        Set the timer

        Args:
            num_secs: data to send to bulb
            dps_id: do not use, kept for compatibility with Device.set_timer()
        rV   r   z'set_timer: Bulb does not support timer.)	set_valuer   r   r   rL   )rO   num_secsdps_idrW   s       rS   	set_timerzBulbDevice.set_timer  s[     >>&(6>BB'''Al,UVV~~djj18F~KKrT   c                    | j                  d|      st        t        d      S d|z  }|| j                  |||| j                  d         z  }|r|dk  rd}t        | j                  d   |z  dz        }|r|dk  rd}t        | j                  d   |z  dz        }| j                  d   d	k(  rd
nd}	||	|z  z  }||	|z  z  }| j                  | j                  d   ||      S )a  
        Set a colour while in music mode

        Args:
            red(float): red value, 0.0 - 255.0
            green(float): green value, 0.0 - 255.0
            blue(float): blue value, 0.0 - 255.0
            brightness(float): optional white light brightness
            colourtemp(float): optional white light colourtemp
            transition(int): optional transition. will use transition provided in set_musicmode() if not provided
        r   rV   z5set_music_colour: Device does not support music mode.z%xr&   r   r%   d   r!   r   z%04x)r   r   r   rk   rL   rc   r   )
rO   
transitionredgreenbluer   r   rW   r   fmts
             rS   set_music_colourzBulbDevice.set_music_colour  s     '''Al,cdd
"$&&UD$**EV:WYY
QJK0:=DE

QJK0:=DE


#45?fV#
""#
""~~djj16&~IIrT   c                     | j                  d|      st        t        d      S | j                  |||| j                  d         | j
                  dd}| j                  ||      S )a@  
        Set colour of an rgb bulb.

        Args:
            r(float): Value for the colour Red from 0.0-255.0.
            g(float): Value for the colour Green from 0.0-255.0.
            b(float): Value for the colour Blue from 0.0-255.0.
            nowait(bool): True to send without waiting for response.
        r   rV   *set_colour: Device does not support color.r&   Tr   r   r#   )r   r   r   rk   rL   DPS_MODE_COLOURr   )rO   rd   re   rf   rW   r   s         rS   
set_colourzBulbDevice.set_colour  sp     ''&'Bl,XYY **1aDJJ?P4QR((
 %%|F%DDrT   c                     | j                  d|      st        t        d      S | j                  |||| j                  d         | j
                  dd}| j                  ||      S )a0  
        Set colour of an rgb bulb using h, s, v.

        Args:
            h(float): colour Hue as float from 0-1
            s(float): colour Saturation as float from 0-1
            v(float): colour Value as float from 0-1
            nowait(bool): True to send without waiting for response.
        r   rV   r   r&   Tr   )r   r   r   rt   rL   r   r   )rO   rq   rr   rs   rW   r   s         rS   set_hsvzBulbDevice.set_hsv-  sp     ''&'Bl,XYY **Aq!TZZ@Q5RT((

 %%|F%DDrT   c                    d}d|cxk  rdk  sn |dz  }d|cxk  rdk  sn |dz  }|rt        d|dd z        t        | j                  d	   |z  dz        }t        | j                  d	   |z  dz        }| j                  |||
      S )a  
        Set white coloured theme of an rgb bulb.

        Args:
            brightness(int): Value for the brightness in percent (0-100)
            colourtemp(int): Value for the colour temperature in percent (0-100)
            nowait(bool): True to send without waiting for response.

        Note: unlike set_colourtemp(), the colour temp will be silently ignored if the bulb does not support it
        r\   r   r   z/Brightnessz/ColourtempzBset_white_percentage: %s percentage needs to be between 0 and 100.r   Nr%   rV   )r`   rc   rL   	set_white)rO   r   r   rW   rh   rf   cs          rS   set_white_percentagezBulbDevice.set_white_percentageA  s     J%#%= CJ%#%= Cbehijikellmm

;'*4;<

;'*4;<~~q!F~44rT   c                 F   | j                  d|      st        t        d      S t        |      }|dk  r| j                  d   }n<|| j                  d   kD  r*t        d| j                  d   | j                  d   fz        |?t        |      }|dk  rd}|| j                  d   kD  rt        d| j                  d   z        i }|| j                  d   k\  r||d<   |||d	<   |r| j                  |d
<   t        || j                  d   k\        |d<   | j                  ||      S )a  
        DEPRECATED Set white coloured theme of an rgb bulb.

        Args:
            brightness(int): Value for the brightness (A:25-255 or B:10-1000)
            colourtemp(int): Value for the colour temperature (A:0-255, B:0-1000).
            nowait(bool): True to send without waiting for response.

            Default: Max Brightness and Min Colourtemp

        Note: unlike set_colourtemp(), the colour temp will be silently ignored if the bulb does not support it
        r   rV   z.set_white: Device does not support brightness.r   r%   z8set_white: The brightness needs to be between %d and %d.r$   z?set_white: The colour temperature needs to be between 0 and %d.r   r   r#   )	r   r   r   rc   rL   r`   DPS_MODE_WHITEboolr   )rO   r   r   rW   r   s        rS   r   zBulbDevice.set_whiteZ  sa    ''f'Fl,\]] _
>K0J$**[11W[_[e[efq[rtxt~t~  @K  uL  [M  M  N  N !ZJA~
DJJ{33 !beieoeop{e|!|}} K00)3L&!)3L&#'#6#6L !%jDJJ{4K&K!LX %%|F%DDrT   c                     d|cxk  rdk  st        d       t        d      t        | j                  d   |z  dz        }| j                  ||      S )z
        Set the brightness value of an rgb bulb.

        Args:
            brightness(int): Value for the brightness in percent (0-100)
            nowait(bool): True to send without waiting for response.
        r   r   zHset_brightness_percentage: The brightness needs to be between 0 and 100.r%   rV   )r`   rc   rL   set_brightness)rO   r   rW   rf   s       rS   set_brightness_percentagez$BulbDevice.set_brightness_percentage  sa     J%#%ghh &ghh

;'*4;<""1V"44rT   c                 F   | j                  d|      st        t        d      S |dk  r| j                  d   }na|| j                  d   k  r| j	                  d|      S || j                  d   kD  r*t        d| j                  d   | j                  d   fz        | j                  |      }d|v sd	|vr|S |d	   | j                  k7  r| j                  |d
|      S |t        | j                  d         z  }| j                  ||      \  }}}| j                  ||||      S )z
        DEPRECATED Set the brightness value of an rgb bulb.

        Args:
            brightness(int): Value for the brightness (25-255).
            nowait(bool): True to send without waiting for response.
        r   rV   z3set_brightness: Device does not support brightness.r   r%   r$   z=set_brightness: The brightness needs to be between %d and %d.Errorr   N)r   r   rW   r   rW   )r   r   r   rL   r   r`   r   r   r   r}   
colour_hsvr   )rO   r   rW   r   valuerq   rr   rs   s           rS   r   zBulbDevice.set_brightness  sU    ''f'Fl,abb >K0J$**[11==6=22$**[11\`d`j`jkv`wy}  zD  zD  EP  zQ  `R  R  S  S 

&
)u&"5L=D000>>ZDQW>XX tzz+'>!??EeFCIQ1<<1eF<;;rT   c                     d|cxk  rdk  st        d       t        d      t        | j                  d   |z  dz        }| j                  ||      S )z
        Set the colour temperature of an rgb bulb.

        Args:
            colourtemp(int): Value for the colour temperature in percentage (0-100).
            nowait(bool): True to send without waiting for response.
        r   r   zOset_colourtemp_percentage: Colourtemp percentage needs to be between 0 and 100.r%   rV   )r`   rc   rL   set_colourtemp)rO   r   rW   r   s       rS   set_colourtemp_percentagez$BulbDevice.set_colourtemp_percentage  sa     J%#%opp &opp

;'*4;<""Af"66rT   c                    | j                  | j                  |      st        t        d      S d|cxk  r| j                  d   k  sn t        d| j                  d   z        || j                  dd}| j                  ||      S )z
        DEPRECATED Set the colour temperature of an rgb bulb.

        Args:
            colourtemp(int): Value for the colour temperature (0-255).
            nowait(bool): True to send without waiting for response.
        rV   z3set_colourtemp: Device does not support colourtemp.r   r%   zDset_colourtemp: The colour temperature needs to be between 0 and %d.T)r   r   r#   )r   BULB_FEATURE_COLOURTEMPr   r   rL   r`   r   r   )rO   r   rW   r   s       rS   r   zBulbDevice.set_colourtemp  s     '')E)Ef'Vl,abbJ9$**["99cfjfpfpq|f}}~~ %''
 %%|F%DDrT   c                 v    |s| j                  |      }d|v rt        d      ||vrt        d|z        ||   S )NrV   r   z#Error getting device current state.zUnknown parameter %r.)r   RuntimeErrorr`   )rO   featurer   rW   s       rS   	get_valuezBulbDevice.get_value  sL    JJfJ-EeDEE%4w>??W~rT   c                 *    | j                  d||      S )zReturn current working moder   r   r   rO   r   rW   s      rS   get_modezBulbDevice.get_mode  s    ~~fE&~AArT   c                 v    |s| j                  |      }| j                  ||      | j                  ||      fS )NrV   r   )r   brightness_percentagecolourtemp_percentager   s      rS   white_percentagezBulbDevice.white_percentage  sB    JJfJ-E**v*FHbHbinw}HbH~rT   c                     | j                   d   r| j                   d   dk  rt        d      | j                  ||      | j                   d   z  dz  S Nr%   r   zEBulb capabilitiy 'value_max' not set, unable to calculate percentage.r   g      Y@)rL   r   r   r   s      rS   get_brightness_percentagez$BulbDevice.get_brightness_percentage  Q    

;'TZZ-Dq-HfggU6:TZZ=TTW\\\rT   c                 *    | j                  d||      S )zReturn brightness valuer   r   r   r   s      rS   r   zBulbDevice.brightness      ~~l%~GGrT   c                     | j                   d   r| j                   d   dk  rt        d      | j                  ||      | j                   d   z  dz  S r   )rL   r   r   r   s      rS   get_colourtemp_percentagez$BulbDevice.get_colourtemp_percentage  r   rT   c                 *    | j                  d||      S )zReturn colour temperaturer   r   r   r   s      rS   r   zBulbDevice.colourtemp   r   rT   c                     | j                  d||      }t        |t              r|S t        j	                  || j
                  d         S )zReturn colour as RGB valuer   r   r&   )r   
isinstancedictr
   r   rL   rO   r   rW   rj   s       rS   
colour_rgbzBulbDevice.colour_rgb  B    >>(%>Gx'O))(DJJ?P4QRRrT   c                     | j                  d||      }t        |t              r|S t        j	                  || j
                  d         S )zReturn colour as HSV valuer   r   r&   )r   r   r   r
   r   rL   r   s       rS   r   zBulbDevice.colour_hsv  r   rT   c                    | j                   s)| j                  |       | j                   st        d      | j                  |      }i }|st	        t
        d      S d|v rt	        t
        |d         S d|vrt	        t
        d      S | j                  D ]3  }| j                  |   }d|v rd||<   ||d   v r|d   |   ||<   /d||<   5 d	|v r|d	   |d
<   |S )zReturn state of BulbrV   z5Bulb not configured, cannot get device current state.zstate: empty responser   rB   zstate: no data pointsr   Nr#   is_on)rF   rY   r   r   r   r   rL   )rO   rW   rX   r   keyr   s         rS   r   zBulbDevice.state  s    ##F+''"#Z[[##6#2h(?@@fhw88h(?@@::CCBcz!c
ve}$#E]2.c
!c
  u"8_E'N rT   c                     | j                   s)| j                  |       | j                   st        d      t        | j                  |         S )NrV   z4Bulb not configured, cannot get device capabilities.)rF   rY   r   r   rL   )rO   r   rW   s      rS   r   zBulbDevice.bulb_has_capability4  sE    ##V-''"#YZZTZZ(**rT   c           	      h   |sB| j                  d|      }|rd|vr)|rt        j                  d       y| j                         }y|rId|v rDt	        |d   t
              r0d|d   v rd|d   v rd| _        d| _        nQd|d   v rd	|d   v rd| _        d
| _        n4d|d   v r-d|d   v r&d| _        t	        |d   d   t              rdnd| _        | j                  r=| j                  | j                  v r$| j                  | j                     d   | j                  d<   | j                  | j                     d   | j                  d<   | j                  | j                     d   | j                  d<   | j                  | j                     d   | j                  d<   | j                  | j                     D ]d  }|dd dk(  r| j                  | j                     |   s)t        | j                  | j                     |         }||d   v sV|| j                  |<   f dD ](  }t        | d|z   t        | j                  |                * t        j                  d| j                  | j                  d   | j                  d   | j                  d          y| j                  st        j                  d       yy)al  
        Attempt to determine BulbDevice Type A, B or C based on:
            Type A has keys 1-9
            Type B has keys 20-28
            Type C is basic (non-CCT) and only has 1-2 (i.e Feit type bulbs from Costco)

        Example status data:
          Sylvania BR30 [v3.3, RGB+CCT]:
            {'20': True, '21': 'colour', '22': 750, '23': 278, '24': '00f003e803e8', '25': '000e0d0000000000000000c803e8', '26': 0}

          Geeni BW229 Smart Filament Bulb [v3.3, CCT only]:
            {'1': True, '2': 25, '3': 0}
            1: switch, 2: brightness, 3: colour temperature

          No-name RGB+CCT (LED BULB W5K) [v3.5, RGB+CCT]:
            {'20': True, '21': 'white', '22': 10, '23': 0, '24': '000003e803e8', '25': '000e0d0000000000000000c80000', '26': 0, '34': False}

          Feit soft white Filament [v3.5, 2700K only]:
            {'20': True, '21': 'white', '22': 60, '25': '000e0d0000000000000000c803e8', '26': 0, '34': False, '41': True}
            (No CCT (23) or colour (24), but does support scenes (25) and music mode (28))

          Feit dimmer switch [v3.3, not a bulb]:
            {'1': True, '2': 10, '3': 10, '4': 'incandescent'}
            Note: after a power cycle, only DP 2 is returned!  The rest are not returned until after they are set
            1: switch, 2: brightness, 3: minimum dim %, 4: installed bulb type (LED/incandescent)
        T)historicrW   rB   zONo cached status, but nowait set! detect_bulb() exiting without detecting bulb!Nr6   r5   r4   r8   r2   r7   r'   r3   r   r$   r%   r&   r   value_)r   r   r   has_zKBulb type set to %r. has brightness: %r, has colourtemp: %r, has colour: %rr   r   r   z,No DPs in response, cannot detect bulb type!)r   r   r   rX   r   r   rF   rG   r   DEFAULT_DPSETrL   setattrr   )rO   responserW   r   r   s        rS   rY   zBulbDevice.detect_bulb;  s   6 ))4)GH%x"7IIop   ${{}H)j%$.O x&3(5/+A'+$!'%(TXe_-D'+$!$'C8E?,B'+$ )38E?33G(MSV~~$..D4F4F"F&*&8&8&H&Q

7#*.*<*<T^^*L[*Y

;'*.*<*<T^^*L[*Y

;'040B0B4>>0RSd0e

,-++DNN;A!u( --dnn=a@ d00@CEBXe_,(*

1 < <vaxdjjm)<> < IIcnndjj&>

<@XZ^ZdZdemZn %%IIDE &rT   c                 4    || _         | j                  |       y N)rG   set_bulb_capabilities)rO   rG   mappings      rS   set_bulb_typezBulbDevice.set_bulb_type  s    """7+rT   c                    | j                   | j                  v r| j                  | j                      }ni }t        |t              si }| j                  D ]_  }||v r||   | j                  |<   | j                  |   *|j                  |d       }|r|d d dk7  rt        |      n|| j                  |<   a | j                  d   r| j                  d   rd| _        y y y )Nr   r   r#   r   T)rG   r   r   r   rL   getr   rF   )rO   r   default_dpsetr   r   s        rS   r   z BulbDevice.set_bulb_capabilities  s    >>T/// ..t~~>MM7D*GAG| '


1A&"&&q$/,.1Ra5H3DB2

1  ::hDJJ|$<#'D  %=rT   )F)r'   r   )r   F)r   F)NF)NNF)r   r   F)rD   rD   F)r   F)NN)J__name__
__module____qualname____doc__r   r   r   DPS_MODE_MUSICDPS_MODE_SCENE_1DPS_MODE_SCENE_2DPS_MODE_SCENE_3DPS_MODE_SCENE_4BULB_FEATURE_MODEBULB_FEATURE_BRIGHTNESSr   BULB_FEATURE_COLOURBULB_FEATURE_SCENEBULB_FEATURE_SCENE_DATABULB_FEATURE_TIMERBULB_FEATURE_MUSICMUSIC_TRANSITION_JUMPMUSIC_TRANSITION_FADEr   DPS_INDEX_SETSDPS_INDEX_ONDPS_INDEX_MODEDPS_INDEX_BRIGHTNESSDPS_INDEX_COLOURTEMPDPS_INDEX_COLOURDPS_INDEX_SCENEDPS_INDEX_TIMERDPS_INDEX_MUSICDPSrN   rX   staticmethodrk   rn   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   r   r   __classcell__)rR   s   @rS   r
   r
   6   s    NONN    **" *  M!M# "M# !M# !M&" !WN4c2LT4N!$4c:!$4d; t$7d6O t$7O t$7O
C:8 ) )X 	> 	> \ \B * *Z ? ? , ,^ ? ?E6:EFE$CBLJ@E*E(52-E^5 <D7E,B@]
H]
HSS D+OFb,(rT   r
   )	r  ra   corer   r   r   r   r   r
    rT   rS   <module>r     s'   +Z   4 4k( k(rT   