Skip to content

Commit bf019f4

Browse files
authored
Merge pull request #11 from adafruit/pylint-update
Pylint update
2 parents b623939 + 0574b16 commit bf019f4

File tree

5 files changed

+144
-133
lines changed

5 files changed

+144
-133
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_hue.py

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -49,58 +49,60 @@
4949
__version__ = "0.0.0-auto.0"
5050
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Hue.git"
5151

52+
5253
class Bridge:
5354
"""
5455
HTTP Interface for interacting with a Philips Hue Bridge.
5556
"""
57+
5658
def __init__(self, wifi_manager, bridge_ip=None, username=None):
5759
"""
5860
Creates an instance of the Philips Hue Bridge Interface.
5961
:param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
6062
"""
6163
wifi_type = str(type(wifi_manager))
62-
if ('ESPSPI_WiFiManager' in wifi_type or 'ESPAT_WiFiManager' in wifi_type):
64+
if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type:
6365
self._wifi = wifi_manager
6466
else:
6567
raise TypeError("This library requires a WiFiManager object.")
6668
self._ip = bridge_ip
6769
self._username = username
6870
if bridge_ip and username is not None:
69-
self._bridge_url = 'http://{}/api'.format(self._ip)
70-
self._username_url = self._bridge_url+'/'+ self._username
71+
self._bridge_url = "http://{}/api".format(self._ip)
72+
self._username_url = self._bridge_url + "/" + self._username
7173

7274
@staticmethod
7375
def rgb_to_hsb(rgb):
7476
"""Returns RGB values as a HSL tuple.
7577
:param list rgb: RGB Values
7678
"""
77-
r = rgb[0]/255
78-
g = rgb[1]/255
79-
b = rgb[2]/255
79+
r = rgb[0] / 255
80+
g = rgb[1] / 255
81+
b = rgb[2] / 255
8082
c_max = max(r, g, b)
8183
c_min = min(r, g, b)
82-
delta = c_max-c_min
83-
light = ((c_max+c_min)/2)
84+
delta = c_max - c_min
85+
light = (c_max + c_min) / 2
8486
if delta == 0.0:
8587
hue = 0
8688
sat = 0
8789
else:
8890
if light < 0.5:
89-
sat = (c_max-c_min)/(c_max+c_min)
91+
sat = (c_max - c_min) / (c_max + c_min)
9092
else:
91-
sat = (c_max-c_min)/(2.0-c_max-c_min)
93+
sat = (c_max - c_min) / (2.0 - c_max - c_min)
9294
if c_max == r:
93-
hue = (g-b)/(c_max-c_min)
95+
hue = (g - b) / (c_max - c_min)
9496
elif c_max == g:
95-
hue = 2.0 + (b-r)/(c_max-c_min)
97+
hue = 2.0 + (b - r) / (c_max - c_min)
9698
else:
97-
hue = 4.0 + (r-g)/(c_max-c_min)
99+
hue = 4.0 + (r - g) / (c_max - c_min)
98100
hue *= 60
99101
if hue < 0:
100102
hue += 360
101103
hue = map_range(hue, 0, 360, 0, 65535)
102-
sat = map_range(sat*100, 0, 100, 0, 254)
103-
light = map_range(light*100, 0, 100, 0, 254)
104+
sat = map_range(sat * 100, 0, 100, 0, 254)
105+
light = map_range(light * 100, 0, 100, 0, 254)
104106
return round(hue), round(sat, 3), round(light, 2)
105107

106108
# Hue Core API
@@ -109,34 +111,36 @@ def discover_bridge(self):
109111
Returns the bridge's IP address.
110112
"""
111113
try:
112-
resp = self._wifi.get('https://discovery.meethue.com')
114+
resp = self._wifi.get("https://discovery.meethue.com")
113115
json_data = resp.json()
114-
bridge_ip = json_data[0]['internalipaddress']
116+
bridge_ip = json_data[0]["internalipaddress"]
115117
resp.close()
116118
except:
117-
raise TypeError('Ensure the Philips Bridge and CircuitPython device\
118-
are both on the same WiFi network.')
119+
raise TypeError(
120+
"Ensure the Philips Bridge and CircuitPython device\
121+
are both on the same WiFi network."
122+
)
119123
self._ip = bridge_ip
120124
# set up hue bridge address path
121-
self._bridge_url = 'http://{}/api'.format(self._ip)
125+
self._bridge_url = "http://{}/api".format(self._ip)
122126
return self._ip
123127

124128
def register_username(self):
125129
"""Attempts to register a Hue application username for use with your bridge.
126130
Provides a 30 second delay to press the link button on the bridge.
127131
Returns username or None.
128132
"""
129-
self._bridge_url = 'http://{}/api'.format(self._ip)
130-
data = {"devicetype":"CircuitPython#pyportal{0}".format(randint(0, 100))}
133+
self._bridge_url = "http://{}/api".format(self._ip)
134+
data = {"devicetype": "CircuitPython#pyportal{0}".format(randint(0, 100))}
131135
resp = self._wifi.post(self._bridge_url, json=data)
132136
connection_attempts = 1
133137
username = None
134138
while username is None and connection_attempts > 0:
135139
resp = self._wifi.post(self._bridge_url, json=data)
136140
json = resp.json()[0]
137-
if json.get('success'):
138-
username = str(json['success']['username'])
139-
self._username_url = self._bridge_url+'/'+ username
141+
if json.get("success"):
142+
username = str(json["success"]["username"])
143+
self._username_url = self._bridge_url + "/" + username
140144
connection_attempts -= 1
141145
time.sleep(1)
142146
resp.close()
@@ -147,7 +151,7 @@ def show_light_info(self, light_id):
147151
"""Gets the attributes and state of a given light.
148152
:param int light_id: Light identifier.
149153
"""
150-
resp = self._get('{0}/lights/{1}'.format(self._username_url, light_id))
154+
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
151155
return resp
152156

153157
def set_light(self, light_id, **kwargs):
@@ -160,29 +164,31 @@ def set_light(self, light_id, **kwargs):
160164
(more settings at:
161165
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
162166
"""
163-
resp = self._put('{0}/lights/{1}/state'.format(self._username_url, light_id), kwargs)
167+
resp = self._put(
168+
"{0}/lights/{1}/state".format(self._username_url, light_id), kwargs
169+
)
164170
return resp
165171

