citruz / haos-rockpi

Home Assistant OS for Rock Pi 4
Apache License 2.0
57 stars 4 forks source link

HOWTO: Automated update (proposal) #36

Open Elarion245 opened 2 days ago

Elarion245 commented 2 days ago

Describe the issue you are experiencing

Hey all,

I wanted to share my way through which I intend to realise updates with the ease of a simple button click.

It is a bit hacky, but maybe someone likes it and wants to try... I am not going into all the details, but rather want to explain the high-level concept.

First, create the following file: /homeassistant/haos_update/haos_update_script.sh with the following content (mind unix formating and line-ending) with the following content:

#!/bin/bash

echo -e -n "#!/bin/bash\nrauc install $1\n" > /mnt/data/supervisor/homeassistant/haos_update/perform_update.sh
chmod 777 /mnt/data/supervisor/homeassistant/haos_update/perform_update.sh

Next, activate the Scrape addon to scrape this awesome repo of citruz. grafik

The definitions are RockPi HAOS Available Version:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

RockPi HAOS Update Command:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

RockPi HAOS URL:

Ressource: https://github.com/citruz/haos-rockpi/releases
Method: GET

grafik

Next, define the following template sensor in configuration.yaml:

  - sensor:
      - name: "RockPi HAOS Current Version"
        unique_id: "RockPi_HAOS_Current"
        icon: "mdi:format-vertical-align-bottom"
        state: >-
          {{ (state_attr('update.home_assistant_operating_system_update', 'installed_version') | regex_findall("(^\d\d\.\d).*") | first) | float }}

This will yield the following entities: grafik

Also, add the following shell commands to configuration.yaml (check out other tutorials on how to generate the private_key or accessing through ssh on port 22222 to get full access to the underlying OS):

  haos_update_script: 'ssh 127.0.0.1 -p22222 -i /config/misc/private_key -o StrictHostKeyChecking=no "/mnt/data/supervisor/homeassistant/haos_update/haos_update_script.sh {{URL}}"'
  haos_perform_update: 'ssh 127.0.0.1 -p22222 -i /config/misc/private_key -o StrictHostKeyChecking=no "/mnt/data/supervisor/homeassistant/haos_update/perform_update.sh"'

Next, create the following automation:

alias: HAOS Update Available
description: ""
triggers:
  - hours: "7"
    trigger: time_pattern
  - hours: "17"
    trigger: time_pattern
conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.rockpi_haos_version
        above: sensor.haos_current_version
actions:
  - data:
      message: >-
        Install via update button on 'Scenarios' or manually via ssh on port 22
        and {{ states('sensor.rockpi_haos_update_command') }}
      title: >-
        HAOS update to version {{ states('sensor.rockpi_haos_version') }}
        available
    enabled: true
    action: notify.persistent_notification
  - action: shell_command.haos_update_script
    data:
      URL: "{{ states('sensor.rockpi_haos_url') }}"
mode: single

Finally, you can create the following button as part of your dashboard:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: shell_command.haos_perform_update
name: Update RockPi HAOS
icon: mdi:update

So basically, the scrape extension checks this site for new releases. Whenever the released version is greater than the currently installed one, a notification will pop up and the shell script to perform the update will be written/updated.

When pressing the update button, the update script is triggered. Mind that there is no error checking and nothing visually will happen, apart from the CPU load spiking during the update. If you hit the button without an update being available, you will silently update to your current version.

The logical flow is thus to press the update button whenever the automation makes you aware of a new update of citruz. Then you wait for a minute and restart HAOS completely. You should then find the newest version in the info section.

I hope this helps. Feel free to improve the method.

citruz commented 7 hours ago

Very cool! Thanks for sharing.