Skip to content

Commit ec1bff2

Browse files
authored
Merge pull request #22 from tcfranks/main
correct Missing Type Annotations #17
2 parents 5d18869 + b7f18b1 commit ec1bff2

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

adafruit_hue.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
from random import randint
3030
from simpleio import map_range
3131

32+
try:
33+
from typing import Optional, List, Union, Sequence, Any
34+
from circuitpython_typing.http import HTTPProtocol
35+
except ImportError:
36+
pass
37+
3238
__version__ = "0.0.0+auto.0"
3339
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Hue.git"
3440

@@ -38,7 +44,12 @@ class Bridge:
3844
HTTP Interface for interacting with a Philips Hue Bridge.
3945
"""
4046

41-
def __init__(self, wifi_manager, bridge_ip=None, username=None):
47+
def __init__(
48+
self,
49+
wifi_manager: HTTPProtocol,
50+
bridge_ip: Optional[str] = None,
51+
username: Optional[str] = None,
52+
) -> None:
4253
"""
4354
Creates an instance of the Philips Hue Bridge Interface.
4455
:param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
@@ -51,11 +62,11 @@ def __init__(self, wifi_manager, bridge_ip=None, username=None):
5162
self._ip = bridge_ip
5263
self._username = username
5364
if bridge_ip and username is not None:
54-
self._bridge_url = "http://{}/api".format(self._ip)
65+
self._bridge_url = f"http://{self._ip}/api"
5566
self._username_url = self._bridge_url + "/" + self._username
5667

5768
@staticmethod
58-
def rgb_to_hsb(rgb):
69+
def rgb_to_hsb(rgb: Sequence[int]) -> Sequence[int]:
5970
"""Returns RGB values as a HSL tuple.
6071
:param list rgb: RGB Values
6172
"""
@@ -89,7 +100,7 @@ def rgb_to_hsb(rgb):
89100
return round(hue), round(sat, 3), round(light, 2)
90101

91102
# Hue Core API
92-
def discover_bridge(self):
103+
def discover_bridge(self) -> str:
93104
"""Discovers Philips Hue Bridge IP from the hosted broker discovery service.
94105
Returns the bridge's IP address.
95106
"""
@@ -105,16 +116,16 @@ def discover_bridge(self):
105116
) from err
106117
self._ip = bridge_ip
107118
# set up hue bridge address path
108-
self._bridge_url = "http://{}/api".format(self._ip)
119+
self._bridge_url = f"http://{self._ip}/api"
109120
return self._ip
110121

111-
def register_username(self):
122+
def register_username(self) -> str:
112123
"""Attempts to register a Hue application username for use with your bridge.
113124
Provides a 30 second delay to press the link button on the bridge.
114125
Returns username or None.
115126
"""
116-
self._bridge_url = "http://{}/api".format(self._ip)
117-
data = {"devicetype": "CircuitPython#pyportal{0}".format(randint(0, 100))}
127+
self._bridge_url = f"http://{self._ip}/api"
128+
data = {"devicetype": f"CircuitPython#pyportal{randint(0, 100)}"}
118129
resp = self._wifi.post(self._bridge_url, json=data)
119130
connection_attempts = 1
120131
username = None
@@ -130,51 +141,50 @@ def register_username(self):
130141
return username
131142

132143
# Lights API
133-
def show_light_info(self, light_id):
144+
def show_light_info(self, light_id: Union[int, str]) -> str:
134145
"""Gets the attributes and state of a given light.
135-
:param int light_id: Light identifier.
146+
:param int|str light_id: Light identifier.
136147
"""
137-
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
148+
resp = self._get(f"{self._username_url}/lights/{light_id}")
138149
return resp
139150

140-
def set_light(self, light_id, **kwargs):
151+
def set_light(self, light_id: Union[int, str], **kwargs) -> str:
141152
"""Allows the user to turn the light on and off, modify the hue and effects.
142153
You can pass the following as valid kwargs into this method:
154+
:param int|str light_id: Light identifier
143155
:param bool on: On/Off state of the light
144156
:param int bri: Brightness value of the light, 0-100% (1 to 254)
145157
:param int hue: Hue value to set the light, in degrees (0 to 360) (0 to 65535)
146158
:param int sat: Saturation of the light, 0-100% (0 to 254)
147159
(more settings at:
148160
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
149161
"""
150-
resp = self._put(
151-
"{0}/lights/{1}/state".format(self._username_url, light_id), kwargs
152-
)
162+
resp = self._put(f"{self._username_url}/lights/{light_id}/state", kwargs)
153163
return resp
154164

155-
def toggle_light(self, light_id):
165+
def toggle_light(self, light_id: Union[int, str]) -> str:
156166
"""Gets and toggles the current state of a specified light.
157-
:param int light_id: Light identifier.
167+
:param int|str light_id: Light identifier.
158168
"""
159169
light_state = self.get_light(light_id)
160170
light_state = not light_state["state"]["on"]
161171
resp = self.set_light(light_id, on=light_state)
162172
return resp
163173

164-
def get_light(self, light_id):
174+
def get_light(self, light_id: Union[int, str]) -> str:
165175
"""Gets the attributes and state of a provided light.
166-
:param int light_id: Light identifier.
176+
:param int|str light_id: Light identifier.
167177
"""
168-
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
178+
resp = self._get(f"{self._username_url}/lights/{light_id}")
169179
return resp
170180

171-
def get_lights(self):
181+
def get_lights(self) -> Any:
172182
"""Returns all the light resources available for a bridge."""
173183
resp = self._get(self._username_url + "/lights")
174184
return resp
175185

176186
# Groups API
177-
def create_group(self, lights, group_id):
187+
def create_group(self, lights: List[Union[int, str]], group_id: str) -> Any:
178188
"""Creates a new group containing the lights specified and optional name.
179189
:param list lights: List of light identifiers.
180190
:param str group_id: Optional group name.
@@ -183,9 +193,9 @@ def create_group(self, lights, group_id):
183193
resp = self._post(self._username_url + "/groups", data)
184194
return resp
185195

186-
def set_group(self, group_id, **kwargs):
196+
def set_group(self, group_id: Union[int, str], **kwargs) -> Any:
187197
"""Allows the user to turn the light on and off, modify the hue and effects.
188-
:param int group_id: Group identifier.
198+
:param int|str group_id: Group identifier.
189199
You can pass the following as (optional) valid kwargs into this method:
190200
:param bool on: On/Off state of the light
191201
:param int bri: Brightness value of the light (1 to 254)
@@ -194,31 +204,30 @@ def set_group(self, group_id, **kwargs):
194204
(more settings at
195205
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
196206
"""
197-
resp = self._put(
198-
"{0}/groups/{1}/action".format(self._username_url, group_id), kwargs
199-
)
207+
resp = self._put(f"{self._username_url}/groups/{group_id}/action", kwargs)
200208
return resp
201209

202-
def get_groups(self):
210+
def get_groups(self) -> Any:
203211
"""Returns all the light groups available for a bridge."""
204212
resp = self._get(self._username_url + "/groups")
205213
return resp
206214

207215
# Scene API
208-
def set_scene(self, group_id, scene_id):
216+
def set_scene(self, group_id: Union[int, str], scene_id: str) -> None:
209217
"""Sets a group scene.
218+
:param int|str group_id: the group identifier
210219
:param str scene: The scene identifier
211220
"""
212221
# To recall an existing scene, use the Groups API.
213222
self.set_group(group_id, scene=scene_id)
214223

215-
def get_scenes(self):
224+
def get_scenes(self) -> Any:
216225
"""Returns a list of all scenes currently stored in the bridge."""
217226
resp = self._get(self._username_url + "/scenes")
218227
return resp
219228

220229
# HTTP Helpers for the Hue API
221-
def _post(self, path, data):
230+
def _post(self, path: str, data: str) -> Any:
222231
"""POST data
223232
:param str path: Formatted Hue API URL
224233
:param json data: JSON data to POST to the Hue API.
@@ -228,7 +237,7 @@ def _post(self, path, data):
228237
resp.close()
229238
return resp_json
230239

231-
def _put(self, path, data):
240+
def _put(self, path: str, data: str) -> Any:
232241
"""PUT data
233242
:param str path: Formatted Hue API URL
234243
:param json data: JSON data to PUT to the Hue API.
@@ -238,7 +247,7 @@ def _put(self, path, data):
238247
resp.close()
239248
return resp_json
240249

241-
def _get(self, path, data=None):
250+
def _get(self, path: str, data: Optional[str] = None) -> Any:
242251
"""GET data
243252
:param str path: Formatted Hue API URL
244253
:param json data: JSON data to GET from the Hue API.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-simpleio
77
adafruit-circuitpython-esp32spi
8+
adafruit-circuitpython-typing

0 commit comments

Comments
 (0)