智能家居HomeAssistant(二)Docker安装homeassistant

1、查找镜像

docker search homeassistant

选择STARS最多的镜像

2、拉取镜像

docker pull homeassistant/home-assistant 

3、查看镜像

docker images homeassistant/home-assistant

4、启动镜像

docker run --init -d --name="home-assistant" -e "TZ=Asia/Shanghai" -v /PATH_TO_YOUR_CONFIG:/config --net=host homeassistant/home-assistant:latest

您需要替换/PATH_TO_YOUR_CONFIG为配置路径,例如,如果您选择配置路径为/home/hanhui/homeassistant,则命令将为:

docker run --init -d --name="home-assistant" -e "TZ=Asia/Shanghai" -v /home/hanhui/homeassistant:/config --net=host homeassistant/home-assistant:latest

5、查看容器运行状态

docker ps

6、开机自动启动配置

使用systemd自动启动home-assistant

/etc/systemd/system/[email protected]_USER.service

YOUR_USER替换为Home Assistant将以其运行的用户帐户(通常为homeassistant)。尤其是Ubuntu 16.04。

如果您想使用Docker,以下模板应该适合您。

 

[Unit]
Description=Home Assistant
Requires=docker.service
After=docker.service

[Service]
Restart=always
RestartSec=3
ExecStart=/usr/bin/docker run --name=home-assistant-%i -v /home/%i/.homeassistant/:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/home-assistant
ExecStop=/usr/bin/docker stop -t 2 home-assistant-%i
ExecStopPost=/usr/bin/docker rm -f home-assistant-%i

[Install]
WantedBy=multi-user.target

 

您需要重新加载systemd以使守护程序知道新配置。

sudo systemctl --system daemon-reload
 
要使Home Assistant在启动时自动启动,请启用该服务。

sudo systemctl enable [email protected]_USER
 
要禁用自动启动,请使用此命令。

sudo systemctl disable [email protected]_USER
 
要立即启动Home Assistant,请使用此命令。

sudo systemctl start [email protected]_USER
 
您也可以用start上面的内容替换stop为停止Home Assistant,restart重新启动Home Assistant和“状态”以查看简短的状态报告,如下所示。

[email protected]:~$ sudo systemctl status [email protected] 
[email protected]
   Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
   Active: active (running) since 二 2020-05-19 16:30:30 CST; 45min ago
  Process: 4336 ExecStopPost=/usr/bin/docker rm -f home-assistant-%i (code=exited, status=0/SUCCESS)
  Process: 4251 ExecStop=/usr/bin/docker stop -t 2 home-assistant-%i (code=exited, status=0/SUCCESS)
 Main PID: 4352 (docker)
   CGroup: /system.slice/system-home\x2dassistant.slice/[email protected]
           └─4352 /usr/bin/docker run --name=home-assistant-hanhui -v /home/hanhui/.homeassistant/:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/home
...

要获得家庭助理的日志记录输出,请简单使用journalctl。

sudo journalctl -f -u [email protected]_USER
 
由于日志可以快速滚动,因此您可以选择仅查看错误行:

sudo journalctl -f -u [email protected]_USER | grep -i 'error'
 
使用Home Assistant时,您可以轻松地重新启动系统,然后通过使用以下命令组合上述命令来查看日志输出 &&

sudo systemctl restart [email protected]_USER && sudo journalctl -f -u [email protected]_USER
 

7、自动重新启动配置
失败时自动重新启动HOME ASSISTANT

如果要在崩溃后自动重新启动Home Assistant服务,请将以下行添加到[Service]单元文件的部分:

Restart=on-failure
RestartSec=5s

 

扩展:

如果Home Assistant的时区异常,非Asia/shanghai时区。

输入如下命令,将宿主机的timezone复制替换HA容器中的timezone。

docker cp /etc/timezone 1eea462d3c39:/etc/    

(注:1eea462d3c39是你的HA容器ID)