|
28 | 28 | import requests
|
29 | 29 |
|
30 | 30 | from .errors import RequestError, ThrottlingError
|
31 |
| -from .model import Data, Feed, Group |
| 31 | +from .model import Data, Feed, Group, Dashboard, Block, Layout |
32 | 32 |
|
33 | 33 | # set outgoing version, pulled from setup.py
|
34 | 34 | version = pkg_resources.require("Adafruit_IO")[0].version
|
@@ -289,11 +289,13 @@ def create_feed(self, feed, group_key=None):
|
289 | 289 | :param string feed: Key of Adafruit IO feed.
|
290 | 290 | :param group_key group: Group to place new feed in.
|
291 | 291 | """
|
| 292 | + f = feed._asdict() |
| 293 | + del f['id'] # Don't pass id on create call |
292 | 294 | path = "feeds/"
|
293 | 295 | if group_key is not None: # create feed in a group
|
294 | 296 | path="/groups/%s/feeds"%group_key
|
295 |
| - return Feed.from_dict(self._post(path, {"feed": feed._asdict()})) |
296 |
| - return Feed.from_dict(self._post(path, {"feed": feed._asdict()})) |
| 297 | + return Feed.from_dict(self._post(path, {"feed": f})) |
| 298 | + return Feed.from_dict(self._post(path, {"feed": f})) |
297 | 299 |
|
298 | 300 | def delete_feed(self, feed):
|
299 | 301 | """Delete the specified feed.
|
@@ -326,3 +328,73 @@ def delete_group(self, group):
|
326 | 328 | """
|
327 | 329 | path = "groups/{0}".format(group)
|
328 | 330 | self._delete(path)
|
| 331 | + |
| 332 | + # Dashboard functionality. |
| 333 | + def dashboards(self, dashboard=None): |
| 334 | + """Retrieve a list of all dashboards, or the specified dashboard. |
| 335 | + :param string dashboard: Key of Adafruit IO Dashboard. Defaults to None. |
| 336 | + """ |
| 337 | + if dashboard is None: |
| 338 | + path = "dashboards/" |
| 339 | + return list(map(Dashboard.from_dict, self._get(path))) |
| 340 | + path = "dashboards/{0}".format(dashboard) |
| 341 | + return Dashboard.from_dict(self._get(path)) |
| 342 | + |
| 343 | + def create_dashboard(self, dashboard): |
| 344 | + """Create the specified dashboard. |
| 345 | + :param Dashboard dashboard: Dashboard object to create |
| 346 | + """ |
| 347 | + path = "dashboards/" |
| 348 | + return Dashboard.from_dict(self._post(path, dashboard._asdict())) |
| 349 | + |
| 350 | + def delete_dashboard(self, dashboard): |
| 351 | + """Delete the specified dashboard. |
| 352 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 353 | + """ |
| 354 | + path = "dashboards/{0}".format(dashboard) |
| 355 | + self._delete(path) |
| 356 | + |
| 357 | + # Block functionality. |
| 358 | + def blocks(self, dashboard, block=None): |
| 359 | + """Retrieve a list of all blocks from a dashboard, or the specified block. |
| 360 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 361 | + :param string block: id of Adafruit IO Block. Defaults to None. |
| 362 | + """ |
| 363 | + if block is None: |
| 364 | + path = "dashboards/{0}/blocks".format(dashboard) |
| 365 | + return list(map(Block.from_dict, self._get(path))) |
| 366 | + path = "dashboards/{0}/blocks/{1}".format(dashboard, block) |
| 367 | + return Block.from_dict(self._get(path)) |
| 368 | + |
| 369 | + def create_block(self, dashboard, block): |
| 370 | + """Create the specified block under the specified dashboard. |
| 371 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 372 | + :param Block block: Block object to create under dashboard |
| 373 | + """ |
| 374 | + path = "dashboards/{0}/blocks".format(dashboard) |
| 375 | + return Block.from_dict(self._post(path, block._asdict())) |
| 376 | + |
| 377 | + def delete_block(self, dashboard, block): |
| 378 | + """Delete the specified block. |
| 379 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 380 | + :param string block: id of Adafruit IO Block. |
| 381 | + """ |
| 382 | + path = "dashboards/{0}/blocks/{1}".format(dashboard, block) |
| 383 | + self._delete(path) |
| 384 | + |
| 385 | + # Layout functionality. |
| 386 | + def layouts(self, dashboard): |
| 387 | + """Retrieve the layouts array from a dashboard |
| 388 | + :param string dashboard: key of Adafruit IO Dashboard. |
| 389 | + """ |
| 390 | + path = "dashboards/{0}".format(dashboard) |
| 391 | + dashboard = self._get(path) |
| 392 | + return Layout.from_dict(dashboard['layouts']) |
| 393 | + |
| 394 | + def update_layout(self, dashboard, layout): |
| 395 | + """Update the layout of the specified dashboard. |
| 396 | + :param string dashboard: Key of Adafruit IO Dashboard. |
| 397 | + :param Layout layout: Layout object to update under dashboard |
| 398 | + """ |
| 399 | + path = "dashboards/{0}/update_layouts".format(dashboard) |
| 400 | + return Layout.from_dict(self._post(path, {'layouts': layout._asdict()})) |
0 commit comments