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