Skip to content

Commit

Permalink
Fix integration test / Docker Compose env
Browse files Browse the repository at this point in the history
The Homeassistant setup needs updating for newer version. This allows
for a basic instance of HASS to be run via docker compose, and then
for wifi-presence to send events to it via MQTT.
  • Loading branch information
awilliams committed Oct 22, 2023
1 parent 4b1c026 commit ec54fd9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 9 deletions.
44 changes: 41 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
.PHONY:
.PHONY: test
test:
go test -race ../...
go test . ../internal/presence ../internal/hass -race -v -mqttAddr "tcp://localhost:1883"
go test \
-race \
../...
go test \
. \
../internal/presence \
../internal/hass \
-race \
-v \
-mqttAddr "tcp://localhost:1883"

.PHONY: wifi-presence
wifi-presence:
go run ../cmd/wifi-presence \
-apName="test-AP" \
-debounce="2s" \
-hass.autodiscovery=true \
-mqtt.addr="tcp://localhost:1883" \
-hostapd.socks="./hostapd.sock" \
-verbose

.PHONY: hostap-ctrl
hostap-ctrl:
go run ./hostap-ctrl

.PHONY: send-cfg
send-cfg:
echo '{"devices":[{"name":"test-phone","mac":"BE:EF:00:00:FA:CE"}]}' \
| mosquitto_pub \
-h 'localhost' \
-t 'wifi-presence/config' \
-l

.PHONY: send-empty-cfg
send-empty-cfg:
echo '{"devices":[]}' \
| mosquitto_pub \
-h 'localhost' \
-t 'wifi-presence/config' \
-l
76 changes: 76 additions & 0 deletions test/create-ha-account.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
#
# This script configures a fresh instance of HASS running
# as part of the associated Docker Compose environment.

set -e

HAURL="http://localhost:8123"

HANAME="admin"
HAUSER="${HANAME}"
HAPASSWORD="${HANAME}"

# Create temporary directory which is deleted on exit.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TMPDIR=`mktemp -d -p "$DIR"`
function cleanup {
rm -rf "${TMPDIR}"
}
trap cleanup EXIT

curl "${HAURL}/api/onboarding/users" \
--silent \
-H 'Content-Type: text/plain;charset=UTF-8' \
--data-binary "{\"client_id\":\"${HAURL}\",\"name\":\"${HANAME}\",\"username\":\"${HAUSER}\",\"password\":\"${HAPASSWORD}\",\"language\":\"en\"}" \
--output - \
| jq -r '.auth_code' > "${TMPDIR}/auth_code"

curl "${HAURL}/auth/token" \
--silent \
-X 'POST' \
-F "client_id=${HAURL}" \
-F "code=$(cat "${TMPDIR}/auth_code")" \
-F 'grant_type=authorization_code' \
--output - \
| jq -r '.access_token' > "${TMPDIR}/access_token"

curl "${HAURL}/api/onboarding/core_config" \
--silent \
-X 'POST' \
-H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \
-o /dev/null

curl "${HAURL}/api/onboarding/analytics" \
--silent \
-X 'POST' \
-H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \
-o /dev/null

curl "${HAURL}/api/onboarding/integration" \
--silent \
-H 'Content-Type: application/json;charset=UTF-8' \
-H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \
--data-binary "{\"client_id\":\"${HAURL}\",\"redirect_uri\":\"${HAURL}/?auth_callback=1\"}" \
-o /dev/null

# Configure MQTT integration.

curl "${HAURL}/api/config/config_entries/flow" \
--silent \
-X 'POST' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \
--data-binary '{"handler":"mqtt","show_advanced_options":false}' \
--output - \
| jq -r '.flow_id' > "${TMPDIR}/flow_id"

curl "${HAURL}/api/config/config_entries/flow/$(cat "${TMPDIR}/flow_id")" \
--silent \
-X 'POST' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H "Authorization: Bearer $(cat "${TMPDIR}/access_token")" \
--data-binary '{"broker":"mosquitto","port":1883}' \
-o /dev/null

echo "Created user '${HAUSER}'"
4 changes: 2 additions & 2 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
homeassistant:
container_name: 'ha'
image: 'homeassistant/home-assistant:2022.2.9'
image: 'homeassistant/home-assistant:2023.10.4'
volumes:
- './ha.yaml:/config/configuration.yaml'
ports:
Expand All @@ -11,7 +11,7 @@ services:

mosquitto:
container_name: mosquitto
image: eclipse-mosquitto
image: eclipse-mosquitto:2.0.17
volumes:
- './mosquitto.conf:/mosquitto/config/mosquitto.conf:ro'
ports:
Expand Down
5 changes: 1 addition & 4 deletions test/ha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ homeassistant:
name: Wifi Presence Test
auth_providers:
- type: trusted_networks
allow_bypass_login: true
trusted_networks:
- 172.0.0.0/8
- 192.0.0.0/8
allow_bypass_login: true
- type: homeassistant

mqtt:
broker: "mosquitto"

0 comments on commit ec54fd9

Please sign in to comment.