| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
from datetime import datetime, timedelta |
| 4 |
|
| 5 |
from ayla_iot_unofficial import ( |
| 6 |
AylaApi, |
| 7 |
AylaAuthError, |
| 8 |
AylaNotAuthedError, |
| 9 |
AylaReadOnlyPropertyError, |
| 10 |
new_ayla_api, |
| 11 |
) |
| 12 |
from ayla_iot_unofficial.device import Device |
| 13 |
|
| 14 |
|
| 15 |
def assert_raises(exc_type, func, *args, **kwargs): |
| 16 |
try: |
| 17 |
func(*args, **kwargs) |
| 18 |
except exc_type as exc: |
| 19 |
return exc |
| 20 |
raise AssertionError(f"{exc_type.__name__} was not raised") |
| 21 |
|
| 22 |
|
| 23 |
api = new_ayla_api( |
| 24 |
"[email protected]", |
| 25 |
"password", |
| 26 |
"application-id", |
| 27 |
"application-secret", |
| 28 |
) |
| 29 |
|
| 30 |
assert isinstance(api, AylaApi) |
| 31 |
assert api._email == "[email protected]" |
| 32 |
assert api._password == "password" |
| 33 |
assert api._app_id == "application-id" |
| 34 |
assert api._app_secret == "application-secret" |
| 35 |
assert api._access_token is None |
| 36 |
assert api._refresh_token is None |
| 37 |
assert api._auth_expiration is None |
| 38 |
assert api._is_authed is False |
| 39 |
assert api.websession is None |
| 40 |
|
| 41 |
api._set_credentials( |
| 42 |
200, |
| 43 |
{ |
| 44 |
"access_token": "access-token", |
| 45 |
"refresh_token": "refresh-token", |
| 46 |
"expires_in": 3600, |
| 47 |
}, |
| 48 |
) |
| 49 |
|
| 50 |
assert api._is_authed is True |
| 51 |
assert api._access_token == "access-token" |
| 52 |
assert api._refresh_token == "refresh-token" |
| 53 |
assert api.auth_expiration > datetime.now() + timedelta(seconds=3500) |
| 54 |
assert api.auth_header == {"Authorization": "auth_token access-token"} |
| 55 |
|
| 56 |
kwargs = {"headers": {"X-Test": "value"}} |
| 57 |
assert api._get_headers(kwargs) == { |
| 58 |
"X-Test": "value", |
| 59 |
"Authorization": "auth_token access-token", |
| 60 |
} |
| 61 |
assert kwargs == {} |
| 62 |
|
| 63 |
api._clear_auth() |
| 64 |
assert_raises(AylaNotAuthedError, lambda: api.auth_header) |
| 65 |
|
| 66 |
auth_exc = assert_raises( |
| 67 |
AylaAuthError, |
| 68 |
api._set_credentials, |
| 69 |
404, |
| 70 |
{"error": {"message": "Not found"}}, |
| 71 |
) |
| 72 |
assert "app_id and app_secret" in str(auth_exc) |
| 73 |
|
| 74 |
device = Device( |
| 75 |
api, |
| 76 |
{ |
| 77 |
"dsn": "dsn-123", |
| 78 |
"key": 123, |
| 79 |
"oem_model": "OEM-1", |
| 80 |
"model": "MODEL-1", |
| 81 |
"mac": "00:11:22:33:44:55", |
| 82 |
"lan_ip": "192.0.2.10", |
| 83 |
"product_name": "Test device", |
| 84 |
}, |
| 85 |
) |
| 86 |
|
| 87 |
assert device.name == "Test device" |
| 88 |
assert device.serial_number == "dsn-123" |
| 89 |
assert device.dsn_all_properties_endpoint.endswith("/apiv1/dsns/dsn-123/properties.json") |
| 90 |
assert device.set_property_endpoint("target") == ( |
| 91 |
"https://ads-field.aylanetworks.com/apiv1/dsns/dsn-123/properties/target/datapoints.json" |
| 92 |
) |
| 93 |
|
| 94 |
assert device._do_update( |
| 95 |
True, |
| 96 |
[ |
| 97 |
{ |
| 98 |
"property": { |
| 99 |
"name": "GET_status", |
| 100 |
"value": "1", |
| 101 |
"base_type": "integer", |
| 102 |
"read_only": True, |
| 103 |
"key": 1, |
| 104 |
} |
| 105 |
}, |
| 106 |
{ |
| 107 |
"property": { |
| 108 |
"name": "SET_target", |
| 109 |
"value": "21", |
| 110 |
"base_type": "integer", |
| 111 |
"read_only": False, |
| 112 |
"key": 2, |
| 113 |
} |
| 114 |
}, |
| 115 |
], |
| 116 |
) is True |
| 117 |
|
| 118 |
assert device.get_property_value("status") == 1 |
| 119 |
assert "target" in device._settable_properties |
| 120 |
assert_raises(AylaReadOnlyPropertyError, device.set_property_value, "status", 0) |