spaxel/mothership/internal/webhook
jedarden eaad8f789a feat: implement home automation integration (MQTT and webhooks)
Implemented comprehensive home automation integration with MQTT support for
Home Assistant auto-discovery and a system-wide webhook for delivering all
Spaxel events.

MQTT Client (mothership/internal/mqtt/client.go):
- Extended MQTT client with proper Home Assistant auto-discovery payloads
- Added support for person presence binary sensors with device discovery
- Added zone occupancy sensors (count + binary occupied sensors)
- Added fall detection binary sensor with safety device class
- Added system health and system mode (select) entities
- Implemented retained message support for presence and occupancy states
- Added Last Will and Testament for availability topic
- Added exponential backoff reconnection (5s to 2min cap)
- Added discovery config cleanup (empty payload) on entity deletion

MQTT Event Publisher (mothership/internal/mqtt/publisher.go):
- Subscribes to internal event bus and publishes relevant events to MQTT
- Publishes zone entry/exit events updating person presence
- Publishes fall detection events with person/zone/timestamp info
- Publishes periodic system health updates (node count, quality, mode)
- Provides zone and person mapping for proper entity naming

System Webhook Integration (mothership/internal/webhook/publisher.go):
- System-wide webhook delivering ALL spaxel events to configured URL
- Event payload includes event_type, timestamp, zone, person, blob_id, severity
- Includes X-Spaxel-Event header with event type for routing
- Implements retry policy (one retry after 30s on 5xx errors)
- Supports concurrent event publishing with goroutine safety
- Provides TestWebhook method for configuration verification

Integration Settings API (mothership/internal/api/integrations.go):
- GET/POST /api/settings/integration for MQTT and webhook configuration
- MQTT settings: broker URL, username, password, TLS, discovery prefix
- Webhook settings: URL, enabled flag
- POST /api/settings/integration/test to test connections
- Returns connection status for MQTT (connected boolean)
- Validates URL formats and required fields

Tests:
- mothership/internal/mqtt/client_test.go: MQTT client tests including
  * Client creation with validation
  * Home Assistant discovery config format verification
  * MQTT topic generation and payload format validation
  * Retained message behavior tests
  * Broker URL parsing tests
- mothership/internal/webhook/publisher_test.go: Webhook tests including
  * Publisher creation and config updates
  * Event publishing with schema validation
  * Retry on 5xx server errors
  * Concurrent event publishing safety
  * All event type coverage
  * Test webhook delivery

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 21:17:07 -04:00
..
publisher.go feat: implement home automation integration (MQTT and webhooks) 2026-04-10 21:17:07 -04:00
publisher_test.go feat: implement home automation integration (MQTT and webhooks) 2026-04-10 21:17:07 -04:00