Skip to content

Instantly share code, notes, and snippets.

@yriiolik
Forked from dieu/Dockerfile
Created March 29, 2024 16:36
Show Gist options
  • Save yriiolik/bf45d6f21c7ba5c42aa5759872994b7f to your computer and use it in GitHub Desktop.
Save yriiolik/bf45d6f21c7ba5c42aa5759872994b7f to your computer and use it in GitHub Desktop.
Home Assistant Docker on Mac OS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.homeassistant.dns.sd</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/dns-sd.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
homekit:
- name: HASS Bridge
port: 51827
advertise_ip: [REAL_IP]
#!/bin/bash -x
# register HASS Bridge by getting the avahi-browse output from the homeassistant container
DNS_SD_NAME="HASS Bridge"
LOG_FILE="/var/log/dns-sd.log"
prepare_command() {
echo '/usr/local/bin/docker exec -t $(/usr/local/bin/docker ps | grep homeassistant | cut -d" " -f1) avahi-browse -t -r -p -k _hap._tcp | grep -m 1 "HASS Bridge" | cut -d";" -f5-6,9-10 | awk -F";" '\''{printf "dns-sd -R \"HASS Bridge\" %s %s %s %s\n", $1, $2, $3, $4}'\'''
}
DNS_SD_CMD="" # Declare CMD as a global variable
run_command() {
PREPARE=$(prepare_command)
echo "Prepare command: $PREPARE" | tee -a $LOG_FILE
DNS_SD_CMD=$(eval $PREPARE)
echo "Running command: $DNS_SD_CMD" | tee -a $LOG_FILE
eval $DNS_SD_CMD >> $LOG_FILE 2>&1 &
pid=$!
echo "Command running with PID: $pid" | tee -a $LOG_FILE
}
check_and_run_command() {
if pgrep -f "$DNS_SD_NAME" >/dev/null; then
echo "Command is already running. Killing it..." | tee -a $LOG_FILE
pkill -f "$DNS_SD_NAME"
fi
run_command
}
check_if_need_run() {
PREPARE=$(prepare_command)
echo "Prepare command: $PREPARE" | tee -a $LOG_FILE
NEW_DNS_SD_CMD=$(eval $PREPARE)
if [[ "$NEW_DNS_SD_CMD" != "$DNS_SD_CMD" ]]; then
echo "DNS_SD_CMD is not the same." | tee -a $LOG_FILE
check_and_run_command
else
echo "DNS_SD_CMD is the same." | tee -a $LOG_FILE
fi
}
interval=86400 # 24 hours in seconds
# Infinite loop to periodically run the check
while true; do
check_if_need_run
sleep $interval
done
version: '3'
services:
homeassistant:
container_name: homeassistant
build: .
volumes:
- /path/to/config:/config
- /etc/localtime:/etc/localtime:ro
ports:
- 8123:8123
- 51827:51827
restart: unless-stopped
privileged: true
#!/bin/bash
set -euxo pipefail
# Start dbus and avahi-daemon
mkdir -p /var/run/dbus/
rm -f /run/dbus/dbus.pid
dbus-uuidgen > /var/lib/dbus/machine-id
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address
avahi-daemon --daemonize
# Run anything else you want to run before HA starts...
# Run original entrypoint
exec /init
FROM homeassistant/home-assistant:stable
# Install avahi-daemon in container
# https://gnanesh.me/avahi-docker-non-root.html
RUN set -ex \
&& apk --no-cache --no-progress add avahi avahi-tools dbus \
# Disable default Avahi services
&& rm /etc/avahi/services/* \
&& rm -rf /var/cache/apk/*
COPY docker-entrypoint.sh /usr/local/sbin/
ENTRYPOINT ["/usr/local/sbin/docker-entrypoint.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment