Skip to content

Commit cfe93c6

Browse files
committed
correct Missing Type Annotations #17
1 parent 5d18869 commit cfe93c6

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

adafruit_hue.py

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

32+
try:
33+
from typing import Optional, List, Union, Any
34+
except ImportError:
35+
pass
36+
3237
__version__ = "0.0.0+auto.0"
3338
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Hue.git"
3439

@@ -38,7 +43,12 @@ class Bridge:
3843
HTTP Interface for interacting with a Philips Hue Bridge.
3944
"""
4045

41-
def __init__(self, wifi_manager, bridge_ip=None, username=None):
46+
def __init__(
47+
self,
48+
wifi_manager: Any,
49+
bridge_ip: Optional[str] = None,
50+
username: Optional[str] = None,
51+
) -> None:
4252
"""
4353
Creates an instance of the Philips Hue Bridge Interface.
4454
:param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
@@ -51,11 +61,11 @@ def __init__(self, wifi_manager, bridge_ip=None, username=None):
5161
self._ip = bridge_ip
5262
self._username = username
5363
if bridge_ip and username is not None:
54-
self._bridge_url = "http://{}/api".format(self._ip)
64+
self._bridge_url = f"http://{self._ip}/api"
5565
self._username_url = self._bridge_url + "/" + self._username
5666

5767
@staticmethod
58-
def rgb_to_hsb(rgb):
68+
def rgb_to_hsb(rgb: tuple) -> tuple:
5969
"""Returns RGB values as a HSL tuple.
6070
:param list rgb: RGB Values
6171
"""
@@ -89,7 +99,7 @@ def rgb_to_hsb(rgb):
8999
return round(hue), round(sat, 3), round(light, 2)
90100

91101
# Hue Core API
92-
def discover_bridge(self):
102+
def discover_bridge(self) -> str:
93103
"""Discovers Philips Hue Bridge IP from the hosted broker discovery service.
94104
Returns the bridge's IP address.
95105
"""
@@ -105,16 +115,16 @@ def discover_bridge(self):
105115
) from err
106116
self._ip = bridge_ip
107117
# set up hue bridge address path
108-
self._bridge_url = "http://{}/api".format(self._ip)
118+
self._bridge_url = f"http://{self._ip}/api"
109119
return self._ip
110120

111-
def register_username(self):
121+
def register_username(self) -> str:
112122
"""Attempts to register a Hue application username for use with your bridge.
113123
Provides a 30 second delay to press the link button on the bridge.
114124
Returns username or None.
115125
"""
116-
self._bridge_url = "http://{}/api".format(self._ip)
117-
data = {"devicetype": "CircuitPython#pyportal{0}".format(randint(0, 100))}
126+
self._bridge_url = f"http://{self._ip}/api"
127+
data = {"devicetype": f"CircuitPython#pyportal{randint(0, 100)}"}
118128
resp = self._wifi.post(self._bridge_url, json=data)
119129
connection_attempts = 1
120130
username = None
@@ -130,14 +140,14 @@ def register_username(self):
130140
return username
131141

132142
# Lights API
133-
def show_light_info(self, light_id):
143+
def show_light_info(self, light_id: Union[int, str]) -> str:
134144
"""Gets the attributes and state of a given light.
135145
:param int light_id: Light identifier.
136146
"""
137-
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
147+
resp = self._get(f"{self._username_url}/lights/{light_id}")
138148
return resp
139149

140-
def set_light(self, light_id, **kwargs):
150+
def set_light(self, light_id: Union[int, str], **kwargs) -> str:
141151
"""Allows the user to turn the light on and off, modify the hue and effects.
142152
You can pass the following as valid kwargs into this method:
143153
:param bool on: On/Off state of the light
@@ -147,12 +157,10 @@ def set_light(self, light_id, **kwargs):
147157
(more settings at:
148158
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
149159
"""
150-
resp = self._put(
151-
"{0}/lights/{1}/state".format(self._username_url, light_id), kwargs
152-
)
160+
resp = self._put(f"{self._username_url}/lights/{light_id}/state", kwargs)
153161
return resp
154162

155-
def toggle_light(self, light_id):
163+
def toggle_light(self, light_id: Union[int, str]) -> str:
156164
"""Gets and toggles the current state of a specified light.
157165
:param int light_id: Light identifier.
158166
"""
@@ -161,20 +169,22 @@ def toggle_light(self, light_id):
161169
resp = self.set_light(light_id, on=light_state)
162170
return resp
163171

164-
def get_light(self, light_id):
172+
def get_light(self, light_id: Union[int, str]) -> str:
165173
"""Gets the attributes and state of a provided light.
166174
:param int light_id: Light identifier.
167175
"""
168-
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
176+
resp = self._get(f"{self._username_url}/lights/{light_id}")
169177
return resp
170178

171-
def get_lights(self):
179+
def get_lights(self) -> str:
172180
"""Returns all the light resources available for a bridge."""
173181
resp = self._get(self._username_url + "/lights")
174182
return resp
175183

176184
# Groups API
177-
def create_group(self, lights, group_id):
185+
def create_group(
186+
self, lights: List[Union[int, str]], group_id: Optional[str]
187+
) -> str:
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,7 +193,7 @@ 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) -> str:
187197
"""Allows the user to turn the light on and off, modify the hue and effects.
188198
:param int group_id: Group identifier.
189199
You can pass the following as (optional) valid kwargs into this method:
@@ -194,31 +204,29 @@ 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) -> str:
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):
209217
"""Sets a group scene.
210218
:param str scene: The scene identifier
211219
"""
212220
# To recall an existing scene, use the Groups API.
213221
self.set_group(group_id, scene=scene_id)
214222

215-
def get_scenes(self):
223+
def get_scenes(self) -> str:
216224
"""Returns a list of all scenes currently stored in the bridge."""
217225
resp = self._get(self._username_url + "/scenes")
218226
return resp
219227

220228
# HTTP Helpers for the Hue API
221-
def _post(self, path, data):
229+
def _post(self, path: str, data: str) -> str:
222230
"""POST data
223231
:param str path: Formatted Hue API URL
224232
:param json data: JSON data to POST to the Hue API.
@@ -228,7 +236,7 @@ def _post(self, path, data):
228236
resp.close()
229237
return resp_json
230238

231-
def _put(self, path, data):
239+
def _put(self, path: str, data: str):
232240
"""PUT data
233241
:param str path: Formatted Hue API URL
234242
:param json data: JSON data to PUT to the Hue API.
@@ -238,7 +246,7 @@ def _put(self, path, data):
238246
resp.close()
239247
return resp_json
240248

241-
def _get(self, path, data=None):
249+
def _get(self, path: str, data: Optional[str] = None):
242250
"""GET data
243251
:param str path: Formatted Hue API URL
244252
:param json data: JSON data to GET from the Hue API.

0 commit comments

Comments
 (0)