Blog |Follow Nick on Twitter| About
 

Everything today is a tab in your browser but sometimes you just want a dedicated app; 😬 nativefier is a great little utility for creating a middle ground, you get a single window application for any website you want!

IMHO Home Assistant is most useful as an app, Alt-Tab'ing to HA waaaayyy quicker than flicking to a browser and then again a pinned tab.

Here is an example using Ansible to wrap a URL into a Linux application, now there's nothing specifically Home Assistant about this example, by changing the Name/Icon/URL you can have anything you like has a local application, e.g. Twitter, BBC iPlayer, etc!

To further complicate things, I decided I didn't want to install nativefier since I'm likely to only use it once or twice, they provide a docker image so this script will leverage podman 😅

This screen-shot is my goal, a gnome application...

Home Assistant Icon in Gnome3 Apps

To get there, ansible will need a few things:

  1. Ansible Playbook
  2. Icon for Shortcut
  3. A .desktop file/template for the Shortcut
  4. A temporary folder to generate the stuff (and the owner/user of said folder)
  5. The URL to wrap up as an app

Ansible Playbook

Copy this onto your system, and edit the variables at the top to fit your needs!

---
- hosts: localhost
  become: true

  vars:
    hassio_dir : "/opt/hassio"
    hassio_url : "http://homeassistant.local:8123"
    hassio_icon : "./home-assistant-icon.png"
    hassio_temp : "./hassio"
    hassio_temp_usr : "linickx"

  tasks:
  - name: Check if Hassio App is installed
    stat: "path={{ hassio_dir }}"
    register: hassio_stat

  - name: Create Temp Home Assistant Directory
    become: no
    file:
      path: "{{ hassio_temp }}"
      state: directory
      owner: "{{ hassio_temp_usr }}"
      group: "{{ hassio_temp_usr }}"
    when: hassio_stat.stat.exists == False

  - name: Set Permissions on Temp Home Assistant Directory
    file:
      path: "{{ hassio_temp }}"
      mode: 0777
    when: hassio_stat.stat.exists == False

  - name: Podman - Run nativefier
    become: no
    containers.podman.podman_container:
      name: nativefier
      image:  docker.io/nativefier/nativefier:latest
      rm: yes
      command:
       - "--name Home Assistant"
       - "--icon /home-assistant-icon.png"
       - "--full-screen"
       - "{{ hassio_url }}"
       -  "/output/"
      volumes:
        - "{{ hassio_icon }}:/home-assistant-icon.png:Z"
        - "{{ hassio_temp }}/:/output/:Z"
    when: hassio_stat.stat.exists == False

  - name: Wait for Hassio to be built
    wait_for:
      path: "{{ hassio_temp }}/HomeAssistant-linux-x64/HomeAssistant"
      delay: 15
    when: hassio_stat.stat.exists == False

  - name: Check if files need moving...
    stat: path="{{ hassio_temp }}/HomeAssistant-linux-x64"
    register: hassio_build_stat

  - name: Move Hassio to {{ hassio_dir }}
    command: "mv {{ hassio_temp }}/HomeAssistant-linux-x64 {{ hassio_dir }}"
    when: hassio_build_stat.stat.exists

  - name: Change Hassio Permission (Owner)
    file:
      path: "{{ hassio_dir }}"
      owner: root
      group: root
      recurse: yes
    when: hassio_build_stat.stat.exists

  - name: Desktop Shortcut
    template:
      src: "./hassio.desktop.j2"
      dest: "/usr/share/applications/hassio.desktop"
      owner: root
      group: root
      mode: 0644

  - name: Clean up Temp Home Assistant Directory
    file:
      path: "{{ hassio_temp }}"
      state: absent

  - name: Clean up Podman  image
    containers.podman.podman_image:
      name: docker.io/nativefier/nativefier
      state: absent

Icon for Shortcut

This is easily Googled, but to save you 30sec the Home Assistant Logos can be found here.

The .desktop file

To make this work with Anisble, it's Jinja file. No changes needed here, save it as: hassio.desktop.j2

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=Home Assistant
Exec={{ hassio_dir }}/HomeAssistant
Terminal=false
Icon={{ hassio_dir }}/resources/app/icon.png
Type=Application
Categories=Application;Network;Internet;
Comment=Native Home Assistant GUI

The temporary folder & URL

The URL was set in the ansible variables, so a quick note about the temporary folder.

By default the nativefier container builds the application within "it's self" which is pretty much no use, therefore the folder is used as a local mount point allowing the output locally, since the container user ID is different to the ansible user, we need to create it as world writeable (which is a security issue), therefore once the process is completed and the container finishes we clean up by moving the folder and correcting the permissions!

Any URL will do!

Assuming this has made sense, the opportunities to make other apps should be clear, tweak the icon, update the .desktop template & away you go!

 

 
Nick Bettison ©