本帖最后由 blindlight 于 2018-2-22 18:27 编辑
首先,介于很多人对appdaemon的误解,这里重新介绍下appdaemon。
AppDaemon is a loosely coupled, multithreaded, sandboxed python execution environment for writing automation apps for Home Assistant home automation software. It also provides a configurable dashboard (HADashboard) suitable for wall mounted tablets.
我就翻第一句:Appdaemon是一个松耦合,多线程,沙盒的python执行环境,为的是给Home Assistant写自动化 自动化 自动化 程序。
HADashboard是通过appdaemon实现的一个前端。
ha本身有自动化那干嘛费这劲开发出这个平台呢,因为原生用yaml和jinjia2的那套东西对于有点编程基础的人来说那就是一坨屎。
简单的ifttt能应付,触发情况复杂的时候condition满天飞,再加上template那惊人的可读性。。。。
吐槽到此为止,下面是正文:
本文实现的内容,是女神和大神在把miio和空调伴侣插件完善之前的过度方案,而且和自动化关系不大,旨在展示ad平台的强大功能。
硬件:
1. 空调伴侣
2. 4进2出的HDMI矩阵 可红外控制(共8个绝对信号)
input_select:
hdmi_output_a:
name: 屏幕信号
icon: mdi:monitor
options:
- PC
- VTEN
- PS4
- Xbox One
hdmi_output_b:
name: 投影信号
icon: mdi:projector
options:
- PC
- VTEN
- PS4
- Xbox One
复制代码
首先加两个选择器作为设备输出
然后在ad的apps文件夹下创建HDMI_Matrix.py,其中的红外码获取见附件。
import appdaemon.appapi as appapi
import miio
import codecs
ip = '10.0.0.148'
Container = miio.Device.discover(ip)
token = str(codecs.encode(Container.checksum, 'hex'), 'utf-8')
AC_Partner = miio.Device(ip, token)
class HDMImatrix(appapi.AppDaemon):
def initialize(self):
self.listen_state(self.select_input, "input_select.hdmi_output_a")
self.listen_state(self.select_input, "input_select.hdmi_output_b")
def select_input(self, entity, attribute, old, new, kwargs):
output_a_state = self.get_state("input_select.hdmi_output_a")
output_b_state = self.get_state("input_select.hdmi_output_b")
HDMI_Matrix = {
"A1": "FE00000000000094701fff9Aff080026270E003600AB00E401C603810FAA138825A843000000000000000001010101010101010000010101000100010100000001000105420742061A",
"A2": "FE00000000000094701fff9Bff0800282711003600AB00E401C503800FA61388259C4300000000000000000101010101010101000101010100000001000000000101010542074207420651",
"A3": "FE00000000000094701FFF9dFF0800262711003600AB00E301C503800FA2138825954300000000000000000101010101010101000000010100010001010100000100010542074206FC",
"A4": "FE00000000000094701FFF9eFF0800262712003600AA00E401C503800FA51388259A430000000000000000010101010101010100010100010000000100000100010101054207420604",
"B1": "FE00000000000094701fff96ff080026270A003700AB00E501C303820FA6138825A043000000000000000001010101010101010100000101000100000101000001000105420742060E",
"B2": "FE00000000000094701fff97ff080026270B003600AB00E501C603810FAA138825A843000000000000000001010101010101010000000101000000010101000001010105420742061B",
"B3": "FE00000000000094701fff98ff080026270C003600AB00E401C503800FA51388259C430000000000000000010101010101010101000101010001000001000000010001054207420607",
"B4": "FE00000000000094701fff99ff080026270D003600AB00E401C503800FA51388259D430000000000000000010101010101010100000101010000000101000000010101054207420608"
}
if output_a_state == 'PC':
ircode = HDMI_Matrix['A1']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("A1 Sent!")
elif output_a_state == 'VTEN':
ircode = HDMI_Matrix['A2']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("A2 Sent!")
elif output_a_state == 'PS4':
ircode = HDMI_Matrix['A3']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("A3 Sent!")
elif output_a_state == 'Xbox One':
ircode = HDMI_Matrix['A4']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("A4 Sent!")
if output_b_state == 'PC':
ircode = HDMI_Matrix['B1']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("B1 Sent!")
elif output_b_state == 'VTEN':
ircode = HDMI_Matrix['B2']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("B2 Sent!")
elif output_b_state == 'PS4':
ircode = HDMI_Matrix['B3']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("B3 Sent!")
elif output_b_state == 'Xbox One':
ircode = HDMI_Matrix['B4']
if AC_Partner.send('send_ir_code', [ircode]) == ['ok']:
self.log("B4 Sent!")
复制代码
最后在apps.yaml下加入这个app的modul和class,当然如果有变量可以在这里传递变量到app(具体参照ad的文档),所有操作和dashboard无异。
HDMI_Matrix:
module: HDMI_Matrix
class: HDMImatrix
复制代码
以上所有操作,除了添加选择器设备时候需要重启ha,其他不用做任何重载操作,包括今后对app的任何修改,ad会自动载入并应用。
本版内所有用yaml写的自动化都可以用appdaemon实现,越复杂的自动化ad的优势越大。
当然对于不懂python的人来说,前面的所有请无视。这里还是要提供下接地气的方式。
附件提供了一个脚本,2个json文件,是在空调伴侣插件出来之前我瞎弄的空伴脚本,其中空调控制部分已经不需要了。
红外的学习可以在bash命令行实现,ha接入红外的发送可以用command line方式,这样就不用appdaemon了,当然正道是等完整插件。
使用方式在domoticz论坛我已经写过这里就不再赘述。