
    j@/                     *    d Z ddl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
    CoverDevice(...)
        See OutletDevice() for constructor arguments

 Functions
    CoverDevice:
        open_cover(switch=None, nowait=False)       # Open the cover (switch defaults to DPS_INDEX_MOVE)
        close_cover(switch=None, nowait=False)      # Close the cover (switch defaults to DPS_INDEX_MOVE)
        stop_cover(switch=None, nowait=False)       # Stop the cover motion (switch defaults to DPS_INDEX_MOVE)
        continue_cover(switch=None, nowait=False)   # Continue cover motion (if supported)
        set_cover_type(cover_type)                  # Manually set cover type (1-8)

 Notes
    CoverDevice automatically detects the device type (1-8) based on status response:
    
    Type 1: ["open", "close", "stop", "continue"] - Most curtains, blinds, roller shades (DEFAULT)
    Type 2: [true, false]                         - Simple relays, garage doors, locks  
    Type 3: ["0", "1", "2"]                       - String-numeric position/state
    Type 4: ["00", "01", "02", "03"]              - Zero-prefixed numeric position/state
    Type 5: ["fopen", "fclose"]                   - Directional binary (no stop)
    Type 6: ["on", "off", "stop"]                 - Switch-lexicon open/close
    Type 7: ["up", "down", "stop"]                - Vertical-motion (lifts, hoists)
    Type 8: ["ZZ", "FZ", "STOP"]                  - Vendor-specific (Abalon-style, older standard)
    
    Credit for discovery: @make-all in https://github.com/jasonacox/tinytuya/issues/653
    Detection occurs on first command by checking device status. Uses priority ordering
    to handle overlapping values (Type 1 has highest priority). Defaults to Type 1 if
    detection fails. You can manually override using set_cover_type(type_id) if needed.
    
    Common DPS IDs:
    - DPS 1: Most common for cover control
    - DPS 101: Second most common (often backlight or secondary function)
    - DPS 4: Commonly used for second curtain in dual-curtain devices
      (DPS 2 and 3 typically for position write/read, DPS 5 and 6 for second curtain,
       with configuration and timers starting from DPS 7 onward)

    Inherited
        json = status()                    # returns json payload
        set_version(version)               # 3.1 [default] or 3.3
        set_socketPersistent(False/True)   # False [default] or True
        set_socketNODELAY(False/True)      # False or True [default]
        set_socketRetryLimit(integer)      # retry count limit [default 5]
        set_socketTimeout(timeout)         # set connection timeout in seconds [default 5]
        set_dpsUsed(dps_to_request)        # add data points (DPS) to request
        add_dps_to_request(index)          # add data point (DPS) index set to None
        set_retry(retry=True)              # retry if response payload is truncated
        set_status(on, switch=1, nowait)   # Set status of switch to 'on' or 'off' (bool)
        set_value(index, value, nowait)    # Set int value of any index.
        heartbeat(nowait)                  # Send heartbeat to device
        updatedps(index=[1], nowait)       # Send updatedps command to device
        turn_on(switch=1, nowait)          # Turn on device / switch #
        turn_off(switch=1, nowait)         # Turn off
        set_timer(num_secs, nowait)        # Set timer for num_secs
        set_debug(toggle, color)           # Activate verbose debugging output
        set_sendWait(num_secs)             # Time to wait after sending commands before pulling response
        detect_available_dps()             # Return list of DPS available from device
        generate_payload(command, data)    # Generate TuyaMessage payload for command with data
        send(payload)                      # Send payload to device (do not wait for response)
        receive()
   )Devicec                       e Zd ZdZdZdZdZdddZdd	d
