forked from zhujisheng/Home-Assistant-DIY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ha_cli.py
56 lines (43 loc) · 1.63 KB
/
ha_cli.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
"""
This is the HomeAssistant Client for voice_assistant.
Only complete the service call and conversation api.
"""
import requests
class ha_cli(object):
def __init__(self, token, base_url='http://localhost:8123', verify=False):
assert base_url[-1] != '/'
self._base_url = base_url
self._session = requests.Session()
self._session.verify = verify
self._session.headers = {
'Authorization': 'Bearer '+ token,
'Content-Type': 'application/json'
}
def process(self, input_sentence):
data = {'text': input_sentence}
url = self._base_url + '/api/conversation/process'
r = self._session.post(url=url, json = data)
ouput_sentence = r.json()['speech']['plain']['speech']
return(ouput_sentence)
def call_service(self, domain, service, data=None):
url = self._base_url + '/api/services/' + domain + '/' + service
r = self._session.post(url = url, json = data)
return
def speak(self, sentence, tts='google_translate_say', media_player='all'):
data = { 'message': sentence,
'entity_id': media_player
}
self.call_service( 'tts',
tts,
data
)
return
def note(self, message='', title='不能理解的语音命令'):
data = { 'message': message,
'title': title
}
self.call_service( 'persistent_notification',
'create',
data
)
return