Update HASS, using shell_commands & HASSCTL to update, check config & restart HASS in a VirtualEnv
There are a few posts explaining how to script the update of HASS, inside a VirtualEnv.
My config: Genestealer/Home-Assistant-Configuration
I wanted to share mine, which sends a push notification with an action button, when pressed will use HASSCTL to update HA, run the config checker and only if it passes restart HA. A log of this script is stored in the same location as the script.
Note: This requires a modification using $sudo visudo
homeassistant ALL=(ALL) NOPASSWD:SETENV: /bin/systemctl
Also you need to have hassctl installed and setup, so it can update and restart your HA instance.
Steps
- Install hassclt:
sudo curl -o /usr/local/bin/hassctl https://raw.githubusercontent.com/dale3h/hassctl/master/hassctl && sudo chmod +x /usr/local/bin/hassctl
- Edit hassclt config:
sudo nano /etc/hassctl.conf
Edit hassctl.conf to match your configuration, mine is shown below :
HASSCTL_BRANCH=master
VIRTUAL_ENV=/srv/homeassistant
PIP_EXEC=$VIRTUAL_ENV/bin/pip3
HASS_EXEC=$VIRTUAL_ENV/bin/hass
HASS_CONFIG=/home/homeassistant/.homeassistant
HASS_USER=homeassistant
[email protected]
OZW_LOG=$HASS_CONFIG/OZW_Log.txt
automation:
- alias: Update Available Notification
# hide_entity: True
trigger:
platform: state
entity_id: updater.updater
action:
service: notify.google_cloud_notify
data_template:
title: 'New Home Assistant Release'
message: "Home Assistant {{ states.updater.updater.state }} is now available. {{now().strftime('%Y-%m-%d %I:%M %p')}}"
data:
data:
# url: "https://home-assistant.io/blog/" #"{{ states.updater.updater.attributes.release_notes}}"
actions:
- action: update
icon: "/static/icons/update-48x48.png"
title: Update Home Assistant
- alias: HTML5 push notification action button clicked
trigger:
platform: event
event_type: html5_notification.clicked
event_data:
action: update
action:
- service: notify.google_cloud_notify
data:
title: 'Home Assistant Update'
message: 'Update Started'
- service: shell_command.update_and_restart_hass_with_logging
shell_command:
- update_and_restart_hass_with_logging: "/bin/bash /home/homeassistant/.homeassistant/includes/shell_scripts/update_hass.sh </dev/null >> /home/homeassistant/.homeassistant/includes/shell_scripts/update_hass.log 2>&1 &"
Linux Shell Script
update_hassl.sh file stored in /home/homeassistant/.homeassistant/includes/shell_scripts/
#!/bin/bash
# This is for log purposes
echo
echo "[$(date)] HA update script starting, using HASSCTL"
# https://github.com/dale3h/hassctl
# Using HASSCTL this script updates HA, runs the config checker and only if it passes restart HA.
# This requires a modification using `sudo visudo`:E.G. "homeassistant ALL=(ALL) NOPASSWD:SETENV: /bin/systemctl"
# A log of this script is stored in the same location as this script.
# Example of HA shell_command used to call this script: update_and_restart_hass_with_logging: "/bin/bash /home/homeassistant/.homeassistant/includes/shell_scripts/update_hass.sh </dev/null >> /home/homeassistant/.homeassistant/includes/shell_scripts/update_hass.log 2>&1 &"
hassctl update-hass && hassctl config && hassctl restart