dg ddddddddgdddddg ddddddg ddddddddgdddd
dg ddddd
dg d dd!d"d#dg d$dd%Z fd&Z	d.d'Z
d( Zd.d)Zd/d*Zd/d+Zd/d,Zd/d-Z xZS )0CoverDevicez{
    Represents a Tuya based Smart Window Cover.
    
    Supports 8 different command types with automatic detection.
    1101r   movement	backlight)r   r   openclosestopcontinue)r
   r   r   r   )r
   r   r   r   detect_valuesTFN20)r   r   r   01020003)r   r   r   r   fopenfcloseonoff)r   r   r   updown)r   r   r   ZZFZSTOP)r   r   r   )r                        c                 H    t        t        | 
  |i | d| _        d | _        y NF)superr   __init___cover_type_detected_cover_type)selfargskwargs	__class__s      A/DATA/.local/lib/python3.12/site-packages/tinytuya/CoverDevice.pyr(   zCoverDevice.__init__   s'    k4)4:6:$)!    c                 Z   | j                   ry|| j                  }| j                  | _        	 | j	                         }|rOd|v rKt        |      }|d   j                  |      }|*g d}|D ]!  }| j                  |   }||d   v s|| _         n d| _         y# t        $ r
 Y d| _         yw xY w)ao  
        Automatically detect the cover device type (1-8) by checking device status.
        Uses priority ordering to handle overlapping values (e.g., 'stop' appears in Types 1, 6, 7).
        Type 1 has highest priority as it's the most comprehensive.
        
        Args:
            switch (str/int): The DPS index to check. Defaults to DPS_INDEX_MOVE.
        Ndps)r   r$   r   r    r!   r#   r   r"   r   T)	r)   DPS_INDEX_MOVEDEFAULT_COVER_TYPEr*   statusstrgetCOVER_TYPES	Exception)r+   switchresultdps_key	dps_valuepriority_ordertype_id	type_infos           r/   _detect_cover_typezCoverDevice._detect_cover_type   s     $$>((F  22	[[]F%6/f+"5M--g6	 (%=N#1$($4$4W$=	$	/(BB/6D,!	 $2 %)!	  	$(!		s   AB 
B 	B*)B*c                 Z    || j                   vrt        d| d      || _        d| _        y)a  
        Manually set the cover device type.
        
        Args:
            cover_type (int): Cover type ID (1-8).
        
        Raises:
            ValueError: If cover_type is not between 1 and 8.
        
        Example:
            cover.set_cover_type(1)  # Set to Type 1 (open/close/stop/continue)
            cover.set_cover_type(6)  # Set to Type 6 (on/off/stop)
        zInvalid cover_type: z. Must be between 1 and 8.TN)r8   
ValueErrorr*   r)   )r+   
cover_types     r/   set_cover_typezCoverDevice.set_cover_type   s8     T---3J<?YZ[[%$(!r0   c                     | j                   s| j                  |       | j                  r@| j                  | j                  v r(| j                  | j                     j	                  |      S y)ap  
        Get the appropriate command for the detected cover type.
        
        Args:
            action (str): The action to perform ('open', 'close', 'stop', 'continue').
            switch (str/int): The DPS index. Defaults to DPS_INDEX_MOVE.
        
        Returns:
            The command value for the detected cover type, or None if not supported.
        N)r)   rA   r*   r8   r7   )r+   actionr:   s      r/   _get_commandzCoverDevice._get_command   s\     ((##F+ 0 0D4D4D D##D$4$4599&AAr0   c                 r    || j                   }| j                  d|      }|| j                  |||       yy)z
        Open the cover.
        
        Args:
            switch (str/int): The DPS index. Defaults to DPS_INDEX_MOVE.
            nowait (bool): Don't wait for device response.
        Nr
   nowaitr3   rH   	set_valuer+   r:   rK   commands       r/   
open_coverzCoverDevice.open_cover   sC     >((F##FF3NN676N: r0   c                 r    || j                   }| j                  d|      }|| j                  |||       yy)z
        Close the cover.
        
        Args:
            switch (str/int): The DPS index. Defaults to DPS_INDEX_MOVE.
            nowait (bool): Don't wait for device response.
        Nr   rJ   rL   rN   s       r/   close_coverzCoverDevice.close_cover   sC     >((F##GV4NN676N: r0   c                 r    || j                   }| j                  d|      }|| j                  |||       yy)a2  
        Stop the cover motion.
        
        Args:
            switch (str/int): The DPS index. Defaults to DPS_INDEX_MOVE.
            nowait (bool): Don't wait for device response.
        
        Note:
            Not all cover types support stop. Types 2 and 5 do not have a stop command.
        Nr   rJ   rL   rN   s       r/   
stop_coverzCoverDevice.stop_cover
  sC     >((F##FF3NN676N: r0   c                 r    || j                   }| j                  d|      }|| j                  |||       yy)a.  
        Continue the cover motion (if supported).
        
        Args:
            switch (str/int): The DPS index. Defaults to DPS_INDEX_MOVE.
            nowait (bool): Don't wait for device response.
        
        Note:
            Only Type 1 and Type 4 support the continue command.
        Nr   rJ   rL   rN   s       r/   continue_coverzCoverDevice.continue_cover  sC     >((F##J7NN676N: r0   )Nr&   )__name__
__module____qualname____doc__r3   DPS_INDEX_BLr4   DPS_2_STATEr8   r(   rA   rE   rH   rP   rR   rT   rV   __classcell__)r.   s   @r/   r   r   H   s    NL K "B
 "E]
 ,
 5
 %x0
 2
 3
 1
e9Kv 
*)X)(&;;;$;r0   r   N)rZ   corer   r    r0   r/   <module>r`      s   AF d;& d;r0   