From d1a34bcb2b970463b3b949b0097989f9e2aec726 Mon Sep 17 00:00:00 2001 From: Deschanel <201400150159@mail.sdu.edu.cn> Date: Wed, 13 Sep 2017 23:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EHomeassistant=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=20(#42)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加homeassistant,当前只是测试阶段,目前只添加了sensor和switch功能 * Delete Hass.py * 增加HomeAssistant * 更改缩进 * Delete Hass.py * 新增Homeassistant * Delete Hass.py * 新增Homeassistant * Update Hass.py * Delete Hass.py * 新增Homeassistant * Delete Hass.py * 新增Homeassistant * 新的更改 * 新的更改 * 新的更改 --- client/plugins/Hass.py | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 client/plugins/Hass.py diff --git a/client/plugins/Hass.py b/client/plugins/Hass.py new file mode 100644 index 0000000..aaa11c3 --- /dev/null +++ b/client/plugins/Hass.py @@ -0,0 +1,89 @@ +# -*- coding:utf-8 -*- +import requests +import json +import logging +import sys +reload(sys) +sys.setdefaultencoding('utf8') + +WORDS = ["JIATINGZHUSHOU", "ZHUSHOU"] +SLUG = "homeassistant" + + +def handle(text, mic, profile, wxbot=None): + mic.say(u"开始家庭助手控制") + mic.say(u'请在滴一声后说明内容') + input = mic.activeListen(MUSIC=True) + while not input: + mic.say(u"请重新说") + input = mic.activeListen(MUSIC=True) + hass(input, mic, profile) + + +def hass(text, mic, profile): + logger = logging.getLogger(__name__) + if profile[SLUG] or 'url' not in profile[SLUG] or \ + 'port' not in profile[SLUG] or \ + 'password' not in profile[SLUG]: + mic.say(u"主人配置有误") + return + url = profile[SLUG]['url'] + port = profile[SLUG]['port'] + password = profile[SLUG]['password'] + headers = {'x-ha-access': password, 'content-type': 'application/json'} + r = requests.get(url+":"+port+"/api/states", headers=headers) + r_jsons = r.json() + devices = [] + for r_json in r_jsons: + entity_id = r_json['entity_id'] + domain = entity_id.split(".")[0] + if domain not in ["group", "automation", "script"]: + url_entity = url + ":" + port + "/api/states/" + entity_id + entity = requests.get(url_entity, headers=headers).json() + devices.append(entity) + for device in devices: + name = device["attributes"]["friendly_name"] + state = device["state"] + if name in text: + device_domain = device["entity_id"].split(".")[0] + if device_domain == "sensor" or not isAction(text): + try: + measurement = device["attributes"]["unit_of_measurement"] + except Exception, e: + pass + if 'measurement' in locals().keys(): + text = text + "状态是" + state + measurement + mic.say(text) + else: + text = text + "状态是" + state + mic.say(text) + elif device_domain == "switch" and isAction(text): + try: + if any(word in text for word in [u"开始", u"打开", u"开启"]): + newS = "turn_on" + elif any(word in text for word in [u"停止", u"结束", u"退出"]): + newS = "turn_off" + p = json.dumps({"entity_id": device["entity_id"]}) + url_s = url + ":" + port + "/api/services/switch/" + newS + request = requests.post(url_s, headers=headers, data=p) + if format(request.status_code) == "200" or \ + format(request.status_code) == "201": + mic.say(u"执行成功") + else: + mic.say(u"对不起,执行失败") + print(format(request.status_code)) + except Exception, e: + pass + break + else: + mic.say(u"对不起,指令不存在") + + +def isAction(text): + return any(word in text for word in ["打开", "停止", "结束", "开始"]) + + +def isValid(text): + return any(word in text for word in [u"开启家庭助手", + u"开启助手", u"打开家庭助手", u"打开助手", + u"家庭助手"])