Skip to content

Add MQTT features: routing, device control, discovery, notifications, sleep tracking - #3

Merged
stuart22 merged 7 commits into
mainfrom
feature/mqtt-features
Apr 12, 2026
Merged

Add MQTT features: routing, device control, discovery, notifications, sleep tracking#3
stuart22 merged 7 commits into
mainfrom
feature/mqtt-features

Conversation

@stuart22

Copy link
Copy Markdown
Owner

Summary

  • Multi-camera MQTT routing (from tanvach PR feat: Enhance Nanit integration with WebSocket resilience and improved controls indiefan/home_assistant_nanit#33): Global connection registry routes commands to correct camera by baby UID, with auto-reconnect on failed commands
  • Device control: Night light brightness, sound playback (start/stop with soundtrack), and volume via MQTT JSON payloads
  • HA MQTT auto-discovery: Sensors (temp, humidity, motion, sound), binary sensors (crying, standing, alerts, sleep state), and switches (night light, standby) auto-registered in Home Assistant
  • Notification event polling: Polls Nanit REST API with jitter/backoff for motion, sound, crying, standing, left bed, alert zone, camera online/offline, breathing/temp/humidity alerts, low battery
  • Sleep tracking: Sleep event poller (fell asleep, woke up, put in bed, removed) with state machine + sleep stats poller (times woke up, interventions, awake/sleep time)
  • Updated protobuf schema: Added brightness (Settings), duration/soundtrack (Playback), SoundtrackStorage enum

All features gated behind env vars (NANIT_MQTT_ENABLED, NANIT_NOTIFICATIONS_ENABLED). See README for full env var table.

Test plan

  • Build passes (go build ./cmd/nanit/)
  • Container starts with NANIT_MQTT_ENABLED=true and NANIT_MQTT_BROKER_URL set
  • HA auto-discovers Nanit devices with sensors and switches
  • Toggle night light switch in HA → camera responds
  • Trigger motion → HA binary sensor updates
  • Sleep events appear in HA

…diefan#33)

- Connection registry routes MQTT commands to correct camera by baby UID
- Manager registry tracks websocket connection managers per baby
- Global MQTT handler registration (moved from per-connection to app-level)
- Handler signatures now include babyUID for proper multi-camera routing
- ForceReconnect() with cooldown skip for failed command recovery
- Pending retry queue replays failed commands after reconnection
- New env vars: NANIT_MQTT_RESET_WHEN_FAILED, NANIT_WEBSOCKET_TIMEOUT
- Extend protobuf schema with brightness (Settings), duration/soundtrack (Playback)
- Add MQTT subscriptions for brightness, playback (start/stop), and volume commands
- Route all commands through connection registry for multi-camera support
- JSON payloads for brightness, playback, and volume topics
- Publish discovery configs on MQTT connect for each registered baby
- Sensors: temperature, humidity, last motion/sound, stream URL
- Binary sensors: night, stream active, camera online, crying, standing,
  left bed, alert zone, temp/humidity/breathing alerts, low battery,
  asleep, in bed
- Switches: night light, standby mode
- Gated behind NANIT_MQTT_DISCOVERY env var (default true when MQTT enabled)
- New notification package with event poller, deduplication, and publisher
- Polls Nanit REST API for events with jitter and exponential backoff
- Publishes events to MQTT: motion, sound, crying, standing, left bed,
  alert zone, temperature/humidity/breathing alerts, camera online/offline
- New env vars: NANIT_NOTIFICATIONS_ENABLED, NANIT_NOTIFICATIONS_POLL_INTERVAL,
  NANIT_NOTIFICATIONS_MAX_BACKOFF
@stuart22
stuart22 merged commit 13da5bd into main Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant