@@ -61,18 +61,13 @@ def __init__(self, username, key, proxies=None, base_url='https://io.adafruit.co
61
61
@staticmethod
62
62
def _create_payload (value , metadata ):
63
63
if metadata is not None :
64
- payload = Data (value = value ,lat = metadata ['lat' ], lon = metadata ['lon' ],
65
- ele = metadata ['ele' ], created_at = metadata ['created_at' ])
64
+ payload = Data (value = value , lat = metadata ['lat' ], lon = metadata ['lon' ],
65
+ ele = metadata ['ele' ], created_at = metadata ['created_at' ])
66
66
return payload
67
67
return Data (value = value )
68
68
69
- def _compose_url (self , path , is_time = None ):
70
- if is_time : # return a call to https://io.adafruit.com/api/v2/time/{unit}
71
- return '{0}/api/{1}/{2}' .format (self .base_url , 'v2' , path )
72
- else :
73
- return '{0}/api/{1}/{2}/{3}' .format (self .base_url , 'v2' , self .username , path )
74
-
75
- def _handle_error (self , response ):
69
+ @staticmethod
70
+ def _handle_error (response ):
76
71
# Throttling Error
77
72
if response .status_code == 429 :
78
73
raise ThrottlingError ()
@@ -84,6 +79,11 @@ def _handle_error(self, response):
84
79
raise RequestError (response )
85
80
# Else do nothing if there was no error.
86
81
82
+ def _compose_url (self , path , is_time = None ):
83
+ if is_time : # return a call to https://io.adafruit.com/api/v2/time/{unit}
84
+ return '{0}/api/{1}/{2}' .format (self .base_url , 'v2' , path )
85
+ return '{0}/api/{1}/{2}/{3}' .format (self .base_url , 'v2' , self .username , path )
86
+
87
87
def _headers (self , given ):
88
88
headers = default_headers .copy ()
89
89
headers .update (given )
@@ -111,7 +111,7 @@ def _post(self, path, data):
111
111
def _delete (self , path ):
112
112
response = requests .delete (self ._compose_url (path ),
113
113
headers = self ._headers ({'X-AIO-Key' : self .key ,
114
- 'Content-Type' : 'application/json' }),
114
+ 'Content-Type' : 'application/json' }),
115
115
proxies = self .proxies )
116
116
self ._handle_error (response )
117
117
@@ -130,7 +130,7 @@ def send_data(self, feed, value, metadata=None, precision=None):
130
130
try :
131
131
value = round (value , precision )
132
132
except NotImplementedError :
133
- raise NotImplementedError ("Using the precision kwarg requires a floating point value" )
133
+ raise NotImplementedError ("Using the precision kwarg requires a float value" )
134
134
payload = self ._create_payload (value , metadata )
135
135
return self .create_data (feed , payload )
136
136
@@ -164,26 +164,26 @@ def receive_time(self, time):
164
164
"""
165
165
timepath = "time/{0}" .format (time )
166
166
return self ._get (timepath , is_time = True )
167
-
167
+
168
168
def receive_weather (self , weather_id = None ):
169
169
"""Adafruit IO Weather Service, Powered by Dark Sky
170
170
:param int id: optional ID for retrieving a specified weather record.
171
171
"""
172
172
if weather_id :
173
- weather_path = "integrations/weather/{0}" .format (weather_id )
173
+ weather_path = "integrations/weather/{0}" .format (weather_id )
174
174
else :
175
- weather_path = "integrations/weather"
175
+ weather_path = "integrations/weather"
176
176
return self ._get (weather_path )
177
-
177
+
178
178
def receive_random (self , id = None ):
179
179
"""Access to Adafruit IO's Random Data
180
180
service.
181
181
:param int id: optional ID for retrieving a specified randomizer.
182
182
"""
183
183
if id :
184
- random_path = "integrations/words/{0}" .format (id )
184
+ random_path = "integrations/words/{0}" .format (id )
185
185
else :
186
- random_path = "integrations/words"
186
+ random_path = "integrations/words"
187
187
return self ._get (random_path )
188
188
189
189
def receive (self , feed ):
@@ -195,7 +195,7 @@ def receive(self, feed):
195
195
return Data .from_dict (self ._get (path ))
196
196
197
197
def receive_next (self , feed ):
198
- """Retrieve the next unread value from the specified feed. Returns a Data
198
+ """Retrieve the next unread value from the specified feed. Returns a Data
199
199
instance whose value property holds the retrieved value.
200
200
:param string feed: Name/Key/ID of Adafruit IO feed.
201
201
"""
@@ -225,10 +225,10 @@ def data(self, feed, data_id=None):
225
225
226
226
def create_data (self , feed , data ):
227
227
"""Create a new row of data in the specified feed.
228
- Returns a Data instance with details about the newly
228
+ Returns a Data instance with details about the newly
229
229
appended row of data.
230
230
:param string feed: Name/Key/ID of Adafruit IO feed.
231
- :param Data data: Instance of the Data class. Must have a value property set.
231
+ :param Data data: Instance of the Data class. Must have a value property set.
232
232
"""
233
233
path = "feeds/{0}/data" .format (feed )
234
234
return Data .from_dict (self ._post (path , data ._asdict ()))
@@ -248,7 +248,7 @@ def toRed(self, data):
248
248
return ((int (data [1 ], 16 ))* 16 ) + int (data [2 ], 16 )
249
249
250
250
def toGreen (self , data ):
251
- """Hex color feed to green channel.
251
+ """Hex color feed to green channel.
252
252
:param int data: Color value, in hexadecimal.
253
253
"""
254
254
return (int (data [3 ], 16 ) * 16 ) + int (data [4 ], 16 )
@@ -310,4 +310,4 @@ def delete_group(self, group):
310
310
:param string group: Name/Key/ID of Adafruit IO Group.
311
311
"""
312
312
path = "groups/{0}" .format (group )
313
- self ._delete (path )
313
+ self ._delete (path )
0 commit comments