From 19ce3b42b46a47a7ef3031848e9f75dcc611c502 Mon Sep 17 00:00:00 2001 From: Adrien Poupa Date: Sun, 5 Jan 2025 00:06:31 -0500 Subject: [PATCH] feat(homeassistant): Add Mosquitto MQTT broker --- homeassistant/.gitignore | 2 ++ homeassistant/README.md | 15 ++++++++++++ homeassistant/docker-compose.yml | 23 +++++++++++++++++++ homeassistant/mosquitto.env.example | 2 ++ homeassistant/mosquitto/config/mosquitto.conf | 9 ++++++++ 5 files changed, 51 insertions(+) create mode 100644 homeassistant/mosquitto.env.example create mode 100755 homeassistant/mosquitto/config/mosquitto.conf diff --git a/homeassistant/.gitignore b/homeassistant/.gitignore index 3c2994b..138fb88 100644 --- a/homeassistant/.gitignore +++ b/homeassistant/.gitignore @@ -2,3 +2,5 @@ !README.md !docker-compose.yml !backup.env.example +!mosquitto.env.example +!mosquitto/config/mosquitto.conf \ No newline at end of file diff --git a/homeassistant/README.md b/homeassistant/README.md index e5d1aa5..51a5c04 100644 --- a/homeassistant/README.md +++ b/homeassistant/README.md @@ -21,6 +21,21 @@ http: Set the `HOMEASSISTANT_ACCESS_TOKEN` for homepage support. +## MQTT + +If you need to use MQTT, you can enable it by setting `COMPOSE_PROFILES=homeassistant,mqtt`. + +Copy the Mosquitto environment file to `mosquitto.env` and fill the username and password: +`cp mosquitto.env.exmple mosquitto.env`. + +Start the container, create a user in mosquitto with the following command and the credentials defined in the `mosquitto.env` file: + +`docker compose exec mosquitto mosquitto_passwd -b /mosquitto/config/pwfile ` + +Restart the Mosquitto container to apply the changes. + +In HomeAssistant, add the MQTT integration with hostname `localhost`, port 1883 and the username and password defined in the `mosquitto.env` file. + ## Backup ### Enable Backups in HomeAssistant diff --git a/homeassistant/docker-compose.yml b/homeassistant/docker-compose.yml index ce82a42..34fcad8 100644 --- a/homeassistant/docker-compose.yml +++ b/homeassistant/docker-compose.yml @@ -34,6 +34,29 @@ services: - homepage.widget.key=${HOMEASSISTANT_ACCESS_TOKEN} profiles: - homeassistant + mosquitto: + container_name: mosquitto + image: eclipse-mosquitto + restart: always + user: ${USER_ID}:${GROUP_ID} + environment: + - PUID=${USER_ID} + - PGID=${GROUP_ID} + env_file: + - ${CONFIG_ROOT:-.}/homeassistant/mosquitto.env + volumes: + - ${CONFIG_ROOT:-.}/homeassistant/mosquitto/config:/mosquitto/config + - ${CONFIG_ROOT:-.}/homeassistant/mosquitto/data:/mosquitto/data + - ${CONFIG_ROOT:-.}/homeassistant/mosquitto/log:/mosquitto/log + ports: + - "1883:1883" + healthcheck: + test: [ "CMD", "mosquitto_sub", "-u", "$$MOSQUITTO_USERNAME", "-P", "$$MOSQUITTO_PASSWORD", "-h", "localhost", "-t", "$$SYS/broker/uptime", "-i", "docker_health_check", "-C", "1" ] + interval: 1m + timeout: 10s + retries: 3 + profiles: + - mqtt homeassistant-backup: image: adrienpoupa/rclone-backup:latest container_name: homeassistant-backup diff --git a/homeassistant/mosquitto.env.example b/homeassistant/mosquitto.env.example new file mode 100644 index 0000000..2e8e41a --- /dev/null +++ b/homeassistant/mosquitto.env.example @@ -0,0 +1,2 @@ +MOSQUITTO_USERNAME= +MOSQUITTO_PASSWORD= \ No newline at end of file diff --git a/homeassistant/mosquitto/config/mosquitto.conf b/homeassistant/mosquitto/config/mosquitto.conf new file mode 100755 index 0000000..a4ddd45 --- /dev/null +++ b/homeassistant/mosquitto/config/mosquitto.conf @@ -0,0 +1,9 @@ +persistence true +persistence_location /mosquitto/data/ + +password_file /mosquitto/config/pwfile +allow_anonymous false + +log_dest file /mosquitto/log/mosquitto.log + +listener 1883 \ No newline at end of file