-
Notifications
You must be signed in to change notification settings - Fork 1
/
hass.py
101 lines (81 loc) · 2.58 KB
/
hass.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
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
import requests
import config
access_token = config.hass_access_token
hass_url = config.hass_url
def getEntities():
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
response = requests.get(f"{hass_url}/api/states", headers=headers)
if response.status_code == 200:
all_entities = response.json()
return all_entities
else:
print(f"Error: {response.status_code}")
return None
def getEntitiesFromDomain(domain):
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
response = requests.get(f"{hass_url}/api/states", headers=headers)
if response.status_code == 200:
all_entities = response.json()
domain_entities = [
entity['entity_id']
for entity in all_entities
if entity['entity_id'].startswith(f"{domain}.")
]
return domain_entities
else:
print(f"Error: {response.status_code}")
return None
def lightTurnOn(entity):
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
payload = {
"entity_id": entity
}
url = f"{hass_url}/api/services/light/turn_on"
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return True
except requests.exceptions.RequestException as e:
print(f"Error turning on light: {e}")
return False
def lightTurnOff(entity):
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
payload = {
"entity_id": entity
}
url = f"{hass_url}/api/services/light/turn_off"
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return True
except requests.exceptions.RequestException as e:
print(f"Error turning off light: {e}")
return False
def sceneActivate(entity):
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
payload = {
"entity_id": entity
}
url = f"{hass_url}/api/services/scene/turn_on"
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return True
except requests.exceptions.RequestException as e:
print(f"Error activating scene: {e}")
return False