1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
--------
Examples
--------
.. contents::
:local:
Sample program
==============
.. literalinclude:: ../examples/remote.py
:language: python
Here's what running that sample would print::
$ AWAIR_TOKEN=foo python examples/remote.py
Device: <AwairDevice: uuid=awair_24947 model=Awair>
dust: 13.7
temperature: 22.12
humidity: 45.18
carbon_dioxide: 1114.0
volatile_organic_compounds: 545.0
temperature again: 22.12
Instantiating a client
======================
To instantiate a client, you'll need your access token and
must also pass in an aiohttp session:
.. code:: python
async with aiohttp.ClientSession() as session:
client = Awair(access_token="token", session=session)
Getting the current user
========================
This example retrieves the user, and prints out some information.
.. code:: python
async with aiohttp.ClientSession() as session:
client = Awair(session=session, token="token")
user = await client.user()
if user.dob is not None:
print(f"This user was born on: {user.dob}")
for method, limit in user.permissions.items():
print(f"Method: {method} - {limit}")
Listing a user's devices
========================
To retrieve every device a user can see:
.. code:: python
async with aiohttp.ClientSession() as session:
client = Awair(session=session, token="token")
user = await client.user()
devices = await user.devices()
for device in devices:
print(f"I can see this device: {device}")
Fetching recent data for a device
=================================
.. code:: python
async with aiohttp.ClientSession() as session:
client = Awair(session=session, token="token")
user = await client.user()
devices = await user.devices()
device = devices[0]
data = await device.air_data_latest()
print(f"Awair score: {data.score}")
for sensor, value in data.sensors:
print(f"{sensor}: {round(value, 2)}")
if sensor in data.indices:
print(f" awair index: {data.indices[sensor]}")
Fetching data from a different time
===================================
.. code:: python
async with aiohttp.ClientSession() as session:
client = Awair(session=session, token="token")
user = await client.user()
devices = await user.devices()
device = devices[0]
data = await device.air_data_five_minute(
fahrenheit=True,
limit=4,
from=(datetime.now() - timedelta(hours=2)),
to=(datetime.now() - timedelta(hours=1, minutes=30))
)
for datum in data:
print("----------------------------")
print(f"Data at: {datum.timestamp}")
print(f"Awair score: {datum.score}")
for sensor, value in datum.sensors:
print(f"{sensor}: {round(value, 2)}")
if sensor in datum.indices:
print(f" awair index: {datum.indices[sensor]}")
Sample local sensors program
=================================
Awair recently added the `local sensors API`_, where you can retrieve current (and only current)
air data from devices on your local network over HTTP.
.. _`local sensors API`: https://docs.google.com/document/d/1001C-ro_ig7aEyz0GiWUiiJn0M6DLj47BYWj31acesg/edit
.. literalinclude:: ../examples/local.py
:language: python
Running this sample prints::
$ AWAIR_DEVICE=192.168.0.5 python examples/local.py
Device: <AwairDevice: uuid=awair-element_5366 model=Awair Element>
dew_point: 10.81
abs_humid: 9.59
co2_est: 461
voc_baseline: 2536742680
voc_h2_raw: 27
voc_ethanol_raw: 39
pm10_est: 3
temperature: 19.16
humidity: 58.46
carbon_dioxide: 438
volatile_organic_compounds: 384
particulate_matter_2_5: 2
temperature again: 19.16
|