Last active
January 20, 2018 08:59
-
-
Save willwhui/26990b37b2fdbd43d2e68f6f550691d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
利用hass的shell_command重启hass服务 |
经过简单尝试,发现有一个问题:
重启hass,需要调用命令
sudo systemctl restart home-assistant@homeassistant
但是hass运行的用户是homeassistant,默认无法获得sudo权限
如果将homeassitant用户设置成为可以获取sudo权限的账户,又担心有系统安全问题。
解决方案:
在homeassistant用户的某目录下写下一个特定文件(如:restart.flag)
在pi用户下执行一个每隔n秒钟(比如3秒)就进行一次检测此文件是否存在
若存在,则先删除之并重启hass
但是系统自带的cron只支持最快每分钟执行一次检测任务
使用gnu.org的mcron: https://www.gnu.org/software/mcron/manual/mcron.html#Top 貌似太重
有一个丑陋的解决方案:通过每隔x秒执行一次来解决。
完整方案:
先配置好检测部分:
运行
crontab -e
在文件中追加:
# check a flag for hass restarting every 1min (for x times, see .sh file. And must run in background)
*/1 * * * * . /home/pi/check-hass-restart-flag.sh &
即:每分钟执行一次。注意必须以后台运行的方式执行,否则会导致阻塞
check-hass-restart-flag.sh 内容如下:
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
sleep 5
. /home/pi/check-hass-restart-flag-2.sh
即:每5秒运行一次实质的检查代码
check-hass-restart-flag-2.sh的内容:
isfound=$(ls /home/homeassistant/.homeassistant/flags | grep "restart.flag" | grep -v "grep"); if [ -z "$isfound" ]; then echo "not found"; else . /home/pi/check-hass-restart-flag-3.sh; fi
即:检测到存在一个flag文件(restart.flag),就调用重启的脚本
check-hass-restart-flag-3.sh内容如下:
sudo rm /home/homeassistant/.homeassistant/flags/restart.flag
. /home/pi/hassrestart.sh
hassrestart.sh内容如下:
sudo systemctl restart home-assistant@homeassistant
然后配置hass:
增加shell_command:
set_restart_hass_flag: echo "restart" > ~/.homeassistant/flags/restart.flag
这样的到了一个shell_command.set_restart_hass_flag服务
在automation中增加:
- alias: "Restart hass: trigger set flag up by button"
#hide_entity: true
trigger:
platform: event
event_type: click
event_data:
entity_id: !secret wireless_button_2_id
click_type: double
action:
- service: shell_command.set_restart_hass_flag
这样双击button,就可以在5秒钟内重启hass了
收尾
增加一个启动时删除flag文件的自动化规则,以防万一陷入无限重启:
- alias: "Restart hass: remove flag after restarting"
#hide_entity: true
trigger:
- platform: homeassistant
event: start
action:
- service: shell_command.remove_restart_hass_flag
对应的shell command很简单:
remove_restart_hass_flag: rm -f ~/.homeassistant/flags/restart.flag
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
思路:
响应小米开关的动作,执行本地命令
之所以不用现成的homeassistant.restart服务,是因为官方说:
解决问题的关键:
利用 shell command 组件: https://home-assistant.io/components/shell_command/