Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HA naming issue #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ build/bin/*
cmd/wifi-presence/wifi-presence
notes.md
Makefile
out/*
6 changes: 3 additions & 3 deletions cmd/wifi-presence/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ func run(ctx context.Context, appName string) error {

statusCtx, statusCancel := context.WithTimeout(ctx, 2*time.Second)
defer statusCancel()
if err := mqtt.StatusOnline(statusCtx); err != nil {
if err = mqtt.StatusOnline(statusCtx); err != nil {
return err
}
defer func() {
// Cannot use main context since it may have already been cancelled.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
_ = mqtt.StatusOffline(ctx)
mqttCloseCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
_ = mqtt.StatusOffline(mqttCloseCtx)
cancel()

mqtt.Close()
Expand Down
14 changes: 7 additions & 7 deletions internal/hass/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Configuration struct {

// TrackConfig describes a single Wifi station/device to monitor for state changes.
type TrackConfig struct {
Name string `json:"name"`
MAC string `json:"mac"`
Name string `json:"name"`
}

// DeviceTracker is used to configure HomeAssistant to track a device.
Expand All @@ -38,21 +38,21 @@ type DeviceTracker struct {
// Device is part of the DeviceTracker configuration.
type Device struct {
Connections [][2]string `json:"connections"` // A list of connections of the device to the outside world as a list of tuples [connection_type, connection_identifier]. For example the MAC address of a network interface: 'connections': ['mac', '02:5b:26:a8:dc:12'].
Name string `json:"name,omitempty"` // The name of the device.
ViaDevice string `json:"via_device,omitempty"` // The name of the device.
Manufacturer string `json:"manufacturer,omitempty"` // The manufacturer of the device.
Name string `json:"name,omitempty"` // The name of the device.
ViaDevice string `json:"via_device,omitempty"` // Identifier of a device that routes messages between this device and Home Assistant. Examples of such devices are hubs, or parent devices of a sub-device. This is used to show device topology in Home Assistant.
}

// Attrs are a device's attributes.
type Attrs struct {
Name string `json:"name"`
MAC string `json:"mac_address"`
IsConnected bool `json:"is_connected"`
APName string `json:"ap_name"`
SSID string `json:"ssid"`
BSSID string `json:"bssid"`
ConnectedAt *time.Time `json:"connected_at,omitempty"`
ConnectedFor int `json:"connected_for,omitempty"`
DisconnectedAt *time.Time `json:"disconnected_at,omitempty"`
DisconnectedFor int `json:"disconnected_for,omitempty"`
IsConnected bool `json:"is_connected"`
MAC string `json:"mac_address"`
Name string `json:"name"`
SSID string `json:"ssid"`
}
2 changes: 1 addition & 1 deletion internal/hass/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (m *MQTT) RegisterDeviceTracker(ctx context.Context, dsc Discovery) error {
},
Icon: icon,
JSONAttributesTopic: m.topics.DeviceJSONAttrs(dsc.MAC),
Name: fmt.Sprintf("%s %s", dsc.Name, m.apName),
Name: fmt.Sprintf("%s %s", dsc.MAC, m.apName), // This cannot start with 'dsc.Name'
ObjectID: deviceID,
PayloadAvailable: StatusOnline,
PayloadNotAvailable: StatusOffline,
Expand Down
2 changes: 1 addition & 1 deletion internal/presence/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (s staChange) String() string {
case staUpdated:
return "updated"
default:
return "?"
return fmt.Sprintf("?:%d", s)
}
}

Expand Down
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"
Loading