cnk700i 发表于 2018-8-16 23:38:22

通用型定时器【任务列表+周期任务+可服务调用】

本帖最后由 cnk700i 于 2019-8-23 09:09 编辑

计划增加计划任务功能(例如每天9点触发之类的),催更可在github上Star
update:2019-08-23
0.97.2测试,修复一些bug

update:2019-05-05
适配0.92.1

update:2018-09-21
增加了列表点击排行功能、保存任务信息功能(HA正常重启保存一次)。更新到github的master分支。
PS:增加了自定义设备周期任务时间比例的功能,不过前端貌似有点bug,当两个设备的时间比例不同,在切换的时候""经常是显示前一个的。。折腾一整天无果,不管了。。。

update:2018-08-28(0.78.0以后不需要改了)

如果有使用custom_ui,把{ha安装目录}/helpers/entity.py里,增加以下代码以保存旧extra_data_template属性,以免引起state changed事件导致周期任务失败。




```python
      old_state = self.hass.states.get(self.entity_id)
      if old_state:
            old_attr = old_state.as_dict().get('attributes',{})
            if 'extra_data_template' in old_attr.keys():
                attr['extra_data_template'] = old_attr['extra_data_template']
```


update:2018-08-23
增加了周期任务的功能:运行一段时间-停一段时间-运行一段时间这样的。另外增加了一些配置项,详细说明见github吧。
最终版,不考虑加新功能了。其实有打算做一个设备设置多个定时任务的,不过最近忙起来了,决定鸽掉了。。。


update:2018-08-20
已更新,还是那个连接到github下载master分支即可,旧版为v1分支。补一个配置说明图:
https://i.imgur.com/WmTJqoM.png
定时任务列表效果图:
https://i.imgur.com/YmYcuf4.gif




> 上次用Appdaemon做了个[通用定时器],想了下其实HA实现比较合适,毕竟不涉及复杂的场景判断。最近因为各种踩坑,读了HA的不少代码也算有一定的积累了,正好借这个机会练练手。

## 0.功能说明 ##
![效果图1](https://ljr.im/articles/plugin-home-assistant-general-timer/demo1.gif)    ![效果图2](https://ljr.im/articles/plugin-home-assistant-general-timer/demo2.gif)
- 支持多个定时任务
- 可定义开/关操作
- 自动加载设备列表,无需额外配置
- 采用看起来很厉害的环形定时队列
- 可记忆设备上次设置的定时时间

>__WARN:新手插件,可能存在未知的bug,使用本插件有风险!__


## 1.环境 ##
- Ubuntu 18.04 + HA 0.74.0

## 2.过程 ##
### 2.1配置项 ###

- configuration.yaml

```yaml
#{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:开关按钮__

### 2.2代码 ###
- input_select.py

```yaml
#{HA安装目录}/components/input_select.py
#……省略……#
def async_setup(hass, config):
    """Set up an input select."""
    component = hass.data = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#
```

- input_text.py

```yaml
#{HA安装目录}/components/input_text.py
#……省略……#
def async_setup(hass, config):
    """Set up an input text box."""
    component = hass.data = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#
```

- input_boolean.py

```yaml
#{HA安装目录}/components/input_boolean.py
#……省略……#
async def async_setup(hass, config):
    """Set up an input boolean."""
    component = hass.data = EntityComponent(_LOGGER, DOMAIN, hass)
#……省略……#
```

>__INFO:把官方的几个input_xxx component修改一下,以便保存EntityComponent到全局变量hass.data,后续可能需要调用。__

- common_timer.py(下载地址在文末)

```yaml
#添加到{HA配置目录}/custom_components/common_timer.py
```

>__INFO:测试版本,代码风格小随意。。。__

- group.yaml(根据实际情况配置)

```yaml
#{HA配置目录}/group.yaml
#参考
default_view:
view: yes
icon: mdi:home
entities:
    - group.common_timer
```

>__INFO:插件会生成一个group.common_timer,如果自定义过group.yaml的分组分页,需要配置放到要显示的分页里。__

## 3.小结 ##
- 难点1,往HA中动态添加ENTITY的方法,需要了解HA的启动过程,以免影响加载原来配置文件。
- 难点2,HA定时调用方法,试用track_time_interval貌似不准,摸索找到的async_track_time_change就好用。
- 难点3,环形定时队列轮询和执行任务分线程运行,以免影响环形定时队列轮询准确性。HA的异步调度机制仍然是个黑洞,不断调式、参考自带的代码摸索出一定的规律,算是勉强能用了。

: https://bbs.hassbian.com/thread-4504-1-1.html "基于Appdaemon的通用定时器"
: https://github.com/cnk700i/commo ... nts/common_timer.py "common_timer.py"


插件下载地址:**** Hidden Message *****















ylfzl 发表于 2018-8-16 23:43:06

学习一下,谢谢

yjb89 发表于 2018-8-17 07:06:30

看看这个干什么用啊

xieahui 发表于 2018-8-17 07:50:17

上次好像看到了 没弄好 这次试试

guitengyue 发表于 2018-8-17 08:08:28

大赞,支持!!!!!!!

Darian 发表于 2018-8-17 08:19:00

好东西,

yjb89 发表于 2018-8-17 10:44:02

在群晖上不能用吗?在群会上除了那个boolean,其他三个py文件都配置错误。怎么弄都不行。

cnk700i 发表于 2018-8-17 10:54:05

yjb89 发表于 2018-8-17 10:44
在群晖上不能用吗?在群会上除了那个boolean,其他三个py文件都配置错误。怎么弄都不行。 ...

把错误截图贴出来我看看。

yjb89 发表于 2018-8-17 11:00:53

老大能不能直接把这几个文件弄个附件,省得自己弄的全部错误啊。

cnk700i 发表于 2018-8-17 11:04:50

yjb89 发表于 2018-8-17 11:00
老大能不能直接把这几个文件弄个附件,省得自己弄的全部错误啊。

插件就一个文件。。。
另外3个是官方组件,修改一行代码就好。
页: [1] 2 3 4 5 6 7 8 9 10
查看完整版本: 通用型定时器【任务列表+周期任务+可服务调用】