Skip to content

Commit aaf5a7e

Browse files
authored
Merge pull request #14 from tekktrik/main
Add type annotations, fix documentations
2 parents 063b685 + c72d683 commit aaf5a7e

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

adafruit_lifx.py

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,28 @@
3434

3535
LIFX_URL = "https://api.lifx.com/v1/lights/"
3636

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+
3749

3850
class LIFX:
39-
"""
40-
HTTP Interface for interacting with the LIFX API
41-
"""
51+
"""HTTP Interface for interacting with the LIFX API
4252
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
4754
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:
5059
wifi_type = str(type(wifi_manager))
5160
allowed_wifi_types = ("ESPSPI_WiFiManager", "ESPAT_WiFiManager", "Session")
5261
if any(x in wifi_type for x in allowed_wifi_types):
@@ -59,7 +68,7 @@ def __init__(self, wifi_manager, lifx_token):
5968
}
6069

6170
@staticmethod
62-
def _parse_resp(response):
71+
def _parse_resp(response: Response) -> str:
6372
"""Parses and returns the JSON response returned
6473
from the LIFX HTTP API.
6574
"""
@@ -74,75 +83,89 @@ def _parse_resp(response):
7483
raise KeyError(response.json()["error"]) from err
7584

7685
# HTTP Requests
77-
def _post(self, path, data):
86+
def _post(self, path: str, data: Dict[str, Any]) -> str:
7887
"""POST data to the LIFX API.
88+
7989
: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.
8191
"""
8292
response = self._wifi.post(path, json=data, headers=self._auth_header)
8393
response = self._parse_resp(response)
8494
return response
8595

86-
def _put(self, path, data):
96+
def _put(self, path: str, data: Dict[str, Any]) -> str:
8797
"""PUT data to the LIFX API.
98+
8899
: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.
90101
"""
91102
response = self._wifi.put(path, json=data, headers=self._auth_header)
92103
response = self._parse_resp(response)
93104
return response
94105

95-
def _get(self, path, data):
106+
def _get(self, path: str, data: Dict[str, Any]) -> Dict[str, Any]:
96107
"""GET data from the LIFX API.
108+
97109
: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.
99111
"""
100112
response = self._wifi.get(path, json=data, headers=self._auth_header)
101113
return response.json()
102114

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:
104118
"""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.
106121
: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.
108123
"""
109124
if all_lights:
110125
selector = "all"
111126
data = {"duration": duration}
112127
return self._post(LIFX_URL + selector + "/toggle", data)
113128

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:
115132
"""Performs a linear move effect on a light, or lights.
133+
134+
:param str selector: Selector to control which lights are requested.
116135
: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.
118137
:param bool power_on: Turn on a light before performing the move.
119138
"""
120139
data = {"direction": move_direction, "period": period, "power_on": power_on}
121140
return self._post(LIFX_URL + selector + "/effects/move", data)
122141

123-
def effects_off(self, selector, power_off=False):
142+
def effects_off(self, selector: str, power_off: bool = False) -> str:
124143
"""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.
126146
:param bool power_off: If true, the devices will also be turned off.
127147
"""
128148
data = {"power_off", power_off}
129149
return self._post(LIFX_URL + selector + "/effects/off", data)
130150

131-
def set_brightness(self, selector, brightness):
151+
def set_brightness(self, selector: str, brightness: float) -> str:
132152
"""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.
135156
"""
136157
data = {"brightness": brightness}
137158
return self._put(LIFX_URL + selector + "/state", data)
138159

139-
def set_color(self, selector, **kwargs):
160+
def set_color(self, selector: str, **kwargs) -> str:
140161
"""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.
142165
"""
143166
return self._put(LIFX_URL + selector + "/state", kwargs)
144167

145-
def list_lights(self):
168+
def list_lights(self) -> Dict[str, Any]:
146169
"""Enumerates all the lights associated with the LIFX Cloud Account"""
147170
response = self._wifi.get(url=LIFX_URL + "all", headers=self._auth_header)
148171
resp = response.json()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
Adafruit-Blinka
66
Adafruit_CircuitPython_ESP32SPI
7+
adafruit-circuitpython-esp-atcontrol
78
adafruit_circuitpython_requests

setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
# Author details
3434
author="Adafruit Industries",
3535
author_email="[email protected]",
36-
install_requires=["Adafruit-Blinka", "Adafruit_CircuitPython_ESP32SPI"],
36+
install_requires=[
37+
"Adafruit-Blinka",
38+
"Adafruit_CircuitPython_ESP32SPI",
39+
"adafruit-circuitpython-esp-atcontrol",
40+
"adafruit_circuitpython_requests",
41+
],
3742
# Choose your license
3843
license="MIT",
3944
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)