igloohome-api 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Library for accessing igloohome API"""
|
|
2
|
+
from typing import Any
|
|
3
|
+
from dacite import from_dict
|
|
4
|
+
|
|
5
|
+
import aiohttp
|
|
6
|
+
import jwt
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
_OAUTH2_HOST = "https://auth.igloohome.co"
|
|
10
|
+
_OAUTH2_TOKEN_PATH = "/oauth2/token"
|
|
11
|
+
_OAUTH2_SCOPE_EVERYTHING = OAUTH2_SCOPE = "igloohomeapi/algopin-hourly igloohomeapi/algopin-daily igloohomeapi/algopin-permanent igloohomeapi/algopin-onetime igloohomeapi/create-pin-bridge-proxied-job igloohomeapi/delete-pin-bridge-proxied-job igloohomeapi/lock-bridge-proxied-job igloohomeapi/unlock-bridge-proxied-job igloohomeapi/get-devices igloohomeapi/get-job-status igloohomeapi/get-properties"
|
|
12
|
+
|
|
13
|
+
_BASE_URL = "https://api.igloodeveloper.co"
|
|
14
|
+
_BASE_PATH = "igloohome"
|
|
15
|
+
_DEVICES_PATH_SEGMENT = "devices"
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class GetDevicesResponsePayload:
|
|
19
|
+
id: str
|
|
20
|
+
type: str
|
|
21
|
+
deviceId: str
|
|
22
|
+
deviceName: str
|
|
23
|
+
pairedAt: str
|
|
24
|
+
homeId: Any
|
|
25
|
+
linkedDevices: Any
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class GetDevicesResponse:
|
|
30
|
+
nextCursor: str
|
|
31
|
+
payload: list[GetDevicesResponsePayload]
|
|
32
|
+
|
|
33
|
+
class Auth:
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
session: aiohttp.ClientSession,
|
|
37
|
+
client_id: str,
|
|
38
|
+
client_secret: str,
|
|
39
|
+
host: str = _OAUTH2_HOST,
|
|
40
|
+
scope: str = _OAUTH2_SCOPE_EVERYTHING,
|
|
41
|
+
) -> None:
|
|
42
|
+
self.access_token = None
|
|
43
|
+
self.session = session
|
|
44
|
+
self.client_id = client_id
|
|
45
|
+
self.client_secret = client_secret
|
|
46
|
+
self.host = host
|
|
47
|
+
self.scope = scope
|
|
48
|
+
|
|
49
|
+
async def async_get_access_token(self) -> str:
|
|
50
|
+
form = aiohttp.FormData()
|
|
51
|
+
form.add_field("grant_type", "client_credentials")
|
|
52
|
+
form.add_field("scope", self.scope)
|
|
53
|
+
response = await self.session.post(
|
|
54
|
+
url=self.host + _OAUTH2_TOKEN_PATH,
|
|
55
|
+
auth=aiohttp.BasicAuth(
|
|
56
|
+
login=self.client_id, password=self.client_secret
|
|
57
|
+
),
|
|
58
|
+
data=form,
|
|
59
|
+
)
|
|
60
|
+
json = await response.json()
|
|
61
|
+
if response.status == 200:
|
|
62
|
+
self.access_token = json["access_token"]
|
|
63
|
+
return self.access_token
|
|
64
|
+
else:
|
|
65
|
+
raise AuthException(f'Failed to get access token. responseCode=${response.status}')
|
|
66
|
+
|
|
67
|
+
async def async_get_valid_access_token(self) -> str:
|
|
68
|
+
"""Gets a valid access token."""
|
|
69
|
+
if self.access_token is None:
|
|
70
|
+
access_token = await self.async_get_access_token()
|
|
71
|
+
return access_token
|
|
72
|
+
elif is_access_token_valid(self.access_token):
|
|
73
|
+
return self.access_token
|
|
74
|
+
else:
|
|
75
|
+
return await self.async_get_access_token()
|
|
76
|
+
|
|
77
|
+
async def request(self, method: str, url: str, **kwargs) -> aiohttp.ClientResponse:
|
|
78
|
+
"""Make a request."""
|
|
79
|
+
headers = kwargs.get("headers")
|
|
80
|
+
|
|
81
|
+
if headers is None:
|
|
82
|
+
headers = {}
|
|
83
|
+
else:
|
|
84
|
+
headers = dict(headers)
|
|
85
|
+
|
|
86
|
+
headers["Authorization"] = f"Bearer {await self.async_get_valid_access_token()}"
|
|
87
|
+
headers["Accept"] = "application/json"
|
|
88
|
+
|
|
89
|
+
return await self.session.request(
|
|
90
|
+
method, url, **kwargs, headers=headers,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def is_access_token_valid(access_token: str) -> bool:
|
|
95
|
+
"""Check if the access token is valid."""
|
|
96
|
+
try:
|
|
97
|
+
# Expiry is automatically verified during decoding. Will raise ExpiredSignatureError.
|
|
98
|
+
# See: https://pyjwt.readthedocs.io/en/stable/usage.html#expiration-time-claim-exp
|
|
99
|
+
claims = jwt.decode(access_token, options={"require": ["exp"]})
|
|
100
|
+
except Exception:
|
|
101
|
+
return False
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class AuthException(Exception):
|
|
105
|
+
pass
|
|
106
|
+
|
|
107
|
+
class Api:
|
|
108
|
+
def __init__(
|
|
109
|
+
self,
|
|
110
|
+
auth: Auth,
|
|
111
|
+
host: str = _BASE_URL
|
|
112
|
+
):
|
|
113
|
+
self.auth = auth
|
|
114
|
+
self.host = host
|
|
115
|
+
|
|
116
|
+
async def get_devices(self) -> GetDevicesResponse:
|
|
117
|
+
response = await self.auth.request(
|
|
118
|
+
"get",
|
|
119
|
+
f'{self.host}/{_BASE_PATH}/{_DEVICES_PATH_SEGMENT}',
|
|
120
|
+
)
|
|
121
|
+
if response.status == 200:
|
|
122
|
+
return from_dict(GetDevicesResponse, await response.json())
|
|
123
|
+
else:
|
|
124
|
+
raise ApiException("Response failure", response.status)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class ApiException(Exception):
|
|
128
|
+
def __init__(self, message: str, response_code: int):
|
|
129
|
+
self.message = message
|
|
130
|
+
self.response_code = response_code
|
|
131
|
+
|
|
132
|
+
def __str__(self):
|
|
133
|
+
return f'ApiException(message={self.message}, response_code={self.response_code})'
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
async def _create_exception(response: aiohttp.ClientResponse) -> ApiException:
|
|
137
|
+
return ApiException(
|
|
138
|
+
message=f'Unsuccessful request. code={response.status}, message={await response.text()}',
|
|
139
|
+
response_code=response.status
|
|
140
|
+
)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: igloohome-api
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: A python package for iglooaccess' API
|
|
5
|
+
Project-URL: Homepage, https://github.com/keithle888/igloohome-api
|
|
6
|
+
Project-URL: Issues, https://github.com/keithle888/igloohome-api/issues
|
|
7
|
+
Author-email: keithle888 <keithle888@gmail.com>
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Requires-Dist: aiohttp~=3.10.10
|
|
13
|
+
Requires-Dist: dacite~=1.8.1
|
|
14
|
+
Requires-Dist: pyjwt~=2.9.0
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# igloohome-api
|
|
18
|
+
A python HTTP library based on `aiohttp` to make use of [igloohome's REST API](https://igloocompany.stoplight.io/docs/igloohome-api/1w1cuv56ge5xq-overview).
|
|
19
|
+
This library is designed to be used via the [iglooaccess](https://www.igloocompany.co/iglooaccess) service.
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
An account on the iglooaccess portal needs to be created to get a `client_id` & `client_secret` for authentication.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Authentication
|
|
27
|
+
```python
|
|
28
|
+
from igloohome_api import Auth
|
|
29
|
+
from aiohttp import ClientSession
|
|
30
|
+
|
|
31
|
+
session = ClientSession()
|
|
32
|
+
auth = Auth(
|
|
33
|
+
client_id="<client_id>",
|
|
34
|
+
client_secret="<client_secret>",
|
|
35
|
+
session=session,
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### API usage
|
|
40
|
+
```python
|
|
41
|
+
frim igloohome_api import Api
|
|
42
|
+
|
|
43
|
+
api = Api(auth)
|
|
44
|
+
|
|
45
|
+
devices = await api.get_devices()
|
|
46
|
+
```
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
igloohome_api/__init__.py,sha256=ISCm3BWpjnvOujkTleoqMEI_7CQzsUtNDon0R9_MKZk,4593
|
|
2
|
+
igloohome_api-0.0.3.dist-info/METADATA,sha256=BTLpLYNKiJW2oo2TVUnKd3JSviUCMxc997R9cWVqjos,1353
|
|
3
|
+
igloohome_api-0.0.3.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
4
|
+
igloohome_api-0.0.3.dist-info/licenses/LICENSE,sha256=Oz9BF-ql3skRL4OjnpK0WsBlvC1Y4AmKL9Kq_HbELZM,1066
|
|
5
|
+
igloohome_api-0.0.3.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Keith Leow
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|