29
29
from random import randint
30
30
from simpleio import map_range
31
31
32
+ try :
33
+ from typing import Optional , List , Union , Any
34
+ except ImportError :
35
+ pass
36
+
32
37
__version__ = "0.0.0+auto.0"
33
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Hue.git"
34
39
@@ -38,7 +43,12 @@ class Bridge:
38
43
HTTP Interface for interacting with a Philips Hue Bridge.
39
44
"""
40
45
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 :
42
52
"""
43
53
Creates an instance of the Philips Hue Bridge Interface.
44
54
: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):
51
61
self ._ip = bridge_ip
52
62
self ._username = username
53
63
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"
55
65
self ._username_url = self ._bridge_url + "/" + self ._username
56
66
57
67
@staticmethod
58
- def rgb_to_hsb (rgb ) :
68
+ def rgb_to_hsb (rgb : tuple ) -> tuple :
59
69
"""Returns RGB values as a HSL tuple.
60
70
:param list rgb: RGB Values
61
71
"""
@@ -89,7 +99,7 @@ def rgb_to_hsb(rgb):
89
99
return round (hue ), round (sat , 3 ), round (light , 2 )
90
100
91
101
# Hue Core API
92
- def discover_bridge (self ):
102
+ def discover_bridge (self ) -> str :
93
103
"""Discovers Philips Hue Bridge IP from the hosted broker discovery service.
94
104
Returns the bridge's IP address.
95
105
"""
@@ -105,16 +115,16 @@ def discover_bridge(self):
105
115
) from err
106
116
self ._ip = bridge_ip
107
117
# set up hue bridge address path
108
- self ._bridge_url = "http://{}/api" . format ( self . _ip )
118
+ self ._bridge_url = f "http://{ self . _ip } /api"
109
119
return self ._ip
110
120
111
- def register_username (self ):
121
+ def register_username (self ) -> str :
112
122
"""Attempts to register a Hue application username for use with your bridge.
113
123
Provides a 30 second delay to press the link button on the bridge.
114
124
Returns username or None.
115
125
"""
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 )} " }
118
128
resp = self ._wifi .post (self ._bridge_url , json = data )
119
129
connection_attempts = 1
120
130
username = None
@@ -130,14 +140,14 @@ def register_username(self):
130
140
return username
131
141
132
142
# Lights API
133
- def show_light_info (self , light_id ) :
143
+ def show_light_info (self , light_id : Union [ int , str ]) -> str :
134
144
"""Gets the attributes and state of a given light.
135
145
:param int light_id: Light identifier.
136
146
"""
137
- resp = self ._get ("{0 }/lights/{1}" . format ( self . _username_url , light_id ) )
147
+ resp = self ._get (f" { self . _username_url } /lights/{ light_id } " )
138
148
return resp
139
149
140
- def set_light (self , light_id , ** kwargs ):
150
+ def set_light (self , light_id : Union [ int , str ], ** kwargs ) -> str :
141
151
"""Allows the user to turn the light on and off, modify the hue and effects.
142
152
You can pass the following as valid kwargs into this method:
143
153
:param bool on: On/Off state of the light
@@ -147,12 +157,10 @@ def set_light(self, light_id, **kwargs):
147
157
(more settings at:
148
158
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
149
159
"""
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 )
153
161
return resp
154
162
155
- def toggle_light (self , light_id ) :
163
+ def toggle_light (self , light_id : Union [ int , str ]) -> str :
156
164
"""Gets and toggles the current state of a specified light.
157
165
:param int light_id: Light identifier.
158
166
"""
@@ -161,20 +169,22 @@ def toggle_light(self, light_id):
161
169
resp = self .set_light (light_id , on = light_state )
162
170
return resp
163
171
164
- def get_light (self , light_id ) :
172
+ def get_light (self , light_id : Union [ int , str ]) -> str :
165
173
"""Gets the attributes and state of a provided light.
166
174
:param int light_id: Light identifier.
167
175
"""
168
- resp = self ._get ("{0 }/lights/{1}" . format ( self . _username_url , light_id ) )
176
+ resp = self ._get (f" { self . _username_url } /lights/{ light_id } " )
169
177
return resp
170
178
171
- def get_lights (self ):
179
+ def get_lights (self ) -> str :
172
180
"""Returns all the light resources available for a bridge."""
173
181
resp = self ._get (self ._username_url + "/lights" )
174
182
return resp
175
183
176
184
# 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 :
178
188
"""Creates a new group containing the lights specified and optional name.
179
189
:param list lights: List of light identifiers.
180
190
:param str group_id: Optional group name.
@@ -183,7 +193,7 @@ def create_group(self, lights, group_id):
183
193
resp = self ._post (self ._username_url + "/groups" , data )
184
194
return resp
185
195
186
- def set_group (self , group_id , ** kwargs ):
196
+ def set_group (self , group_id : Union [ int , str ], ** kwargs ) -> str :
187
197
"""Allows the user to turn the light on and off, modify the hue and effects.
188
198
:param int group_id: Group identifier.
189
199
You can pass the following as (optional) valid kwargs into this method:
@@ -194,31 +204,29 @@ def set_group(self, group_id, **kwargs):
194
204
(more settings at
195
205
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
196
206
"""
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 )
200
208
return resp
201
209
202
- def get_groups (self ):
210
+ def get_groups (self ) -> str :
203
211
"""Returns all the light groups available for a bridge."""
204
212
resp = self ._get (self ._username_url + "/groups" )
205
213
return resp
206
214
207
215
# Scene API
208
- def set_scene (self , group_id , scene_id ):
216
+ def set_scene (self , group_id : Union [ int , str ], scene_id : str ):
209
217
"""Sets a group scene.
210
218
:param str scene: The scene identifier
211
219
"""
212
220
# To recall an existing scene, use the Groups API.
213
221
self .set_group (group_id , scene = scene_id )
214
222
215
- def get_scenes (self ):
223
+ def get_scenes (self ) -> str :
216
224
"""Returns a list of all scenes currently stored in the bridge."""
217
225
resp = self ._get (self ._username_url + "/scenes" )
218
226
return resp
219
227
220
228
# HTTP Helpers for the Hue API
221
- def _post (self , path , data ) :
229
+ def _post (self , path : str , data : str ) -> str :
222
230
"""POST data
223
231
:param str path: Formatted Hue API URL
224
232
:param json data: JSON data to POST to the Hue API.
@@ -228,7 +236,7 @@ def _post(self, path, data):
228
236
resp .close ()
229
237
return resp_json
230
238
231
- def _put (self , path , data ):
239
+ def _put (self , path : str , data : str ):
232
240
"""PUT data
233
241
:param str path: Formatted Hue API URL
234
242
:param json data: JSON data to PUT to the Hue API.
@@ -238,7 +246,7 @@ def _put(self, path, data):
238
246
resp .close ()
239
247
return resp_json
240
248
241
- def _get (self , path , data = None ):
249
+ def _get (self , path : str , data : Optional [ str ] = None ):
242
250
"""GET data
243
251
:param str path: Formatted Hue API URL
244
252
:param json data: JSON data to GET from the Hue API.
0 commit comments