-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathHassApi.py
81 lines (70 loc) · 3.05 KB
/
HassApi.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
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import requests
import json
class HassApi:
""" Home Assistant REST API Calls """
def __init__(self, url, token):
self.url = url
self.token = token
def TriggerAutomation(self, name):
url = self.url + "/api/services/automation/trigger"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def TurnOnLight(self, name, color):
url = self.url + "/api/services/light/turn_on"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name, "color_name": color}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def PlayPlaylist(self, name, contentId):
url = self.url + "/api/services/media_player/play_media"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name, "media_content_type": "playlist", "media_content_id": contentId}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def PlaySong(self, name, contentId):
url = self.url + "/api/services/media_player/play_media"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name, "media_content_type": "music", "media_content_id": contentId}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def PauseMedia(self, name):
url = self.url + "/api/services/media_player/media_pause"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def PlayMedia(self, name):
url = self.url + "/api/services/media_player/media_play"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
def TurnOnSwitch(self, name):
url = self.url + "/api/services/switch/turn_on"
headers = {
'Authorization': "Bearer " + self.token,
}
payload = {"entity_id": name}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)