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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
"""Test basic python_awair functionality."""
from datetime import date, datetime, timedelta
from typing import Any
from unittest.mock import patch
import aiohttp
import pytest
import voluptuous as vol
from python_awair import Awair, AwairLocal, const
from python_awair.attrdict import AttrDict
from python_awair.exceptions import AuthError, AwairError, NotFoundError, QueryError
from tests.const import (
ACCESS_TOKEN,
AWAIR_GEN1_ID,
MOCK_ELEMENT_DEVICE_A_ATTRS,
MOCK_ELEMENT_DEVICE_B_ATTRS,
MOCK_GEN2_DEVICE_ATTRS,
MOCK_GLOW_DEVICE_ATTRS,
MOCK_MINT_DEVICE_ATTRS,
MOCK_OMNI_DEVICE_ATTRS,
)
from tests.utils import VCR, SillyAuth, mock_awair_device, mock_awair_user, time_travel
async def test_get_user() -> Any:
"""Test that we can get a user response."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("user.yaml"):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
user = await awair.user()
assert user.user_id == "32406"
assert user.email == "foo@bar.com"
assert user.first_name == "Andrew"
assert user.dob == date(year=2020, month=4, day=8)
assert user.tier == "Large_developer"
assert user.permissions["FIFTEEN_MIN"] == 30000
assert user.usages["USER_INFO"] == 80
assert "<AwairUser" in str(user)
async def test_custom_auth() -> Any:
"""Test that we can use the API with a custom auth class."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("custom_auth.yaml"):
auth = SillyAuth(access_token=ACCESS_TOKEN)
awair = Awair(session=session, authenticator=auth)
user = await awair.user()
assert user.user_id == "32406"
async def test_get_devices() -> Any:
"""Test that we can get a list of devices."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("devices.yaml"):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
user = mock_awair_user(client=awair.client)
devices = await user.devices()
assert devices[0].device_id == AWAIR_GEN1_ID
assert devices[0].device_type == "awair"
assert devices[0].uuid == f"awair_{AWAIR_GEN1_ID}"
assert "<AwairDevice" in str(devices[0])
async def test_get_local_devices() -> Any:
"""Test that we can get a list of devices."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("local_devices.yaml"):
awair = AwairLocal(
session=session,
device_addrs=["AWAIR-ELEM-1416DC.local", "AWAIR-ELEM-1419E1.local"],
)
devices = await awair.devices()
assert len(devices) == 2
assert devices[0].device_id == MOCK_ELEMENT_DEVICE_A_ATTRS["deviceId"]
assert devices[0].device_type == MOCK_ELEMENT_DEVICE_A_ATTRS["deviceType"]
assert devices[0].uuid == MOCK_ELEMENT_DEVICE_A_ATTRS["deviceUUID"]
assert devices[0].fw_version == MOCK_ELEMENT_DEVICE_A_ATTRS["fw_version"]
assert "<AwairDevice" in str(devices[0])
assert devices[1].device_id == MOCK_ELEMENT_DEVICE_B_ATTRS["deviceId"]
assert devices[1].device_type == MOCK_ELEMENT_DEVICE_B_ATTRS["deviceType"]
assert devices[1].uuid == MOCK_ELEMENT_DEVICE_B_ATTRS["deviceUUID"]
assert devices[1].fw_version == MOCK_ELEMENT_DEVICE_B_ATTRS["fw_version"]
assert "<AwairDevice" in str(devices[1])
async def test_get_latest() -> Any:
"""Test that we can get the latest air data."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("latest.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
resp = await device.air_data_latest()
assert resp is not None
assert resp.timestamp == datetime(2020, 4, 10, 15, 38, 24, 111000)
assert resp.score == 88.0
assert resp.sensors["temperature"] == 21.770000457763672
assert resp.indices["temperature"] == -1.0
assert "<AirData@2020-04-10" in str(resp)
async def test_get_latest_local() -> Any:
"""Test that we can get the latest air data."""
target = datetime(2020, 8, 31, 22, 7, 3)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("latest_local.yaml"), time_travel(target):
awair = AwairLocal(
session=session, device_addrs=["AWAIR-ELEM-1419E1.local"]
)
devices = await awair.devices()
assert len(devices) == 1
device = devices[0]
resp = await device.air_data_latest()
assert resp is not None
assert resp.timestamp == datetime(2020, 8, 31, 22, 7, 3, 831000)
assert resp.score == 93
assert resp.sensors["temperature"] == 19.59
assert len(resp.indices) == 0
assert "<AirData@2020-08-31" in str(resp)
async def test_get_five_minute() -> Any:
"""Test that we can get the five-minute avg air data."""
target = datetime(2020, 4, 10, 10, 38, 31, 2883)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("five_minute.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
resp = await device.air_data_five_minute(
from_date=(target - timedelta(minutes=30))
)
assert resp[0].timestamp == datetime(2020, 4, 10, 15, 35)
assert resp[0].score == 88.0
assert resp[0].sensors["temperature"] == 21.777143478393555
assert resp[0].indices["temperature"] == -1.0
async def test_get_fifteen_minute() -> Any:
"""Test that we can get the fifteen-minute avg air data."""
target = datetime(2020, 4, 10, 10, 38, 31, 252873)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("fifteen_minute.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
resp = await device.air_data_fifteen_minute(
from_date=(target - timedelta(minutes=30))
)
assert resp[0].timestamp == datetime(2020, 4, 10, 15, 30)
assert resp[0].score == 88.0
assert resp[0].sensors["temperature"] == 21.791961108936984
assert resp[0].indices["temperature"] == -1.0
async def test_get_raw() -> Any:
"""Test that we can get the raw air data."""
target = datetime(2020, 4, 10, 10, 38, 31, 720296)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("raw.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
resp = await device.air_data_raw(from_date=(target - timedelta(minutes=30)))
assert resp[0].timestamp == datetime(2020, 4, 10, 15, 38, 24, 111000)
assert resp[0].score == 88.0
assert resp[0].sensors["temperature"] == 21.770000457763672
assert resp[0].indices["temperature"] == -1.0
async def test_sensor_creation_gen1() -> Any:
"""Test that an Awair gen 1 creates expected sensors."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("latest.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
resp = await device.air_data_latest()
assert hasattr(resp, "timestamp")
assert hasattr(resp, "score")
assert hasattr(resp, "sensors")
assert hasattr(resp, "indices")
expected_sensors = [
"humidity",
"temperature",
"carbon_dioxide",
"volatile_organic_compounds",
"dust",
]
assert resp is not None
assert len(resp.sensors) == len(expected_sensors)
assert len(resp.indices) == len(expected_sensors)
for sensor in expected_sensors:
assert hasattr(resp.sensors, sensor)
assert hasattr(resp.indices, sensor)
assert device.model == "Awair"
async def test_sensor_creation_omni() -> Any:
"""Test that an Awair omni creates expected sensors."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("omni.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(
client=awair.client, device=MOCK_OMNI_DEVICE_ATTRS
)
resp = await device.air_data_latest()
assert hasattr(resp, "timestamp")
assert hasattr(resp, "score")
assert hasattr(resp, "sensors")
assert hasattr(resp, "indices")
expected_sensors = [
"humidity",
"temperature",
"carbon_dioxide",
"volatile_organic_compounds",
"particulate_matter_2_5",
"illuminance",
"sound_pressure_level",
]
assert resp is not None
assert len(resp.sensors) == len(expected_sensors)
for sensor in expected_sensors:
assert hasattr(resp.sensors, sensor)
expected_indices = [
"humidity",
"temperature",
"carbon_dioxide",
"volatile_organic_compounds",
"particulate_matter_2_5",
]
assert len(resp.indices) == len(expected_indices)
for sensor in expected_indices:
assert hasattr(resp.indices, sensor)
async def test_sensor_creation_mint() -> Any:
"""Test that an Awair mint creates expected sensors."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("mint.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(
client=awair.client, device=MOCK_MINT_DEVICE_ATTRS
)
resp = await device.air_data_latest()
assert hasattr(resp, "timestamp")
assert hasattr(resp, "score")
assert hasattr(resp, "sensors")
assert hasattr(resp, "indices")
expected_sensors = [
"humidity",
"temperature",
"volatile_organic_compounds",
"particulate_matter_2_5",
"illuminance",
]
assert resp is not None
assert len(resp.sensors) == len(expected_sensors)
for sensor in expected_sensors:
assert hasattr(resp.sensors, sensor)
expected_indices = [
"humidity",
"temperature",
"volatile_organic_compounds",
"particulate_matter_2_5",
]
assert len(resp.indices) == len(expected_indices)
for sensor in expected_indices:
assert hasattr(resp.indices, sensor)
async def test_sensor_creation_gen2() -> Any:
"""Test that an Awair gen2 creates expected sensors."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("awair-r2.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(
client=awair.client, device=MOCK_GEN2_DEVICE_ATTRS
)
resp = await device.air_data_latest()
assert hasattr(resp, "timestamp")
assert hasattr(resp, "score")
assert hasattr(resp, "sensors")
assert hasattr(resp, "indices")
expected_sensors = [
"humidity",
"temperature",
"volatile_organic_compounds",
"particulate_matter_2_5",
"carbon_dioxide",
]
assert resp is not None
assert len(resp.sensors) == len(expected_sensors)
for sensor in expected_sensors:
assert hasattr(resp.sensors, sensor)
expected_indices = [
"humidity",
"temperature",
"volatile_organic_compounds",
"particulate_matter_2_5",
"carbon_dioxide",
]
assert len(resp.indices) == len(expected_indices)
for sensor in expected_indices:
assert hasattr(resp.indices, sensor)
async def test_sensor_creation_glow() -> Any:
"""Test that an Awair glow creates expected sensors."""
target = datetime(2020, 4, 10, 10, 38, 30)
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("glow.yaml"), time_travel(target):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(
client=awair.client, device=MOCK_GLOW_DEVICE_ATTRS
)
resp = await device.air_data_latest()
assert hasattr(resp, "timestamp")
assert hasattr(resp, "score")
assert hasattr(resp, "sensors")
assert hasattr(resp, "indices")
expected_sensors = [
"humidity",
"temperature",
"volatile_organic_compounds",
"carbon_dioxide",
]
assert resp is not None
assert len(resp.sensors) == len(expected_sensors)
for sensor in expected_sensors:
assert hasattr(resp.sensors, sensor)
expected_indices = [
"humidity",
"temperature",
"volatile_organic_compounds",
"carbon_dioxide",
]
assert len(resp.indices) == len(expected_indices)
for sensor in expected_indices:
assert hasattr(resp.indices, sensor)
assert "Indices(" in str(resp.indices)
assert "Sensors(" in str(resp.sensors)
async def test_auth_failure() -> Any:
"""Test that we can raise on bad auth."""
async with aiohttp.ClientSession() as session:
with pytest.raises(AwairError):
Awair(session=session)
with VCR.use_cassette("bad_auth.yaml"):
awair = Awair(session=session, access_token="bad")
with pytest.raises(AuthError):
await awair.user()
async def test_bad_query() -> Any:
"""Test that we can raise on bad query."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("bad_params.yaml"):
with patch(
"python_awair.devices.AwairDevice._format_args",
return_value="?fahrenheit=451",
):
with pytest.raises(QueryError):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
await device.air_data_latest()
async def test_not_found() -> Any:
"""Test that we can raise on 404."""
async with aiohttp.ClientSession() as session:
with VCR.use_cassette("not_found.yaml"):
with patch("python_awair.const.DEVICE_URL", f"{const.USER_URL}/devicesxyz"):
with pytest.raises(NotFoundError):
awair = Awair(session=session, access_token=ACCESS_TOKEN)
user = mock_awair_user(client=awair.client)
await user.devices()
async def test_air_data_handles_boolean_attributes() -> Any:
"""Test that we handle boolean query attributes."""
async with aiohttp.ClientSession() as session:
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
with pytest.raises(vol.Invalid):
await device.air_data_raw(desc=None)
with pytest.raises(vol.Invalid):
await device.air_data_raw(fahrenheit=1)
async def test_air_data_handles_numeric_limits() -> Any:
"""Test that we handle numeric query attributes."""
async with aiohttp.ClientSession() as session:
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
with pytest.raises(vol.Invalid):
await device.air_data_raw(limit=-1)
with pytest.raises(vol.Invalid):
await device.air_data_raw(limit=361)
with pytest.raises(vol.Invalid):
await device.air_data_five_minute(limit=289)
with pytest.raises(vol.Invalid):
await device.air_data_fifteen_minute(limit=673)
async def test_air_data_handles_datetime_limits() -> Any:
"""Test that we handle date limits."""
async with aiohttp.ClientSession() as session:
awair = Awair(session=session, access_token=ACCESS_TOKEN)
device = mock_awair_device(client=awair.client)
now = datetime.now()
with pytest.raises(vol.Invalid):
await device.air_data_raw(from_date=(now + timedelta(hours=1)))
with pytest.raises(vol.Invalid):
await device.air_data_raw(from_date=False)
with pytest.raises(vol.Invalid):
await device.air_data_raw(from_date=(now - timedelta(hours=2)))
with pytest.raises(vol.Invalid):
await device.air_data_five_minute(from_date=(now - timedelta(hours=25)))
with pytest.raises(vol.Invalid):
await device.air_data_fifteen_minute(from_date=(now - timedelta(days=8)))
with pytest.raises(vol.Invalid):
await device.air_data_fifteen_minute(
from_date=(now - timedelta(hours=1)), to_date=(now - timedelta(hours=3))
)
def test_attrdict() -> Any:
"""Test a few AttrDict properties."""
comp = AttrDict({"foo": "bar", "humid": 123})
with pytest.raises(AttributeError):
print(comp.nope)
with pytest.raises(AttributeError):
print(comp.humid)
assert comp.humidity == 123
comp["nope"] = "hi"
assert comp.nope == "hi"
comp.nope = "hello"
assert comp.nope == "hello"
del comp["nope"]
del comp.humidity
assert "foo" in dir(comp)
assert "nope" not in dir(comp)
assert "humid" not in dir(comp)
assert "humidity" not in dir(comp)
|