-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
33 lines (24 loc) · 969 Bytes
/
api.py
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
import requests
class HaApi:
def __init__(self, url: str, token: str, timeout: float = 1.0, logging = False):
self.base_url = url
self.token = token
self.timeout = timeout
self.logging = logging
self.headers = {
"Authorization": f'Bearer {token}',
"content-type": "application/json",
}
def log(self, *args):
if self.logging: print(*args)
def turn_on(self, data: object, id = None):
url = self.base_url + '/services/light/turn_on'
try:
response = requests.post(url, json=data, headers=self.headers, timeout=self.timeout)
if not response.ok:
error_message = response.json()
raise ValueError(f'HTTP Error: {error_message}')
self.log(id, "Sent", data)
return response.json()
except requests.exceptions.ReadTimeout:
self.log(id, "Timed out")