From 960528c001599f98ed1d261f918521b7905ec5ac Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Tue, 5 Mar 2019 18:27:37 -0600 Subject: [PATCH] roles/homeassistant: Deploy Home Assistant This commit introduces a *homeassistant* role that installs and sets up Home Assistant using `pip`. --- homeassistant.yml | 13 +++++ roles/homeassistant/files/hass.sh | 3 ++ .../homeassistant/files/homeassistant.service | 12 +++++ roles/homeassistant/handlers/main.yml | 10 ++++ roles/homeassistant/tasks/main.yml | 54 +++++++++++++++++++ .../templates/configuration.yaml.j2 | 37 +++++++++++++ .../templates/homeassistant.httpd.conf.j2 | 13 +++++ 7 files changed, 142 insertions(+) create mode 100644 homeassistant.yml create mode 100644 roles/homeassistant/files/hass.sh create mode 100644 roles/homeassistant/files/homeassistant.service create mode 100644 roles/homeassistant/handlers/main.yml create mode 100644 roles/homeassistant/tasks/main.yml create mode 100644 roles/homeassistant/templates/configuration.yaml.j2 create mode 100644 roles/homeassistant/templates/homeassistant.httpd.conf.j2 diff --git a/homeassistant.yml b/homeassistant.yml new file mode 100644 index 0000000..40d5f5f --- /dev/null +++ b/homeassistant.yml @@ -0,0 +1,13 @@ +- hosts: home-assistant + roles: + - apache + - homeassistant + tasks: + - name: ensure homeassistant is running + service: + name: homeassistant + state: started + - name: ensure apache is running + service: + name: httpd + state: started diff --git a/roles/homeassistant/files/hass.sh b/roles/homeassistant/files/hass.sh new file mode 100644 index 0000000..c579028 --- /dev/null +++ b/roles/homeassistant/files/hass.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec /var/lib/homeassistant/.local/bin/hass diff --git a/roles/homeassistant/files/homeassistant.service b/roles/homeassistant/files/homeassistant.service new file mode 100644 index 0000000..ca3bced --- /dev/null +++ b/roles/homeassistant/files/homeassistant.service @@ -0,0 +1,12 @@ +# vim: set ft=systemd : +[Unit] +Description=Home Assistant + +[Service] +Type=simple +ExecStart=/usr/local/bin/hass +User=homeassistant +UMask=0077 + +[Install] +WantedBy=multi-user.target diff --git a/roles/homeassistant/handlers/main.yml b/roles/homeassistant/handlers/main.yml new file mode 100644 index 0000000..7bb1fd3 --- /dev/null +++ b/roles/homeassistant/handlers/main.yml @@ -0,0 +1,10 @@ +- name: reload systemd + command: systemctl daemon-reload +- name: restart homeassistant + service: + name: homeassistant + state: restarted +- name: restart httpd + service: + name: homeassistant + state: restarted diff --git a/roles/homeassistant/tasks/main.yml b/roles/homeassistant/tasks/main.yml new file mode 100644 index 0000000..431e7f8 --- /dev/null +++ b/roles/homeassistant/tasks/main.yml @@ -0,0 +1,54 @@ +- name: ensure system dependencies are installed + package: + name: + - python3-pip + state: present + tags: + - install + +- name: ensure homeassistant user exists + user: + name: homeassistant + system: true + home: /var/lib/homeassistant + +- name: ensure homeassistant is installed + become: true + become_user: homeassistant + pip: + name: homeassistant + extra_args: >- + --user + +- name: ensure homeassistant entry point is installed + copy: + src: hass.sh + dest: /usr/local/bin/hass + mode: '0755' + notify: + - restart homeassistant +- name: ensure homeassistant systemd unit is installed + copy: + src: homeassistant.service + dest: /etc/systemd/system/homeassistant.service + mode: '0644' + notify: + - reload systemd + - restart homeassistant +- name: ensure homeassistant starts at boot + service: + name: homeassistant + enabled: true + +- name: ensure apache is configured to proxy for homeassistant + template: + src: homeassistant.httpd.conf.j2 + dest: /etc/httpd/conf.d/homeassistant.conf + mode: '0644' + notify: + - restart httpd +- name: ensure selinux allows apache to proxy + seboolean: + name: httpd_can_network_connect + state: true + persistent: true diff --git a/roles/homeassistant/templates/configuration.yaml.j2 b/roles/homeassistant/templates/configuration.yaml.j2 new file mode 100644 index 0000000..8ffc9ae --- /dev/null +++ b/roles/homeassistant/templates/configuration.yaml.j2 @@ -0,0 +1,37 @@ +homeassistant: + # Name of the location where Home Assistant is running + name: Home + # Location required to calculate the time the sun rises and sets + latitude: 38.9568 + longitude: -94.6832 + # Impacts weather/sunrise data (altitude above sea level in meters) + elevation: 0 + # metric for Metric, imperial for Imperial + unit_system: imperial + # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones + time_zone: America/Chicago + # Customization file + customize: !include customize.yaml + +# Configure a default setup of Home Assistant (frontend, api, etc) +default_config: + +# Show the introduction message on startup. +introduction: + +# Uncomment this if you are using SSL/TLS, running in Docker container, etc. +# http: +# base_url: example.duckdns.org:8123 +http: + server_host: '::1' + +# Sensors +sensor: !include sensors.yaml + +# Text to speech +tts: + - platform: google + +group: !include groups.yaml +automation: !include automations.yaml +script: !include scripts.yaml diff --git a/roles/homeassistant/templates/homeassistant.httpd.conf.j2 b/roles/homeassistant/templates/homeassistant.httpd.conf.j2 new file mode 100644 index 0000000..03fc35e --- /dev/null +++ b/roles/homeassistant/templates/homeassistant.httpd.conf.j2 @@ -0,0 +1,13 @@ +ProxyPreserveHost On +ProxyRequests Off + +ProxyPass /api/websocket ws://localhost:8123/api/websocket +ProxyPassReverse /api/websocket ws://localhost:8123/api/websocket +ProxyPass / http://localhost:8123/ +ProxyPassReverse / http://localhost:8123/ + +RewriteEngine on +RewriteCond %{HTTP:Upgrade} =websocket [NC] +RewriteRule /(.*) ws://localhost:8123/$1 [P,L] +RewriteCond %{HTTP:Upgrade} !=websocket [NC] +RewriteRule /(.*) http://localhost:8123/$1 [P,L]