166172
def toggle_light(self, light_id):
167173
"""Gets and toggles the current state of a specified light.
168174
:param int light_id: Light identifier.
169175
"""
170176
light_state = self.get_light(light_id)
171-
light_state = not light_state['state']['on']
177+
light_state = not light_state["state"]["on"]
172178
resp = self.set_light(light_id, on=light_state)
173179
return resp
174180

175181
def get_light(self, light_id):
176182
"""Gets the attributes and state of a provided light.
177183
:param int light_id: Light identifier.
178184
"""
179-
resp = self._get('{0}/lights/{1}'.format(self._username_url, light_id))
185+
resp = self._get("{0}/lights/{1}".format(self._username_url, light_id))
180186
return resp
181187

182188
def get_lights(self):
183189
"""Returns all the light resources available for a bridge.
184190
"""
185-
resp = self._get(self._username_url+'/lights')
191+
resp = self._get(self._username_url + "/lights")
186192
return resp
187193

188194
# Groups API
@@ -191,11 +197,8 @@ def create_group(self, lights, group_id):
191197
:param list lights: List of light identifiers.
192198
:param str group_id: Optional group name.
193199
"""
194-
data = {'lights':lights,
195-
'name':group_id,
196-
'type':group_id
197-
}
198-
resp = self._post(self._username_url+'/groups', data)
200+
data = {"lights": lights, "name": group_id, "type": group_id}
201+
resp = self._post(self._username_url + "/groups", data)
199202
return resp
200203

201204
def set_group(self, group_id, **kwargs):
@@ -209,13 +212,15 @@ def set_group(self, group_id, **kwargs):
209212
(more settings at
210213
https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state )
211214
"""
212-
resp = self._put('{0}/groups/{1}/action'.format(self._username_url, group_id), kwargs)
215+
resp = self._put(
216+
"{0}/groups/{1}/action".format(self._username_url, group_id), kwargs
217+
)
213218
return resp
214219

215220
def get_groups(self):
216221
"""Returns all the light groups available for a bridge.
217222
"""
218-
resp = self._get(self._username_url+'/groups')
223+
resp = self._get(self._username_url + "/groups")
219224
return resp
220225

221226
# Scene API
@@ -229,7 +234,7 @@ def set_scene(self, group_id, scene_id):
229234
def get_scenes(self):
230235
"""Returns a list of all scenes currently stored in the bridge.
231236
"""
232-
resp = self._get(self._username_url+'/scenes')
237+
resp = self._get(self._username_url + "/scenes")
233238
return resp
234239

235240
# HTTP Helpers for the Hue API
@@ -238,10 +243,7 @@ def _post(self, path, data):
238243
:param str path: Formatted Hue API URL
239244
:param json data: JSON data to POST to the Hue API.
240245
"""
241-
resp = self._wifi.post(
242-
path,
243-
json=data
244-
)
246+
resp = self._wifi.post(path, json=data)
245247
resp_json = resp.json()
246248
resp.close()
247249
return resp_json
@@ -251,10 +253,7 @@ def _put(self, path, data):
251253
:param str path: Formatted Hue API URL
252254
:param json data: JSON data to PUT to the Hue API.
253255
"""
254-
resp = self._wifi.put(
255-
path,
256-
json=data
257-
)
256+
resp = self._wifi.put(path, json=data)
258257
resp_json = resp.json()
259258
resp.close()
260259
return resp_json
@@ -264,10 +263,7 @@ def _get(self, path, data=None):
264263
:param str path: Formatted Hue API URL
265264
:param json data: JSON data to GET from the Hue API.
266265
"""
267-
resp = self._wifi.get(
268-
path,
269-
json=data
270-
)
266+
resp = self._wifi.get(path, json=data)
271267
resp_json = resp.json()
272268
resp.close()
273269
return resp_json

0 commit comments

Comments
 (0)