|
18 | 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19 | 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20 | 20 | # SOFTWARE.
|
| 21 | +import time |
21 | 22 | from time import struct_time
|
22 | 23 | import json
|
23 | 24 | import platform
|
|
30 | 31 | import requests
|
31 | 32 |
|
32 | 33 | from .errors import RequestError, ThrottlingError
|
33 |
| -from .model import Data, Feed, Group |
| 34 | +from .model import Data, Feed, Group, Dashboard, Block, Layout |
34 | 35 |
|
35 | 36 | API_PAGE_LIMIT = 1000
|
36 | 37 |
|
@@ -194,9 +195,19 @@ def receive_time(self):
|
194 | 195 | https://docs.python.org/3.7/library/time.html#time.struct_time
|
195 | 196 | """
|
196 | 197 | path = 'integrations/time/struct.json'
|
197 |
| - time = self._get(path) |
198 |
| - return struct_time((time['year'], time['mon'], time['mday'], time['hour'], |
199 |
| - time['min'], time['sec'], time['wday'], time['yday'], time['isdst'])) |
| 198 | + return self._parse_time_struct(self._get(path)) |
| 199 | + |
| 200 | + @staticmethod |
| 201 | + def _parse_time_struct(time_dict: dict) -> time.struct_time: |
| 202 | + """Parse the time data returned by the server and return a time_struct |
| 203 | +
|
| 204 | + Corrects for the weekday returned by the server in Sunday=0 format |
| 205 | + (Python expects Monday=0) |
| 206 | + """ |
| 207 | + wday = (time_dict['wday'] - 1) % 7 |
| 208 | + return struct_time((time_dict['year'], time_dict['mon'], time_dict['mday'], |
| 209 | + time_dict['hour'], time_dict['min'], time_dict['sec'], |
| 210 | + wday, time_dict['yday'], time_dict['isdst'])) |
200 | 211 |
|
201 | 212 | def receive_weather(self, weather_id=None):
|
202 | 213 | """Adafruit IO Weather Service, Powered by Dark Sky
|
@@ -326,11 +337,13 @@ def create_feed(self, feed, group_key=None):
|
326 | 337 | :param string feed: Key of Adafruit IO feed.
|
327 | 338 | :param group_key group: Group to place new feed in.
|
328 | 339 | """
|
| 340 | + f = feed._asdict() |
| 341 | + del f['id'] # Don't pass id on create call |
329 | 342 | path = "feeds/"
|
330 | 343 | if group_key is not None: # create feed in a group
|
331 | 344 | path="/groups/%s/feeds"%group_key
|
332 |
| - return Feed.from_dict(self._post(path, {"feed": feed._asdict()})) |
333 |
| - return Feed.from_dict(self._post(path, {"feed": feed._asdict()})) |
| 345 | + return Feed.from_dict(self._post(path, {"feed": f})) |
| 346 | + return Feed.from_dict(self._post(path, {"feed": f})) |
334 | 347 |
|
335 | 348 | def delete_feed(self, feed):
|
336 | 349 | """Delete the specified feed.
|
@@ -363,3 +376,73 @@ def delete_group(self, group):
|
363 | 376 | """
|
364 | 377 | path = "groups/{0}".format(group)
|
365 | 378 | self._delete(path)
|
| 379 | + |
| 380 | + # Dashboard functionality. |
| 381 | + def dashboards(self, dashboard=None): |
| 382 | + """Retrieve a list of all dashboards, or the specified dashboard. |
| 383 | + :param string dashboard: Key of Adafruit IO Dashboard. Defaults to None. |
| 384 | + """ |
| 385 | + if dashboard is None: |
| 386 | + path = "dashboards/" |
| 387 | + return list(map(Dashboard.from_dict, self._get(path))) |
| 388 | + path = "dashboards/{0}".format(dashboard) |
| 389 | + return Dashboard.from_dict(self._get(path)) |
| 390 | + |
| 391 | + def create_dashboard(self, dashboard): |
| 392 | + """Create the specified dashboard. |
| 393 | + :param Dashboard dashboard: Dashboard object to create |
| 394 | + """ |
| 395 | + path = "dashboards/" |
| 396 | + return Dashboard.from_dict(self._post(path, dashboard._asdict())) |
| 397 | + |
| 398 | + def delete_dashboard(self, dashboard): |
| 399 | + """Delete the specified dashboard. |
| 400 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 401 | + """ |
| 402 | + path = "dashboards/{0}".format(dashboard) |
| 403 | + self._delete(path) |
| 404 | + |
| 405 | + # Block functionality. |
| 406 | + def blocks(self, dashboard, block=None): |
| 407 | + """Retrieve a list of all blocks from a dashboard, or the specified block. |
| 408 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 409 | + :param string block: id of Adafruit IO Block. Defaults to None. |
| 410 | + """ |
| 411 | + if block is None: |
| 412 | + path = "dashboards/{0}/blocks".format(dashboard) |
| 413 | + return list(map(Block.from_dict, self._get(path))) |
| 414 | + path = "dashboards/{0}/blocks/{1}".format(dashboard, block) |
| 415 | + return Block.from_dict(self._get(path)) |
| 416 | + |
| 417 | + def create_block(self, dashboard, block): |
| 418 | + """Create the specified block under the specified dashboard. |
| 419 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 420 | + :param Block block: Block object to create under dashboard |
| 421 | + """ |
| 422 | + path = "dashboards/{0}/blocks".format(dashboard) |
| 423 | + return Block.from_dict(self._post(path, block._asdict())) |
| 424 | + |
| 425 | + def delete_block(self, dashboard, block): |
| 426 | + """Delete the specified block. |
| 427 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 428 | + :param string block: id of Adafruit IO Block. |
| 429 | + """ |
| 430 | + path = "dashboards/{0}/blocks/{1}".format(dashboard, block) |
| 431 | + self._delete(path) |
| 432 | + |
| 433 | + # Layout functionality. |
| 434 | + def layouts(self, dashboard): |
| 435 | + """Retrieve the layouts array from a dashboard |
| 436 | + :param string dashboard: key of Adafruit IO Dashboard. |
| 437 | + """ |
| 438 | + path = "dashboards/{0}".format(dashboard) |
| 439 | + dashboard = self._get(path) |
| 440 | + return Layout.from_dict(dashboard['layouts']) |
| 441 | + |
| 442 | + def update_layout(self, dashboard, layout): |
| 443 | + """Update the layout of the specified dashboard. |
| 444 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 445 | + :param Layout layout: Layout object to update under dashboard |
| 446 | + """ |
| 447 | + path = "dashboards/{0}/update_layouts".format(dashboard) |
| 448 | + return Layout.from_dict(self._post(path, {'layouts': layout._asdict()})) |
0 commit comments