
    &i!                        S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJrJr  S SKrS SKrS SKJr  S SKJr  S S	KJrJrJr  S S
KJrJrJrJr  SSKJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)  Sr*\RV                  " \,5      r- " S S5      r. " S S5      r/ " S S5      r0 " S S5      r1 " S S\5      r2 " S S\\2   \5      r3 " S S\35      r4 " S S\35      r5 " S S\35      r6\	" S S!9 " S" S#5      5       r7\	" S S!9S$ " 5       5       r8\	" S S!9S% " 5       5       r9\	" S S!9S& " 5       5       r:\	" S S!9S' " 5       5       r;\	" S S!9S( " 5       5       r< " S) S*\=\3   5      r>S+\S,\?S-\?S.\@4S/ jrAS+\S,\?S-\?S.\@4S0 jrBS+\S,\?S-\?S.\@4S1 jrCg)2    N)abstractmethodABC)	Awaitable)	dataclass)	timedelta)AnyCallable)ConfigEntry)
DeviceInfo)CoordinatorEntityDataUpdateCoordinatorUpdateFailed)ACInfinityClientACInfinityClientInvalidAuthACInfinityClientCannotConnectACInfinityClientRequestFailed   )AI_CONTROLLER_TYPESDOMAINMANUFACTURERControllerPropertyKeyControllerTypeDeviceControlKeyDevicePropertyKeySensorPropertyKey
SensorTypeConfigurationKeyEntityConfigValuezRetry limit exceeded contacting the AC Infinity API.  The AC Infinity API can be unstable; Please try your request again later.c                   &   \ rS rSrSrS\\\4   SS4S jr\	S\4S j5       r
\	S\4S j5       r\	S\4S	 j5       r\	S\4S
 j5       r\	S\S   4S j5       r\	S\S   4S j5       r\	S\4S j5       r\	S\\\4   4S j5       r\S\S\4S j5       rSrg)ACInfinityController)   z&
