-
Notifications
You must be signed in to change notification settings - Fork 0
/
hass.py
161 lines (144 loc) · 5.5 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os, json
try:
from requests import get, post
except ModuleNotFoundError:
os.system("python -m pip install requests")
from requests import get, post
try:
import yaml
except ModuleNotFoundError:
os.system("python -m pip install PyYAML")
import yaml
dirname = os.path.dirname(__file__)
tokenpath = os.path.join(dirname, 'Token.txt')
hass_url = "http://homeassistant:8123"
with open(tokenpath, 'r') as tokenfile:
headers = {
"Authorization": "Bearer " + tokenfile.read(),
"content-type": "application/json",
}
def call_service(service, data={}):
"""Calls a Home Assistant Service (obviously).\nService: The service that should be called, in the form domain.service\nData: A dictionary of data\n"""
split_service = service.split(".")
domain = split_service[0]
service = split_service[1]
url = hass_url + "/api/services/" + domain + "/" + service
response = post(url, headers=headers, json=data)
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = "Success!"
elif code == 404:
message = "Not found. Wrong service?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def fire_event(event, data={}):
url = hass_url + "/api/events/" + event
response = post(url, headers=headers, json=data)
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = "Success!"
elif code == 404:
message = "Not found. Wrong event?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def render_template(template):
url = hass_url + "/api/template"
response = post(url, headers=headers, json={'template': template})
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = response.text
elif code == 404:
message = "Not found. Wrong Home Assistant URL?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def is_service(service):
split_service = service.split(".")
domain = split_service[0]
service = split_service[1]
domains = get(hass_url + "/api/services", headers=headers)
for d in json.loads(domains.text):
if d['domain'] == domain:
for s in d['services']:
if s == service:
return True
return False
def get_state(entity_id, print_result=None):
url = hass_url + "/api/states/" + entity_id
response = get(url, headers=headers)
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = json.loads(response.text)
if str(print_result).lower() == 'json' and print_result:
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4))
elif str(print_result).lower() == 'yaml' and print_result:
print(yaml.dump(json.loads(response.text), default_flow_style=False))
elif code == 404:
message = "Not found. Wrong entity ID?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def get_history(entities=None, start_time='', end_time='', minimal=False, significant_only=False, print_result=None):
url = hass_url + "/api/history/period" + ("/" if start_time else "") + start_time
data = {"filter_entity_id": entities, "end_time": end_time, "minimal_response": minimal, "significant_changes_only": significant_only}
if not entities:
print("This could take some time...")
response = get(url, headers=headers, json=data)
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = json.loads(response.text)
if str(print_result).lower() == 'json':
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4))
elif str(print_result).lower() == 'yaml':
print(yaml.dump(json.loads(response.text), default_flow_style=False))
elif code == 404:
message = "Not found. Wrong entity ID or time?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def get_logbook(entities=None, start_time='', end_time='', print_result=None):
url = hass_url + "/api/logbook" + ("/" if start_time else "") + start_time
data = {"filter_entity_id": entities, "end_time": end_time}
if not entities:
print("This could take some time...")
response = get(url, headers=headers, json=data)
code = int(response.status_code)
message = ""
if code in range(200, 300):
message = json.loads(response.text)
if str(print_result).lower() == 'json':
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4))
elif str(print_result).lower() == 'yaml':
print(yaml.dump(json.loads(response.text), default_flow_style=False))
elif code == 404:
message = "Not found. Wrong entity ID or time?"
elif code == 400:
message = "Bad request. Syntax error?"
return message
def set_hass_url(new_url):
global hass_url
hass_url = new_url
def yamlify(data):
if type(data) == str:
return yaml.dump(json.loads(data))
else:
return yaml.dump(data)
def jsonify(data):
if type(data) == str:
return json.dumps(yaml.load(data))
else:
return json.dumps(data)
def test(print_result=True):
url = hass_url+"/api/"
response = get(url, headers=headers)
if print_result:
print(response.text)
return response.text
#get_logbook(print_result='yaml')