Skip to content

Commit 9ff7c12

Browse files
committed
homeassistant: add docs for https reverse proxy setup
Also: * fix header: must use double ## as a single # breaks mkdocs * add homeassistant advanced installation guide link
1 parent fcd8cc9 commit 9ff7c12

File tree

1 file changed

+117
-2
lines changed

1 file changed

+117
-2
lines changed

docs/Containers/Home-Assistant.md

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ $ docker-compose up -d
6060

6161
The direction being taken by the Home Assistant folks is to supply a ready-to-run image for your Raspberry Pi. That effectively dedicates your Raspberry Pi to Home Assistant and precludes the possibility of running alongside IOTstack and containers like Mosquitto, InfluxDB, Node-RED, Grafana, PiHole and WireGuard.
6262

63-
It is possible to run Supervised Home Assistant on the same Raspberry Pi as IOTstack. The recommended approach is to start from a clean slate and use [PiBuilder](https://github.com/Paraphraser/PiBuilder).
63+
Alternatively you can try to manually install Supervised Home Assistant using their [installation instructions for advanced users](https://github.com/home-assistant/supervised-installer) and when it works, install IOTstack. In theory this should work, but isn't tested or supported.
64+
65+
The recommended approach is to start from a clean slate and use [PiBuilder](https://github.com/Paraphraser/PiBuilder).
6466

6567
When you visit the PiBuilder link you may well have a reaction like "all far too complicated" but you should try to get past that. PiBuilder has two main use-cases:
6668

@@ -169,7 +171,7 @@ Random MACs are not a problem for a **client** device like a phone, tablet or la
169171
170172
It is not just configuration-time SSH sessions that break. If you decide to leave Raspberry Pi random Wifi MAC active **and** you have other clients (eq IoT devices) communicating with the Pi over WiFi, you will wrong-foot those clients each time the Raspberry Pi reboots. Data communications services from those clients will be impacted until those client devices time-out and catch up.
171173

172-
# Using bluetooth from the container
174+
## Using bluetooth from the container
173175
In order to be able to use BT & BLE devices from HA integrations, make sure that bluetooth is enabled and powered on at the start of the (Rpi) host by editing `/etc/bluetooth/main.conf`:
174176

175177
```conf
@@ -187,3 +189,116 @@ UP
187189
...
188190
```
189191
ref: https://scribles.net/auto-power-on-bluetooth-adapter-on-boot-up/
192+
193+
## HTTPS with a valid SSL certificate
194+
195+
Some HA integrations (e.g google assistant) require your HA API to be
196+
accessible via https with a valid certificate. You can configure HA to do this:
197+
[docs](https://www.home-assistant.io/docs/configuration/remote/) /
198+
[guide](https://www.home-assistant.io/docs/ecosystem/certificates/lets_encrypt/)
199+
or use a reverse proxy container, as described below.
200+
201+
The linuxserver Secure Web Access Gateway container
202+
([swag](https://docs.linuxserver.io/general/swag)) ([Docker hub
203+
docs](https://hub.docker.com/r/linuxserver/swag)) will automatically generate a
204+
SSL-certificate, update the SSL certificate before it expires and act as a
205+
reverse proxy.
206+
207+
1. First test your HA is working correctly: `http://raspberrypi.local:8123/` (assuming
208+
your RPi hostname is raspberrypi)
209+
2. Make sure you have duckdns working.
210+
3. On your internet router, forward public port 443 to the RPi port 443
211+
4. Add swag to ~/IOTstack/docker-compose.yml beneath the `services:`-line:
212+
```
213+
swag:
214+
image: ghcr.io/linuxserver/swag
215+
cap_add:
216+
- NET_ADMIN
217+
environment:
218+
- PUID=1000
219+
- PGID=1000
220+
- TZ=Etc/UTC
221+
- URL=<yourdomain>.duckdns.org
222+
- SUBDOMAINS=wildcard
223+
- VALIDATION=duckdns
224+
- DUCKDNSTOKEN=<token>
225+
- CERTPROVIDER=zerossl
226+
- EMAIL=<e-mail> # required when using zerossl
227+
volumes:
228+
- ./volumes/swag/config:/config
229+
ports:
230+
- 443:443
231+
restart: unless-stopped
232+
```
233+
Replace the bracketed values. Do NOT use any "-characters to enclose the values.
234+
235+
5. Start the swag container, this creates the file to be edited in the next step:
236+
```
237+
cd ~/IOTstack && docker-compose up -d
238+
```
239+
240+
Check it starts up OK: `docker-compose logs -f swag`. It will take a minute or two before it finally logs "Server ready".
241+
242+
6. Enable reverse proxy for `raspberrypi.local`. `homassistant.*` is already by default. and fix homeassistant container name ("upstream_app"):
243+
```
244+
sed -e 's/server_name/server_name *.local/' \
245+
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf.sample \
246+
> volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
247+
```
248+
249+
7. Forward to correct IP when target is a container running in "network_mode:
250+
host" (like Home Assistant does):
251+
```
252+
cat << 'EOF' | sudo tee volumes/swag/config/custom-cont-init.d/add-host.docker.internal.sh
253+
#!/bin/sh
254+
DOCKER_GW=$(ip route | awk 'NR==1 {print $3}')
255+
256+
sed -i -e "s/upstream_app .*/upstream_app ${DOCKER_GW};/" \
257+
/config/nginx/proxy-confs/homeassistant.subdomain.conf
258+
EOF
259+
sudo chmod u+x volumes/swag/config/custom-cont-init.d/add-host.docker.internal.sh
260+
```
261+
(This needs to be copy-pasted/entered as-is, ignore any "> "-prefixes printed
262+
by bash)
263+
264+
8. (optional) Add reverse proxy password protection if you don't want to rely
265+
on the HA login for security, doesn't affect API-access:
266+
```
267+
sed -i -e 's/#auth_basic/auth_basic/' \
268+
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
269+
docker-compose exec swag htpasswd -c /config/nginx/.htpasswd anyusername
270+
```
271+
9. Add `use_x_forwarded_for` and `trusted_proxies` to your homeassistant [http
272+
config](https://www.home-assistant.io/integrations/http). The configuration
273+
file is at `volumes/home_assistant/configuration.yaml` For a default install
274+
the resulting http-section should be:
275+
```
276+
http:
277+
use_x_forwarded_for: true
278+
trusted_proxies:
279+
- 192.168.0.0/16
280+
- 172.16.0.0/12
281+
- 10.77.0.0/16
282+
```
283+
10. Refresh the stack: `cd ~/IOTstack && docker-compose stop && docker-compose
284+
up -d` (again may take 1-3 minutes for swag to start if it recreates
285+
certificates)
286+
11. Test homeassistant is still working correctly:
287+
`http://raspberrypi.local:8123/` (assuming your RPi hostname is
288+
raspberrypi)
289+
12. Test the reverse proxy https is working correctly:
290+
`https://raspberrypi.local/` (browser will issue a warning about wrong
291+
certificate domain, as the certificate is issued for you duckdns-domain, we
292+
are just testing)
293+
294+
Or from the command line in the RPi:
295+
```
296+
curl --resolve homeassistant.<yourdomain>.duckdns.org:443:127.0.0.1 \
297+
https://homeassistant.<yourdomain>.duckdns.org/
298+
```
299+
(output should end in `if (!window.latestJS) { }</script></body></html>`)
300+
301+
13. And finally test your router forwards correctly by accessing it from
302+
outside your LAN(e.g. using a mobile phone):
303+
`https://homeassistant.<yourdomain>.duckdns.org/` Now the certificate
304+
should work without any warnings.

0 commit comments

Comments
 (0)