A UIS enabled AC Infinity Controller
controller_jsonreturnNc                    [        U[        R                     5      U l        U[        R                     U l        U[        R                     U l        U[        R                     U l	        [        U R                  4U l        U[        R                     [        R                     =(       d    / nU Vs/ s H  n[        X5      PM     snU l        [!        U R                  1U R                  ["        U[        R$                     U[        R&                     U R)                  U[        R                     5      S9U l        / U l        [        R.                  U[        R                     ;   aO  U[        R                     [        R.                     =(       d    / nU Vs/ s H  n[1        X5      PM     snU l        ggs  snf s  snf )z
Args:
    controller_json: Json of an individual controller. This is typically obtained from
    /api/user/devInfoListAll endpoint, and would be a single object obtained from the array
    in the data field of the json returned.
)identifiersnamemanufacturer
hw_version
sw_versionmodelN)strr   	DEVICE_ID_controller_idMAC_ADDR	_mac_addrDEVICE_NAME_controller_nameDEVICE_TYPE_controller_typer   _identifierDEVICE_INFOPORTSACInfinityDevice_devicesr   r   
HW_VERSION
SW_VERSION6_ACInfinityController__get_device_model_by_device_type_device_info_sensorsSENSORSACInfinitySensor)selfr"   devicesdevicesensorssensors         -/config/custom_components/ac_infinity/core.py__init__ACInfinityController.__init__.   s~    "/2G2Q2Q"RS()>)G)GH /0E0Q0Q R /0E0Q0Q R"D$7$78!"7"C"CDEZE`E`agegELMW6)$7WM&))*&&%&'<'G'GH&'<'G'GH88 5 A AB	
  ((O<Q<]<],^^%&;&G&GHI^IfIfgmkmGJQR'-d;'RDM _ N" Ss   2F9F>c                     U R                   $ )z+The unique identifier of the UIS Controller)r-   r@   s    rE   controller_id"ACInfinityController.controller_idR   s     """    c                     U R                   $ )z8The name of the controller as set in the Android/iOS app)r1   rI   s    rE   controller_name$ACInfinityController.controller_nameW   s     $$$rL   c                 (    U R                   [        ;   $ )z3Returns true if this controller is an AI controller)r3   r   rI   s    rE   is_ai_controller%ACInfinityController.is_ai_controller\   s     $$(;;;rL   c                     U R                   $ )zFThe unique mac address of the UIS controller's WI-FI network interface)r/   rI   s    rE   mac_addrACInfinityController.mac_addra        ~~rL   r7   c                     U R                   $ )zA list of USB-C ports associated with this controller and their associated settings, with or without a UIS child device plugged into it.)r8   rI   s    rE   rA   ACInfinityController.devicesf        }}rL   r?   c                     U R                   $ )zVA list of USB-C sensors associated with this controller and their associated settings.)r=   rI   s    rE   rC   ACInfinityController.sensorsk   rY   rL   c                     U R                   $ )z7A HAAS device definition visible in the device manager.r<   rI   s    rE   device_info ACInfinityController.device_infop           rL   c                     U R                   $ )z@The unique identifier for the HAAS device in the device manager.)r4   rI   s    rE   
identifierACInfinityController.identifieru        rL   device_typec                     U =[         R                  :X  a    g=[         R                  :X  a    g=[         R                  :X  a    g=[         R                  :X  a    g[         R
                  :X  a  g SU  3$ )NzUIS Controller 69 Pro (CTR69P)zUIS Controller 69 Pro+ (CTR69Q)zUIS Controller AI+ (CTR89Q)z"UIS Controller Outlet AI (AC-ADA4)z#UIS Controller Outlet AI+ (AC-ADA8)zUIS Controller Type )r   
UIS_69_PROUIS_69_PRO_PLUSUIS_89_AI_PLUSUIS_OUTLET_AIUIS_OUTLET_AI_PLUS)re   s    rE   !__get_device_model_by_device_type6ACInfinityController.__get_device_model_by_device_typez   sY    ***7///8...4---;22<-k];;rL   )r-   r1   r3   r<   r8   r4   r/   r=   )__name__
__module____qualname____firstlineno____doc__dictr+   r   rF   propertyrJ   rN   boolrQ   rT   listrA   rC   r   r^   tuplerb   staticmethodintr;   __static_attributes__ rL   rE   r    r    )   s6   "S#CH~"S	"SH #s # # % % % <$ < < #   01   01   !Z ! !  E#s(O     <s <s < <rL   r    c                       \ rS rSrSrS\S\\\4   SS4S jr	\
S\S\S	\4S
 j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       rSrg)r?      
A USB-C port associated with this controller and its associated settings,
with or without a UIS child device (fan, light, etc...) plugged into it.

controllersensor_jsonr#   Nc                     Xl         U[        R                     U l        U[        R                     U l        U R                  U R                   U R                  U R
                  5      U l        g)a-  
Args:
    controller: The controller that the USB-C port is attached to
    sensor_json: Json of an individual sensor. This is typically obtained from
    the sensor field of a single controller returned from the /api/user/devInfoListAll endpoint.
    See the ports property on ACInfinityController.
N)_controllerr   ACCESS_PORT_sensor_portSENSOR_TYPE_sensor_type"_ACInfinitySensor__get_device_infor<   )r@   r   r   s      rE   rF   ACInfinitySensor.__init__   s^     &'(9(E(EF'(9(E(EF 22d//1B1B
rL   sensor_portsensor_typec                    [        U5      ==[        R                  :X  a  OA=[        R                  :X  a  O,=[        R                  :X  a  O=[        R
                  :X  a  O  OA    [        [        U R                   SU S341U R                   S3[        U R                  SS9$ ==[        R                  :X  a  O=[        R                  :X  a  O  OA    [        [        U R                   SU S341U R                   S3[        U R                  SS9$ =[        R                  :X  a@    [        [        U R                   SU S	341U R                   S
3[        U R                  SS9$ =[        R                  :X  a@    [        [        U R                   SU S341U R                   S3[        U R                  SS9$ =[        R                   :X  a    U R(                  $ =[        R"                  :X  a     U R(                  $ =[        R$                  :X  a     U R(                  $ =[        R&                  :X  a     U R(                  $    [        [        U R                   SU SU 341U R                   S3[        U R                  SU 3S9$ )N__spc24z Probe Sensorz&UIS Controller Sensor Probe (AC-SPC24)r%   r&   r'   
via_devicer*   _cos3z CO2 + Light Sensorz UIS CO2 + Light Sensor (AC-COS3)_wds3z Water SensorzUIS Water Sensor (AC-WDS3)_sls3z Soil SensorzUIS Soil Sensor (AC-SLS3)_unknownz Unknown SensorzUIS Sensor Type )ry   r   PROBE_TEMPERATURE_FPROBE_TEMPERATURE_CPROBE_HUMIDITY	PROBE_VPDr   r   rJ   rN   r   rb   CO2LIGHTWATERSOILCONTROLLER_TEMPERATURE_FCONTROLLER_TEMPERATURE_CCONTROLLER_HUMIDITYCONTROLLER_VPDr^   )r   r   r   s      rE   __get_device_info"ACInfinitySensor.__get_device_info   s    +'.
..0*00+*++&*&&'
 "J$<$<#=Q{m6!RS! '667}E!-)44B  3"2*"2"22!J$<$<#=Q{m5!QR! '6677JK!-)44<  "!!!J$<$<#=Q{m5!QR! '667}E!-)446  !!J$<$<#=Q{m5!QR! '667|D!-)445  4
33 ,
 "---	 6*55,
 "--- 1*00,
 "--- ,*++,
 "---, !J$<$<#=Q{m8T_S`!ab! '667G!-)44,[M: rL   c                     U R                   $ z)The parent controller for this USB-C portr   rI   s    rE   r   ACInfinitySensor.controller   rd   rL   c                     U R                   $ )z@The index of the USB-C sensor port, as labeled on the controller)r   rI   s    rE   r   ACInfinitySensor.sensor_port   r`   rL   c                     U R                   $ )z5The type of sensor plugged into the USB-C sensor port)r   rI   s    rE   r   ACInfinitySensor.sensor_type   r`   rL   c                     U R                   $ z|A HAAS device definition visible in the device manager. Will be a child to the device associated with the parent controller.r]   rI   s    rE   r^   ACInfinitySensor.device_info   r`   rL   )r   r<   r   r   )rn   ro   rp   rq   rr   r    rs   r+   r   rF   rx   ry   r   rt   r   r   r   r   r^   rz   r{   rL   rE   r?   r?      s    

#7 
d3PS8n 
Y] 
" A(A7:AILA AF  0     !S ! ! !S ! ! !Z ! !rL   r?   c                       \ rS rSrSrS\S\\\4   SS4S jr	\
S\4S j5       r\
S\4S	 j5       r\
S\4S
 j5       r\
S\4S j5       rSrg)r7      r~   r   device_jsonr#   Nc                 "   Xl         U[        R                     U l        U[        R                     U l        [        [        UR                   SU R                   341UR                   SU R                   3[        UR                  SS9U l        g)a0  
Args:
    controller: The controller that the USB-C port is attached to
    device_json: Json of an individual controller. This is typically obtained from
    the ports field of a single controller returned from the /api/user/devInfoListAll endpoint.
    See the ports property on ACInfinityController.
r    zUIS Enabled Devicer   N)r   r   PORT_device_portNAME_device_namer   r   rJ   rN   device_namer   rb   r<   )r@   r   r   s      rE   rF   ACInfinityDevice.__init__  s     &'(9(>(>?'(9(>(>?& Z%=%=$>a@Q@Q?R"STU../q1A1A0BC%!,,&
rL   c                     U R                   $ r   r   rI   s    rE   r   ACInfinityDevice.controller  rd   rL   c                     U R                   $ )z@The index of the USB-C device port, as labeled on the controller)r   rI   s    rE   device_portACInfinityDevice.device_port  r`   rL   c                     U R                   $ )zLThe name of the USB-C device port, as set by the user in the Android/iOS app)r   rI   s    rE   r   ACInfinityDevice.device_name"  r`   rL   c                     U R                   $ r   r]   rI   s    rE   r^   ACInfinityDevice.device_info'  r`   rL   )r   r<   r   r   )rn   ro   rp   rq   rr   r    rs   r+   r   rF   rt   r   ry   r   r   r   r^   rz   r{   rL   rE   r7   r7      s    

.
=A#s(^
	
.  0     !S ! ! !S ! ! !Z ! !rL   r7   c            
          \ rS rSr% Sr\" SS9r0 r\\	\
4   \S'   0 r\\\	\\4   \
4   \S'   0 r\\\	\4   \
4   \S'   0 r\\\	\4   \
4   \S'   0 r\\\	\4   \
4   \S	'   S
\SS4S jrS\\	   4S jrS\	\-  S\	S\4S jr S3S\	\-  S\	4S jjrS\	\-  S\S\S\	S\4
S jr S3S\	\-  S\S\S\	4S jjrS\	\-  S\S\	S\4S jr S3S\	\-  S\S\	4S jjrS\	\-  S\	S\4S jr S3S\	\-  S\	4S jjrS\	\-  S\S\	S\4S jr S3S\	\-  S\S\	4S jjr S\	\-  S\S\	S\4S jr! S3S\	\-  S\S\	4S  jjr"S4S! jr#S\\$   4S" jr%S#\$S\	S$\4S% jr&S#\$S&\\	\4   4S' jr'S(\(S\	S$\4S) jr)S(\(S&\\	\4   4S* jr*S(\(S\	S$\4S+ jr+S(\(S&\\	\4   4S, jr,S\	\-  S\S&\\	\4   4S- jr-S\	\-  S\S.\	S&\\	\4   4S/ jr.S\	\-  S\S&\\	\4   4S0 jr/S4S1 jr0S2r1g)5ACInfinityServicei-  z^Service layer object responsible for initializing and updating values from the AC Infinity API   seconds_controller_properties_sensor_properties_device_properties_device_controls_device_settingsclientr#   Nc                     Xl         g)zR
Args:
    client: The http client to use to make requests to the AC Infinity API
N_client)r@   r   s     rE   rF   ACInfinityService.__init__A  s	     rL   c                 H    [        U R                  R                  5       5      $ )z7
returns a list of devices associated with the account
)rv   r   keysrI   s    rE   get_device_ids ACInfinityService.get_device_idsJ  s     D//44677rL   rJ   property_keyc                     [        U5      nX0R                  ;   a*  U R                  U   nX$;   a  gX$[        R                     ;   $ g)zreturns if a given property exists on a given controller.

Args:
    controller_id: the device id of the controller
    property_key: the json field name for the data being retrieved
TFr+   r   r   r5   )r@   rJ   r   normalized_idresults        rE   get_controller_property_exists0ACInfinityService.get_controller_property_existsP  sK     M*77700?F%*?*K*K#LLLrL   c                     [        U5      nX@R                  ;   aR  U R                  U   nX%;   a  XR   nUb  U$ U$ X%[        R                     ;   a  U[        R                     U   nUb  U$ U$ U$ )a)  gets a property value for a given controller, if both the property and controller exist.

Args:
    controller_id: the device id of the controller
    property_key: the json field name for the data being retrieved
    default_value: the value to return if the controller or property doesn't exist
r   )r@   rJ   r   default_valuer   r   values          rE   get_controller_property)ACInfinityService.get_controller_propertyb  s     M*77700?F%, % 1uD}D(=(I(I!JJ4@@A,O % 1uD}DrL   r   r   c                 h    [        U5      X#4nXPR                  ;   =(       a    X@R                  U   ;   $ )aI  returns if a given sensor property exists on a given controller.

Args:
    controller_id: the device id of the controller
    sensor_port: the sensor port on the AI controller the sensor is plugged into
    sensor_type: the type of sensor plugged into the port
    property_key: the json field name for the data being retrieved
r+   r   )r@   rJ   r   r   r   r   s         rE   get_sensor_property_exists,ACInfinityService.get_sensor_property_existsx  s;     ]+[F444 G 7 7 FF	
rL   c                 |    [        U5      X#4nX`R                  ;   a  U R                  U   nXG;   a  Xt   nUb  U$ U$ U$ )a  gets a property value for a given sensor on a controller, if the property, controller, access port, and sensor all exist.

Args:
    controller_id:  the device id of the controller
    sensor_port: the index of the sensor port on the controller
    sensor_type: the type of sensor
    property_key: the json filed name for the data being retrieved
    default_value: the default value to return if the controller, port, or property doesn't exist
r   )	r@   rJ   r   r   r   r   r   foundr   s	            rE   get_sensor_property%ACInfinityService.get_sensor_property  sS    " ]+[F333++M:E$+ % 1uD}DrL   r   c                 h    [        U5      U4nX@R                  ;   =(       a    X0R                  U   ;   $ )zreturn if a given property key exists on a given device port

Args:
    controller_id: the device id of the controller
    device_port: the index of the port on the controller
    property_key: the setting to pull the value of
r+   r   )r@   rJ   r   r   r   s        rE   get_device_property_exists,ACInfinityService.get_device_property_exists  s;     ]+[9444 G 7 7 FF	
rL   c                 |    [        U5      U4nXPR                  ;   a  U R                  U   nX6;   a  Xc   nUb  U$ U$ U$ )a  gets a property value for a given port on a controller, if the property, controller and port all exist.

Args:
    controller_id:  the device id of the controller
    device_port: the index of the port on the controller
    property_key: the json filed name for the data being retrieved
    default_value: the default value to return if the controller, port, or property doesn't exist
r   )r@   rJ   r   r   r   r   r   r   s           rE   get_device_property%ACInfinityService.get_device_property  sS     ]+[9333++M:E$+ % 1uD}DrL   setting_keyc                 (    U R                  USU5      $ )zreturns if a given setting exists on a given controller.

Args:
    controller_id: the device id of the controller
    setting_key: the json field name for the data being retrieved
r   )get_device_setting_exists)r@   rJ   r   s      rE   get_controller_setting_exists/ACInfinityService.get_controller_setting_exists  s     --mQLLrL   c                 (    U R                  USX#5      $ )a(  gets a property value for a given controller, if both the property and controller exist.

Args:
    controller_id: the device id of the controller
    setting_key: the json field name for the data being retrieved
    default_value: the value to return if the controller or property doesn't exist
r   )get_device_setting)r@   rJ   r   r   s       rE   get_controller_setting(ACInfinityService.get_controller_setting  s     &&}aTTrL   c                 h    [        U5      U4nX@R                  ;   =(       a    X0R                  U   ;   $ )zreturns if a given setting exists on a given controller.

Args:
    controller_id: the device id of the controller
    device_port: the port index of the device.
    setting_key: the json field name for the data being retrieved
r+   r   )r@   rJ   r   r   r   s        rE   r   +ACInfinityService.get_device_setting_exists  s6     ]+[9 5 55m+I^I^_lIm:mmrL   c                 ~    [        U5      nXR4U R                  ;   a   U R                  XR4   nX6;   a  Xc   nUb  U$ U$ U$ )aM  gets a property value for a given device, if both the setting and device exist.

Args:
    controller_id: the device id of the controller
    device_port: the port index of the device
    setting_key: the json field name for the data being retrieved
    default_value: the value to return if the controller or property doesn't exist
r   r@   rJ   r   r   r   r   r   r   s           rE   r   $ACInfinityService.get_device_setting  sV     M*'4+@+@@**M+GHF$+ % 1uD}DrL   c                     [        U5      U4nX@R                  ;   a*  U R                  U   nX5;   a  gX5[        R                     ;   $ g)zreturn if a given setting key exists on a given device port

Args:
    controller_id: the device id of the controller
    device_port: the index of the port on the controller
    setting_key: the setting to pull the value of
TFr+   r   r   DEV_SETTING)r@   rJ   r   r   r   r   s         rE   get_device_control_exists+ACInfinityService.get_device_control_exists  sP     ]+[9111))-8E#(8(D(D"EEErL   c                     [        U5      U4nXPR                  ;   aR  U R                  U   nX6;   a  Xc   nUb  U$ U$ X6[        R                     ;   a  U[        R                     U   nUb  U$ U$ U$ )a<  gets the current set value for a given device setting

Args:
    controller_id: the device id of the controller
    device_port: the index of the port on the controller
    setting_key: the setting to pull the value of
    default_value: the default value to return if the controller, port, or setting doesn't exist
r   r   s           rE   get_device_control$ACInfinityService.get_device_control$  s     ]+[9111**=9F$+ % 1uD}D'7'C'C DD/;;<[I % 1uD}DrL   c                 Z  #    Sn  U R                   R                  5       (       d"  U R                   R                  5       I Sh  vN   U R                   R                  5       I Sh  vN nU GH  nU[        R
                     nX0R                  [        U5      '   U R                   R                  US5      I Sh  vN nU[        R                     U R                  US4'   [        R                  U[        R                     ;   al  U[        R                     [        R                     =(       d    / nU H9  nU[        R                     nU[        R                      n	XpR"                  XHU	4'   M;     U[        R                     [        R$                      H  n
U
[&        R(                     nXR*                  XK4'   U R                   R                  XK5      I Sh  vN nXR,                  XK4'   U R                   R                  XK5      I Sh  vN nU[        R                     U R                  XK4'   M     GM     g GN GN GN Nj N:! [.        [0        [2        R4                  [6        R8                  4 al  nUS:  aH  US-  n[:        R=                  S[        U5      5        [6        R>                  " S5      I Sh  vN     SnAOe[:        RA                  [B        US9  e SnAf[D         a  n[:        RA                  SUS9  e SnAf[F         a  n[:        RA                  SUS9  e SnAff = fGM%  7f)	zHrefreshes the values of properties and settings from the AC infinity APIr   N   r   zBUnable to refresh from data update coordinator. Retry attempt %s/4exc_infozEUnable to refresh from data update coordinator: Authentication failedz@Unable to refresh from data update coordinator: Unexpected error)$r   is_logged_inloginget_account_controllersr   r,   r   r+   get_device_mode_settingsr   r   r   r>   r5   r   r   r   r   r6   r   r   r   r   r   r   aiohttpClientErrorasyncioTimeoutError_LOGGERwarningsleeperrorACINFINITY_API_ERRORr   	Exception)r@   	try_countall_devices_jsoncontroller_properties_jsonrJ   controller_settings_jsonrC   sensor_properties_jsonaccess_port_indexr   device_properties_jsonr   device_controls_jsondevice_settings_jsonexs                  rE   refreshACInfinityService.refresh?  s    	;||0022,,,,...)-)M)M)O#O 2B.$>?T?^?^$_M Ga//M0BC 6:\\5Z5Z[hjk5l/l,@XYiYuYu@vD))=!*<= -448RShStSt8uu"<=R=^=^"_`u`}`}"~  #E  CE6=20FGXGdGd0e-*@ARA^A^*_K h~33]Wb4cd 7> 3MMbMnMn2o  qF  qL  qL  3M.&<=N=S=S&T Qg//0LM 6:\\5Z5Z[h5v/v,Nb--}.JK 6:\\5Z5Z[h5v/v,Nbcscc  OA--}.JK 3M) 3CD K /#O 0m( 0w 0w .-##$$	  q=NIOO$hjmnwjxy!--***MM"6MD. eprs `kmnu s   L+<I H:!I %H=&AI 9I :DI I1I 
I.I 9L+:I =I  I I I .L%5AK8J;9K>L+KL%)K>>L%L  L%%L+c                     U R                   c  / $ U R                   R                  5        Vs/ s H  n[        U5      PM     sn$ s  snf )zZgets device metadata, such as ids, labels, macaddr, etc... that are not expected to change)r   valuesr    )r@   rB   s     rE   get_all_controller_properties/ACInfinityService.get_all_controller_properties  sB    &&.I;?;V;V;];];_`;_$V,;_```s   Ar   	new_valuec                 F   #    U R                  XU05      I Sh  vN   g N7f)zUpdate the value of a setting via the AC Infinity API

Args:
    controller: the controller
    setting_key: the setting to update the value of
    new_value: the new value of the setting to set
N)update_controller_settings)r@   r   r   r(  s       rE   update_controller_setting+ACInfinityService.update_controller_setting  s       --j	:RSSS   !!
key_valuesc                    #    UR                   (       a  [        SU5      eU R                  UR                  SUR                  U5      I Sh  vN   g N7f)zUpdate the values of a set of settings via the AC Infinity API

Args:
    controller: controller to update
    key_values: a list of key/value pairs to update, as a tuple of (setting_key, new_value)
z>AI controllers do not support updating controller settings: %sr   N)rQ   NotImplementedError,_ACInfinityService__update_advanced_settingsrJ   rN   )r@   r   r.  s      rE   r*  ,ACInfinityService.update_controller_settings  sF      &&%&fhrss11*2J2JAzOiOikuvvvs   A	AAArB   c                 F   #    U R                  XU05      I Sh  vN   g N7f)zUpdate the value of a setting via the AC Infinity API

Args:
    device: the device
    setting_key: the setting to update the value of
    new_value: the new value of the setting to set
N)update_device_settingsr@   rB   r   r(  s       rE   update_device_setting'ACInfinityService.update_device_setting         ))&	2JKKKr-  c                 F  #    UR                   R                  (       a:  U R                  UR                   R                  UR                  U5      I Sh  vN   gU R                  UR                   R                  UR                  UR                  U5      I Sh  vN   g NJ N7f)zUpdate the values of a set of settings via the AC Infinity API

Args:
    device: the device
    key_values: a list of key/value pairs to update, as a tuple of (setting_key, new_value)
N)r   rQ   2_ACInfinityService__update_ai_control_and_settingsrJ   r   r1  r   r@   rB   r.  s      rE   r4  (ACInfinityService.update_device_settings  s      --778I8I8W8WY_YkYkmwxxx11&2C2C2Q2QSYSeSegmgygy  |F  G  G  G y Gs%   AB!BAB!BB!B!c                 F   #    U R                  XU05      I Sh  vN   g N7f)zUpdate the value of a setting via the AC Infinity API

Args:
    device: the index of the port on the controller
    setting_key: the setting to update the value of
    new_value: the new value of the setting to set
N)update_device_controlsr5  s       rE   update_device_control'ACInfinityService.update_device_control  r8  r-  c                 0  #    UR                   R                  (       a:  U R                  UR                   R                  UR                  U5      I S h  vN   g U R                  UR                   R                  UR                  U5      I S h  vN   g  N? N7fN)r   rQ   r:  rJ   r   *_ACInfinityService__update_device_controlsr;  s      rE   r>  (ACInfinityService.update_device_controls  sw     
 --778I8I8W8WY_YkYkmwxxx//0A0A0O0OQWQcQceoppp yps$   ABB9BBBBc                 (  #    Sn  U R                   R                  XU5      I Sh  vN   g N! [        [        [        R
                  [        R                  4 al  nUS:  aH  US-  n[        R                  S[        U5      5        [        R                  " S5      I Sh  vN     SnAOe[        R                  [        US9  e SnAf[         a  n[        R                  SUS9  e SnAf[         a  n[        R                  SUS9  e SnAff = fGM  7f)	  Update the values of a set of settings via the AC Infinity API

Args:
    controller_id: the device id of the controller
    device_port: the index of the port on the controller
    key_values: a list of key/value pairs to update, as a tuple of (setting_key, new_value)
r   Nr  r   z4Unable to update device controls. Retry attempt %s/4r  z7Unable to update device controls: Authentication failedz2Unable to update device controls: Unexpected error)r   r>  r   r   r  r  r  r  r  r  r+   r  r  r  r   r  r@   rJ   r   r.  r  r!  s         rE   __update_device_controls*ACInfinityService.__update_device_controls  s      	ll99-V`aaa b .-##$$	  q=NIOO$Z\_`i\jk!--***MM"6MD. Wbde R]_`- b   D. ,. D. .DACB" C%D*CDC%%D2DDDr   c                 (  #    Sn  U R                   R                  XX45      I Sh  vN   g N! [        [        [        R
                  [        R                  4 al  nUS:  aH  US-  n[        R                  S[        U5      5        [        R                  " S5      I Sh  vN     SnAOe[        R                  [        US9  e SnAf[         a  n[        R                  SUS9  e SnAf[         a  n[        R                  SUS9  e SnAff = fGM  7f)	a0  Update the values of a set of settings via the AC Infinity API

Args:
    controller_id: The device id of the controller to update
    device_port: 0 for controller settings, or the port number for port settings
    key_values: a list of key/value pairs to update, as a tuple of (setting_key, new_value)
r   Nr  r   zAUnable to update advanced controller settings. Retry attempt %s/4r  zDUnable to update advanced controller settings: Authentication failedz?Unable to update advanced controller settings: Unexpected error)r   r4  r   r   r  r  r  r  r  r  r+   r  r  r  r   r  )r@   rJ   r   r   r.  r  r!  s          rE   __update_advanced_settings,ACInfinityService.__update_advanced_settings  s      	ll99-Vannn o .-##$$	  q=NIOO$gilmviwx!--***MM"6MD. doqr _jlm+ rJ  c                 (  #    Sn  U R                   R                  XU5      I Sh  vN   g N! [        [        [        R
                  [        R                  4 al  nUS:  aH  US-  n[        R                  S[        U5      5        [        R                  " S5      I Sh  vN     SnAOe[        R                  [        US9  e SnAf[         a  n[        R                  SUS9  e SnAf[         a  n[        R                  SUS9  e SnAff = fGM  7f)	rF  r   Nr  r   zDUnable to update ai device controls and settings. Retry attempt %s/4r  zGUnable to update ai device controls and settings: Authentication failedzBUnable to update ai device controls and settings: Unexpected error)r   %update_ai_device_control_and_settingsr   r   r  r  r  r  r  r  r+   r  r  r  r   r  rG  s         rE    __update_ai_control_and_settings2ACInfinityService.__update_ai_control_and_settings,  s      	llHHeoppp q .-##$$	  q=NIOO$jlopylz{!--***MM"6MD. grtu bmop- rJ  c                 x   #    U R                   (       a#  U R                   R                  5       I Sh  vN   gg N7f)z"Close the client session when doneN)r   closerI   s    rE   rS  ACInfinityService.closeT  s*     <<,,$$&&& &s   /:8:r   rB  )r#   N)2rn   ro   rp   rq   rr   r   MIN_TIME_BETWEEN_UPDATESr   rs   r+   r   __annotations__r   rw   ry   r   r   r   r   rF   rv   r   ru   r   r   r   r   r   r   r   r   r   r   r  r  r"  r    r&  r+  r*  r7   r6  r4  r?  r>  rC  r1  r:  rS  rz   r{   rL   rE   r   r   -  sO   h(3 .0DcN/ ;=U3S=1367< 68U38_c127 46d5c?C/05 46d5c?C/05&	8S	 8 3Y69	& JN 3Y69,
Sy
 
 	

 
 

6 Sy  	
 4
Sy
 
 	

 

0 Sy  	0	M 3Y	M58	M		M IM
U 3Y
U58
Un 3Yn58nGJn	n$ Sy  	0Sy  	
 
6 Sy  	6?Bat4H/I aT(T T 	Tw.w<@cNwL L L 	LG G cNG L L L 	Lq q cNq&Sy& & cN	&P&Sy& & 	&
 cN&P&Sy& & cN	&P'rL   r   c                   Z   ^  \ rS rSrSrS\S\S\4U 4S jjrS r	\
S\4S	 j5       rS
rU =r$ )ACInfinityDataUpdateCoordinatoriZ  z)Handles updating data for the integrationentryservicepolling_intervalc           
      R   > [         TU ]  U[        [        U[	        US9S9  X0l        g)Constructorr   )r&   config_entryupdate_intervalN)superrF   r  r   r   _ac_infinity)r@   hassrY  rZ  r[  	__class__s        rE   rF   (ACInfinityDataUpdateCoordinator.__init__]  s4     	%.>? 	 	
 $rL   c                 d  #    [         R                  S5         [        R                  " S5       ISh  vN   U R                  R                  5       I Sh  vN   U R                  sSSS5      ISh  vN   $  NC N# N	! , ISh  vN  (       d  f       g= f! [         a  n[        UeSnAff = f7f)z#Fetch data from the AC Infinity APIz,Refreshing data from data update coordinator
   N)r  debugasync_timeouttimeoutra  r"  r  r   )r@   es     rE   _async_update_data2ACInfinityDataUpdateCoordinator._async_update_datao  s~     DE	&$,,R00''//111(( 1001 1000  	&A%	&s   B0B A7B A=A9A=%B 1A;2B 6B07B 9A=;B =BBBB B0B 
B-!B((B--B0r#   c                     U R                   $ rB  ra  rI   s    rE   ac_infinity+ACInfinityDataUpdateCoordinator.ac_infinityy  s       rL   rn  )rn   ro   rp   rq   rr   r
   r   ry   rF   rk  rt   ro  rz   __classcell__rc  s   @rE   rX  rX  Z  sJ    3$ $ #	$
 $$& !. ! !rL   rX  c                   "  ^  \ rS rSr% Sr\\S'   \\S'   S\S\S\4U 4S jjrS r	\
S	\4S
 j5       r\
S	\4S j5       r\
\S	\4S j5       5       r\
\S	\4S j5       5       r\S\S	\4S j5       r\
\S	\4S j5       5       r\
S	\4S j5       rSrU =r$ )ACInfinityEntityi~  Tcoordinatortranslation_keyplatformdata_keyc                 <   > [         TU ]  U5        X l        X0l        g rB  )r`  rF   _platform_name	_data_key)r@   ru  rw  rx  rc  s       rE   rF   ACInfinityEntity.__init__  s     	%&!rL   c                 "    SU R                    S3$ )Nz<ACInfinityEntity unique_id=>)	unique_idrI   s    rE   __repr__ACInfinityEntity.__repr__  s    -dnn-=Q??rL   r#   c                     U R                   $ )zFReturns the underlying ac_infinity api data key used to track the data)r{  rI   s    rE   rx  ACInfinityEntity.data_key  rV   rL   c                 .    U R                   R                  $ )zKReturns the underlying ac_infinity api object from the assigned coordinator)ru  ro  rI   s    rE   ro  ACInfinityEntity.ac_infinity       +++rL   c                     g)%Return the unique ID for this entity.Nr{   rI   s    rE   r  ACInfinityEntity.unique_id      rL   c                     g)1Returns the device info for the controller entityNr{   rI   s    rE   r^   ACInfinityEntity.device_info  r  rL   rY  c                     g)z7Returns true if the entity is enabled via options flowdNr{   r@   rY  s     rE   
is_enabledACInfinityEntity.is_enabled  r  rL   c                     g)zKReturns true if the field's backing key exists in the initial data obtainedNr{   rI   s    rE   is_suitableACInfinityEntity.is_suitable  r  rL   c                     U R                   $ rB  )rz  rI   s    rE   platform_nameACInfinityEntity.platform_name  s    """rL   )r{  rz  )rn   ro   rp   rq   _attr_has_entity_namerX  rV  r+   rF   r  rt   rx  r   ro  r   r  r   r^   r
   ru   r  r  r  rz   rq  rr  s   @rE   rt  rt  ~  s    00":"FI"UX"@ #   ,. , , 43 4  4 @Z @  @ F F F F ZT Z  Z #s # #rL   rt  c                      ^  \ rS rSrS\S\S\\\\/\	4   S\\
\/\	4   S\S\4U 4S jjr\S	\4S
 j5       r\S	\4S j5       r\S	\4S j5       rS\S	\	4S jr\S	\	4S j5       rSrU =r$ )ACInfinityControllerEntityi  ru  r   
enabled_fnsuitable_fnrx  rw  c                 J   > [         TU ]  XU5        X l        X0l        X@l        g rB  )r`  rF   r   _enabled_fn_suitable_fn)r@   ru  r   r  r  rx  rw  rc  s          rE   rF   #ACInfinityControllerEntity.__init__  s&     	9%%'rL   r#   c                 Z    [          SU R                  R                   SU R                   3$ )r  r   )r   r   rT   rx  rI   s    rE   r  $ACInfinityControllerEntity.unique_id  s,     4++445Qt}}oFFrL   c                 .    U R                   R                  $ )r  )r   r^   rI   s    rE   r^   &ACInfinityControllerEntity.device_info  r  rL   c                     U R                   $ rB  r   rI   s    rE   r   %ACInfinityControllerEntity.controller  s    rL   rY  c                 b    U R                  U[        U R                  R                  5      S5      $ )Nr   )r  r+   r   rJ   r  s     rE   r  %ACInfinityControllerEntity.is_enabled  s(    s4+;+;+I+I'JLYYrL   c                 8    U R                  X R                  5      $ rB  )r  r   rI   s    rE   r  &ACInfinityControllerEntity.is_suitable  s      77rL   )r   r  r  )rn   ro   rp   rq   rX  r    r	   r
   r+   ru   rt  rF   rt   r  r   r^   r   r  r  rz   rq  rr  s   @rE   r  r    s    (4( )( k34d:;	(
 /1EFLM( ( ( G3 G G ,Z , ,  0    Z Z Z 8T 8 8rL   r  c                      ^  \ rS rSrS\S\S\\\\/\	4   S\\
\/\	4   S\S\4U 4S jjr\S	\4S
 j5       r\S	\4S j5       r\S	\4S j5       rS\S	\	4S jr\S	\	4S j5       rSrU =r$ )ACInfinitySensorEntityi  ru  rD   r  r  rx  rw  c                 J   > [         TU ]  XU5        X l        X0l        X@l        g rB  )r`  rF   _sensorr  r  )r@   ru  rD   r  r  rx  rw  rc  s          rE   rF   ACInfinitySensorEntity.__init__  s%     	9%'rL   r#   c                     [          SU R                  R                  R                   SU R                  R                   SU R
                   3$ )r  r   _sensor_)r   r  r   rT   r   rx  rI   s    rE   r   ACInfinitySensorEntity.unique_id  sF     4<<22;;<HT\\E]E]D^^_`d`m`m_noorL   c                 .    U R                   R                  $ z+Returns the device info for the port entity)r  r^   rI   s    rE   r^   "ACInfinitySensorEntity.device_info       ||'''rL   c                     U R                   $ rB  )r  rI   s    rE   rD   ACInfinitySensorEntity.sensor      ||rL   rY  c                 v    U R                  U[        U R                  R                  R                  5      S5      $ )NrC   )r  r+   r  r   rJ   r  s     rE   r  !ACInfinitySensorEntity.is_enabled  s-    s4<<+B+B+P+P'QS\]]rL   c                 8    U R                  X R                  5      $ rB  )r  rD   rI   s    rE   r  "ACInfinitySensorEntity.is_suitable  s      {{33rL   )r  r  r  )rn   ro   rp   rq   rX  r?   r	   r
   r+   ru   rt  rF   rt   r  r   r^   rD   r  r  rz   rq  rr  s   @rE   r  r    s    (4( !( k34d:;	(
 /1ABDHI( ( ( p3 p p (Z ( ( (  ^ ^ ^ 4T 4 4rL   r  c                     ^  \ rS rSrS\S\S\\\\/\	4   S\\
\/\	4   S\\/\	4   S-  S\S	\4U 4S
 jjr\S\4S j5       r\S\4S j5       r\S\4S j5       rS\S\	4S jr\S\	4S j5       r\S\	4U 4S jj5       rSrU =r$ )ACInfinityDeviceEntityi  ru  rB   r  r  
at_type_fnNrx  rw  c                 V   > [         TU ]  XU5        X l        X0l        X@l        XPl        g rB  )r`  rF   _devicer  r  _at_type_fn)	r@   ru  rB   r  r  r  rx  rw  rc  s	           rE   rF   ACInfinityDeviceEntity.__init__  s+     	9%'%rL   r#   c                     [          SU R                  R                  R                   SU R                  R                   SU R
                   3$ )r  r   _port_)r   r  r   rT   r   rx  rI   s    rE   r   ACInfinityDeviceEntity.unique_id
  sF     4<<22;;<F4<<C[C[B\\]^b^k^k]lmmrL   c                 .    U R                   R                  $ r  )r  r^   rI   s    rE   r^   "ACInfinityDeviceEntity.device_info  r  rL   c                     U R                   $ rB  )r  rI   s    rE   r   "ACInfinityDeviceEntity.device_port  r  rL   rY  c                     U R                  U[        U R                  R                  R                  5      SU R                  R
                   35      $ )Nport_)r  r+   r  r   rJ   r   r  s     rE   r  !ACInfinityDeviceEntity.is_enabled  sB    s4<<+B+B+P+P'QUZ[_[g[g[s[sZtSuvvrL   c                 8    U R                  X R                  5      $ rB  )r  r   rI   s    rE   r  "ACInfinityDeviceEntity.is_suitable  s      '7'788rL   c                   > U R                   R                  U R                  R                  R                  U R
                  R
                  [        R                  5      n[        TU ]$  =(       a    U R                   R                  U R                  R                  R                  U R
                  R
                  [        R                  5      S:H  =(       a&    U R                  SL =(       d    U R                  U5      $ )zaReturns true if the device is online and, if provided, the active mode matches the at_type filterr   N)ro  r  r  r   rJ   r   r   AT_TYPEr`  	availabler   r   ONLINEr  )r@   active_at_typerc  s     rE   r   ACInfinityDeviceEntity.available  s     ))<<LL##11(($$

 w  RT%5%5%I%ILL##11(($$&
 	& R $$,P0@0@0P		RrL   )r  r  r  r  )rn   ro   rp   rq   rX  r7   r	   r
   r+   ru   rt  ry   rF   rt   r  r   r^   r   r  r  r  rz   rq  rr  s   @rE   r  r    s"   &4& !& k34d:;	&
 /1ABDHI& cUD[)D0& & &  n3 n n (Z ( ( -  w w w 9T 9 9 R4 R RrL   r  T)frozenc                   0    \ rS rSr% \\\\/\4   \S'   Sr	g)ACInfinityBaseMixini.  r  r{   N)
rn   ro   rp   rq   r	   r
   r+   ru   rV  rz   r{   rL   rE   r  r  .  s    +sC0$677:rL   r  c                  @   ^^ S 7=m4m " UU4S jST7
n [         U 5      $ )Tc                   V   > \ rS rSr Y r% Sr\\\/\	4   \
S'    \\\/ Y4   \
S'   Srg)!ACInfinityControllerReadOnlyMixini4  8Mixin for retrieving values for controller level sensorsr  get_value_fnr{   N)rn   ro   rp   rq   __type_params__rr   r	   rt  r    ru   rV  rz   .type_paramsr  s   rE   r  r  4  s<     B+-ABDHII?,.BCQFGG>rL   r  r  .generic_baser  r  s    @@rE   9<generic parameters of ACInfinityControllerReadOnlyMixin>r  4  s    () ? ?+> ?rL   c                  F   ^^ S 7=m4m " UU4S jST7
n [         T   U 5      $ )r  c                   D   > \ rS rSr Y r% Sr\\\ Y/\	S   4   \
S'   Srg)"ACInfinityControllerReadWriteMixini>  zFMixin for retrieving and updating values for controller level settingsNset_value_fnr{   )rn   ro   rp   rq   r  rr   r	   rt  r    r   rV  rz   r  s   rE   r  r  >  s)     P,.BAF	RVWXXGrL   r  )r  r  s    @@rE   :<generic parameters of ACInfinityControllerReadWriteMixin>r  >  s     )* H H,Ma,P HrL   c                  @   ^^ S 7=m4m " UU4S jST7
n [         U 5      $ )r  c                   V   > \ rS rSr Y r% Sr\\\/\	4   \
S'    \\\/ Y4   \
S'   Srg)ACInfinitySensorReadOnlyMixiniF  r  r  r  r{   N)rn   ro   rp   rq   r  rr   r	   rt  r?   ru   rV  rz   r  s   rE   r  r  F  s<     B+-=>DEE?,.>?BCC>rL   r  r  r  s    @@rE   5<generic parameters of ACInfinitySensorReadOnlyMixin>r  F  s    $% ? ?': ?rL   c                  @   ^^ S 7=m4m " UU4S jST7
n [         U 5      $ )r  c                   V   > \ rS rSr Y r% Sr\\\/\	4   \
S'    \\\/ Y4   \
S'   Srg)ACInfinityDeviceReadOnlyMixiniO  z9Mixin for retrieving values for port device level sensorsr  r  r{   N)rn   ro   rp   rq   r  rr   r	   rt  r7   ru   rV  rz   r  s   rE   r  r  O  s<     C+-=>DEEL,.>?BCCJrL   r  r  r  s    @@rE   5<generic parameters of ACInfinityDeviceReadOnlyMixin>r  O  s    $% K K': KrL   c                  F   ^^ S 7=m4m " UU4S jST7
n [         T   U 5      $ )r  c                   b   > \ rS rSr Y r% Sr\\\ Y/\	S   4   \
S'    \\/\4   S-  \
S'   Srg)ACInfinityDeviceReadWriteMixiniX  zGMixin for retrieving and updating values for port device level settingsNr  r  r{   )rn   ro   rp   rq   r  rr   r	   rt  r7   r   rV  ry   ru   rz   r  s   rE   r  r  X  sA     Q,.>BIdOSTTG#%,,srL   r  )r  r  s    @@rE   6<generic parameters of ACInfinityDeviceReadWriteMixin>r  X  s     %& t t(Ea(H trL   c                   >   ^  \ rS rSrS\4U 4S jjrS\4S jrSrU =r	$ )ACInfinityEntitiesia  configc                 .   > [         TU ]  5         Xl        g rB  )r`  rF   _config_entry)r@   r  rc  s     rE   rF   ACInfinityEntities.__init__b  s    #rL   entityc                    UR                  U R                  5      (       a  UR                  (       aH  U R                  U5        [        R                  SUR                  UR                  UR                  5        g [        R                  SUR                  UR                  UR                  5        g [        R                  SUR                  UR                  UR                  5        g )Nz0Initializing entity "%s" (%s) for platform "%s".zSIgnoring unsuitable entity "%s" (%s) for platform "%s". (Not applicable for device)zHIgnoring disabled entity "%s" (%s) for platform "%s". (Disabled by user))	r  r  r  appendr  rg  r  rv  r  )r@   r  s     rE   append_if_suitable%ACInfinityEntities.append_if_suitablef  s    T//00!!F#F$$**((	 i$$**((	 MMZ  &&$$	rL   )r  )
rn   ro   rp   rq   r
   rF   rt  r  rz   rq  rr  s   @rE   r  r  a  s!    ${ $)9  rL   r  rY  	device_identity_config_keyr#   c                 j    U R                   [        R                     U   U   [        R                  :g  $ rB  )datar   ENTITIESr   Disable)rY  r  r  s      rE   enabled_fn_sensorr	    s/    ::&//0;<MNRcRkRkkkrL   c                     U R                   [        R                     U   U   nU[        R                  :H  =(       d    U[        R
                  :H  $ rB  )r  r   r  r   AllSensorsAndControlsrY  r  r  settings       rE   enabled_fn_controlr    E    jj)223I>?PQG'+++^w:K:^:^/^^rL   c                     U R                   [        R                     U   U   nU[        R                  :H  =(       d    U[        R
                  :H  $ rB  )r  r   r  r   r  SensorsAndSettingsr  s       rE   enabled_fn_settingr    r  rL   )Dr  jsonloggingabcr   r   collections.abcr   dataclassesr   datetimer   typingr   r	   r  rh  homeassistant.config_entriesr
   homeassistant.helpers.entityr   (homeassistant.helpers.update_coordinatorr   r   r   $custom_components.ac_infinity.clientr   r   r   r   constr   r   r   r   r   r   r   r   r   r   r   r  	getLoggerrn   r  r    r?   r7   r   rX  rt  r  r  r  r  r  r  r  r  r  rv   r  r+   ru   r	  r  r  r{   rL   rE   <module>r!     s      # % !      4 3 A A    Y 


H
%_< _<Dm! m!`/! /!dj' j'Z!!&; !!H.#()HI3 .#b"8!1 "8J"4- "4J2R- 2Rj $; ; ;
 $? ? $H H $? ? $K K $t t./ @l[ lS lS lUY l_k _c _c _VZ _
_k _c _c _VZ _rL   