-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
72 lines (57 loc) · 2.51 KB
/
Copy pathapi_client.py
File metadata and controls
72 lines (57 loc) · 2.51 KB
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
"""API Client for Ajax Cloud Backend."""
from __future__ import annotations
import asyncio
import logging
from typing import Any
import aiohttp
from aiohttp import ClientSession, ClientTimeout
_LOGGER = logging.getLogger(__name__)
TIMEOUT = ClientTimeout(total=30)
class AjaxCloudClient:
"""Client to communicate with Ajax Cloud backend."""
def __init__(self, session: ClientSession, backend_url: str, token: str) -> None:
"""Initialize the client."""
self._session = session
self._backend_url = backend_url.rstrip("/")
self._token = token
async def _request(
self, method: str, endpoint: str, data: dict[str, Any] | None = None
) -> dict[str, Any]:
"""Make a request to the backend."""
url = f"{self._backend_url}/api/v1{endpoint}"
headers = {
"Authorization": f"Bearer {self._token}",
"Content-Type": "application/json",
}
try:
async with self._session.request(
method, url, json=data, headers=headers, timeout=TIMEOUT
) as response:
response.raise_for_status()
return await response.json()
except aiohttp.ClientError as err:
_LOGGER.error("Error communicating with backend: %s", err)
raise
except asyncio.TimeoutError as err:
_LOGGER.error("Timeout communicating with backend")
raise
async def async_authenticate(self, email: str) -> dict[str, Any]:
"""Request authentication/registration."""
return await self._request("POST", "/auth/register", {"email": email})
async def async_check_status(self) -> dict[str, Any]:
"""Check connection status and approval."""
return await self._request("GET", "/auth/status")
async def async_get_devices(self) -> dict[str, Any]:
"""Get all devices and their states."""
return await self._request("GET", "/devices")
async def async_get_device_state(self, device_id: str) -> dict[str, Any]:
"""Get specific device state."""
return await self._request("GET", f"/devices/{device_id}")
async def async_arm_alarm(self, hub_id: str, mode: str) -> dict[str, Any]:
"""Arm the alarm system."""
return await self._request(
"POST", f"/hubs/{hub_id}/arm", {"mode": mode}
)
async def async_disarm_alarm(self, hub_id: str) -> dict[str, Any]:
"""Disarm the alarm system."""
return await self._request("POST", f"/hubs/{hub_id}/disarm")