上次用Appdaemon做了个通用定时器,想了下其实HA实现比较合适,毕竟不涉及复杂的场景判断。最近因为各种踩坑,读了HA的不少代码也算有一定的积累了,正好借这个机会练练手。
0.功能说明
![[效果图1]](/articles/plugin-home-assistant-universal-timer/demo1.gif)
![[效果图2]](/articles/plugin-home-assistant-universal-timer/demo2.gif)
- 支持多个定时任务
- 可定义开/关操作
- 自动加载设备列表,无需额外配置
- 采用看起来很厉害的环形定时队列
- 可记忆设备上次设置的定时时间
WARN:新手插件,可能存在未知的bug,使用本插件有风险!
1.环境
- Ubuntu 18.04 + HA 0.74.0
2.过程
2.1配置项
- configuration.yaml
1
2#{HA配置目录}/configuration.yaml
common_timer:
__INFO:目前代码内置固定配置,以后再分离做可配置的。初始化会增加以下5个entity,请确保名称不会冲突。
1.input_select.domain: 用于选择设备类型
2.input_select.entity: 设备
3.input_select.operation: 操作类型
4.input_text.common_timer: 输入定时
5.input_boolean.timer_button:开关按钮__
- group.yaml(根据实际情况配置)
1 | #{HA配置目录}/group.yaml |
__INFO:插件会生成一个group.common_timer,如果自定义过group.yaml的分组分页,需要配置放到要显示的分页里。__
2.2代码
input_select.py
1
2
3
4
5
6#{HA安装目录}/components/input_select.py
#……省略……#
def async_setup(hass, config):
"""Set up an input select."""
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#input_text.py
1
2
3
4
5
6#{HA安装目录}/components/input_text.py
#……省略……#
def async_setup(hass, config):
"""Set up an input text box."""
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#input_boolean.py
1
2
3
4
5
6#{HA安装目录}/components/input_boolean.py
#……省略……#
async def async_setup(hass, config):
"""Set up an input boolean."""
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#
__INFO:把官方的几个input_xxx component修改一下,以便保存EntityComponent到全局变量hass.data[DOMAIN],后续可能需要调用。__
- common_timer.py(github下载)
1
#放到{HA配置目录}/components/common_timer.py
INFO:测试版本,代码风格小随意。。。
3.小结
- 难点1,往HA中动态添加ENTITY的方法,需要了解HA的启动过程,以免影响加载原来配置文件。
- 难点2,HA定时调用方法,试用track_time_interval貌似不准,摸索找到的async_track_time_change就好用。
- 难点3,环形定时队列轮询和执行任务分线程运行,以免影响环形定时队列轮询准确性。HA的异步调度机制仍然是个黑洞,不断调式、参考自带的代码摸索出一定的规律,算是勉强能用了。