34
34
35
35
LIFX_URL = "https://api.lifx.com/v1/lights/"
36
36
37
+ try :
38
+ from typing import Union , Dict , Any
39
+ from adafruit_esp32spi .adafruit_esp32spi_wifimanager import ESPSPI_WiFiManager
40
+ from adafruit_espatcontrol .adafruit_espatcontrol_wifimanager import (
41
+ ESPAT_WiFiManager ,
42
+ )
43
+ from adafruit_requests import Session , Response
44
+
45
+ WifiManagerType = Union [ESPSPI_WiFiManager , ESPAT_WiFiManager , Session ]
46
+ except ImportError :
47
+ pass
48
+
37
49
38
50
class LIFX :
39
- """
40
- HTTP Interface for interacting with the LIFX API
41
- """
51
+ """HTTP Interface for interacting with the LIFX API
42
52
43
- def __init__ (self , wifi_manager , lifx_token ):
44
- """
45
- Creates an instance of the LIFX HTTP API client.
46
- :param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
53
+ :param wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
47
54
or session from adafruit_requests.Session
48
- :param str lifx_token: LIFX API token (https://api.developer.lifx.com/docs/authentication)
49
- """
55
+ :param str lifx_token: LIFX API token (https://api.developer.lifx.com/docs/authentication)
56
+ """
57
+
58
+ def __init__ (self , wifi_manager : WifiManagerType , lifx_token : str ) -> None :
50
59
wifi_type = str (type (wifi_manager ))
51
60
allowed_wifi_types = ("ESPSPI_WiFiManager" , "ESPAT_WiFiManager" , "Session" )
52
61
if any (x in wifi_type for x in allowed_wifi_types ):
@@ -59,7 +68,7 @@ def __init__(self, wifi_manager, lifx_token):
59
68
}
60
69
61
70
@staticmethod
62
- def _parse_resp (response ) :
71
+ def _parse_resp (response : Response ) -> str :
63
72
"""Parses and returns the JSON response returned
64
73
from the LIFX HTTP API.
65
74
"""
@@ -74,75 +83,89 @@ def _parse_resp(response):
74
83
raise KeyError (response .json ()["error" ]) from err
75
84
76
85
# HTTP Requests
77
- def _post (self , path , data ) :
86
+ def _post (self , path : str , data : Dict [ str , Any ]) -> str :
78
87
"""POST data to the LIFX API.
88
+
79
89
:param str path: Formatted LIFX API URL
80
- :param json data: JSON data to POST to the LIFX API.
90
+ :param dict data: JSON data to POST to the LIFX API.
81
91
"""
82
92
response = self ._wifi .post (path , json = data , headers = self ._auth_header )
83
93
response = self ._parse_resp (response )
84
94
return response
85
95
86
- def _put (self , path , data ) :
96
+ def _put (self , path : str , data : Dict [ str , Any ]) -> str :
87
97
"""PUT data to the LIFX API.
98
+
88
99
:param str path: Formatted LIFX API URL
89
- :param json data: JSON data to PUT to the LIFX API.
100
+ :param dict data: JSON data to PUT to the LIFX API.
90
101
"""
91
102
response = self ._wifi .put (path , json = data , headers = self ._auth_header )
92
103
response = self ._parse_resp (response )
93
104
return response
94
105
95
- def _get (self , path , data ) :
106
+ def _get (self , path : str , data : Dict [ str , Any ]) -> Dict [ str , Any ] :
96
107
"""GET data from the LIFX API.
108
+
97
109
:param str path: Formatted LIFX API URL
98
- :param json data: JSON data to GET from the LIFX API.
110
+ :param dict data: JSON data to GET from the LIFX API.
99
111
"""
100
112
response = self ._wifi .get (path , json = data , headers = self ._auth_header )
101
113
return response .json ()
102
114
103
- def toggle_light (self , selector , all_lights = False , duration = 0 ):
115
+ def toggle_light (
116
+ self , selector : str , all_lights : bool = False , duration : float = 0
117
+ ) -> str :
104
118
"""Toggles current state of LIFX light(s).
105
- :param dict selector: Selector to control which lights are requested.
119
+
120
+ :param str selector: Selector to control which lights are requested.
106
121
:param bool all: Toggle all lights at once. Defaults to false.
107
- :param double duration: Time (in seconds) to spend performing a toggle. Defaults to 0.
122
+ :param float duration: Time (in seconds) to spend performing a toggle. Defaults to 0.
108
123
"""
109
124
if all_lights :
110
125
selector = "all"
111
126
data = {"duration" : duration }
112
127
return self ._post (LIFX_URL + selector + "/toggle" , data )
113
128
114
- def move_effect (self , selector , move_direction , period , power_on ):
129
+ def move_effect (
130
+ self , selector : str , move_direction : str , period : float , power_on : bool
131
+ ) -> str :
115
132
"""Performs a linear move effect on a light, or lights.
133
+
134
+ :param str selector: Selector to control which lights are requested.
116
135
:param str move_direction: Move direction, forward or backward.
117
- :param double period: Time in second per effect cycle.
136
+ :param float period: Time in second per effect cycle.
118
137
:param bool power_on: Turn on a light before performing the move.
119
138
"""
120
139
data = {"direction" : move_direction , "period" : period , "power_on" : power_on }
121
140
return self ._post (LIFX_URL + selector + "/effects/move" , data )
122
141
123
- def effects_off (self , selector , power_off = False ):
142
+ def effects_off (self , selector : str , power_off : bool = False ) -> str :
124
143
"""Turns off any running effects on the selected device.
125
- :param dict selector: Selector to control which lights are requested.
144
+
145
+ :param str selector: Selector to control which lights are requested.
126
146
:param bool power_off: If true, the devices will also be turned off.
127
147
"""
128
148
data = {"power_off" , power_off }
129
149
return self ._post (LIFX_URL + selector + "/effects/off" , data )
130
150
131
- def set_brightness (self , selector , brightness ) :
151
+ def set_brightness (self , selector : str , brightness : float ) -> str :
132
152
"""Sets the state of the lights within the selector.
133
- :param dict selector: Selector to control which lights are requested.
134
- :param double brightness: Brightness level of the light, from 0.0 to 1.0.
153
+
154
+ :param str selector: Selector to control which lights are requested.
155
+ :param float brightness: Brightness level of the light, from 0.0 to 1.0.
135
156
"""
136
157
data = {"brightness" : brightness }
137
158
return self ._put (LIFX_URL + selector + "/state" , data )
138
159
139
- def set_color (self , selector , ** kwargs ):
160
+ def set_color (self , selector : str , ** kwargs ) -> str :
140
161
"""Sets the state of the light's color within the selector.
141
- Valid arguments: https://api.developer.lifx.com/docs/set-state
162
+ Valid keyword arguments: https://api.developer.lifx.com/docs/set-state
163
+
164
+ :param str selector: Selector to control which lights are requested.
142
165
"""
143
166
return self ._put (LIFX_URL + selector + "/state" , kwargs )
144
167
145
- def list_lights (self ):
168
+ def list_lights (self ) -> Dict [ str , Any ] :
146
169
"""Enumerates all the lights associated with the LIFX Cloud Account"""
147
170
response = self ._wifi .get (url = LIFX_URL + "all" , headers = self ._auth_header )
148
171
resp = response .json ()
0 commit comments