本帖最后由 afeifly 于 2019-3-20 12:04 编辑
看了有朋友用小米网关报时给网关上传文件 刚刚做了类似的东西有点忍不住
网关不是早就可以TTS了吗 语音应该是自由的
我已经实现了开门问候 忘记关门提醒 自动报时 闲着没事调戏家里猫儿狗儿等等
第一步网关配合HASS TTS
网关配置类似 这样
xiaomi_aqara:
gateways:
- key: XXXXXXXXXXXXXXX
host: 192.168.1.XX
miio_acpartner:
- platform: xiaomi_miio
name: "XMGW"
host: 192.168.1.103
token: "XXXXXXXXXXXXXXXXXXXXX"
api_key: "XXXXXXXXXXXXXXXXXXXXXXXX"
secret_key: "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
base_url: "http://192.168.1.XX:8123"
base_path: "/home/pi/.homeassistant"
notify: true
speed: 5
pitch: 5
volume: 5
person: 0
然后 homeassistant中就能测试这个服务了
再做了一个React界面集成在hass里简化发送TTS的过程 因为有块小OLED屏 可选这段输入要不要顺便显示到OLED上
第二步 准备2个python程序
报时Python程序 timereport.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import restclient
import datetime
import time
def getCurrentTime():
localtime = time.localtime()
theTime = time.strftime("%-H", localtime)
int_Time = int(theTime)
if int_Time<=12 :
theTime = "上午"+str(int_Time)
elif int_Time>12 and int_Time<19 :
theTime = "下午"+str(int_Time-12)
else :
theTime = "晚上"+str(int_Time-12)
return theTime;
if __name__ == '__main__':
msg = '滴---滴!!!! 现在时刻'+getCurrentTime()+'点整!'
restclient.send(msg)
向HASS提交Rest请求的 restclient.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import time
import requests
import json
from requests import post
from datetime import datetime
def speak(msg):
task = {
'entity_id' : 'miio_acpartner.xmgw',
'message' : msg
}
print(json.dumps(task))
headers = {
'content-type': 'application/json',
'Authorization':'Bearer ------------------------------------------------------'}
url = "http://HASSIP:8123/api/services/miio_acpartner/play_tts"
response = post(url,headers=headers,data=json.dumps(task))
print(response.text)
def send(msg):
speak(msg)
if __name__ == "__main__":
try:
send("作业做完了没呀?")
except KeyboardInterrupt: # ctrl+c退出
pass
注意 'Authorization':'Bearer ------------------
填自己的HASS认证
第三步 crontab定时运行python代码
0 7,8,12,14,15,16,17,18,19,20,21,22 * * * python /home/pi/tools/timereport.py
怕吵 只选择了几个时间点报时
思路是跳跃的 代码也不一定完整 因为是从很多代码里面抠出来的. 随便玩玩 高手不要笑了
有问题尽量回复
|