# Plan: SmartHome Ecosystem Integration

**Goal:** Maven connects to the SmartHome ecosystem — Home Assistant, MQTT, Zigbee2MQTT, or direct HTTP APIs — to read sensor states, control devices, and trigger automations based on facts and presence.

**Done when:**
- MQTT client in `internal/smarthome/mqtt.go` — connects to broker, subscribes to topic patterns, publishes control messages
- Home Assistant API client in `internal/smarthome/ha.go` — REST API + WebSocket for state reads and service calls
- SmartHome entities are writable as facts (sensor → fact write for loop predicates)
- Tool verbs for device control: `turn_on`, `turn_off`, `set_temp`, `set_brightness` — mapped to existing `tool.Executor` or the SmartHome API directly
- Presence integration: motion sensors, door sensors, WiFi presence feed into the existing `store.PresenceProbes` pipeline
- Voice control: "maven, выключи свет в гостиной" routes through `IntentAct` → SmartHome tool

**Scope:**
- New `internal/smarthome/` package — MQTT client, Home Assistant client, entity registry
- New `cmd/mavpoll/` extension — existing polling infrastructure extended with SmartHome sensors
- Config: `smarthome` block in `config.Config` — `{provider: "homeassistant|mqtt", url, token, mqtt_broker}`
- Reuses `internal/tool.Executor` for device control tools
- Reuses `internal/store.PresenceProbes` for presence input

**Steps:**
1. Create `internal/smarthome/ha.go` — Home Assistant REST client: `GetStates()`, `CallService(domain, service, target, data)`, subscribe to WebSocket events
2. Create `internal/smarthome/mqtt.go` — MQTT client via `github.com/eclipse/paho.mqtt.golang`: subscribe to `zigbee2mqtt/#`, `homeassistant/#`, publish to `cmnd/#`
3. Create `internal/smarthome/entity.go` — entity registry: maps entity_id → fact key, device_class → fact kind
4. Create programmatic tool registration — on startup, enumerate SmartHome entities and call `ProposeTool` for each controllable device (light, switch, climate, cover)
5. Wire MQTT sensor updates into `store.WriteFact` — e.g., `zigbee2mqtt/temperature` → `WriteFact(kind="env", key="temp:living_room", value="22.5")`
6. Wire SmartHome presence signals into `store.PresenceProbes` — e.g., WiFi presence or motion sensor → `presence_wifi` / `presence_motion` probes
7. Add `smarthome` block to `config.Config` and wire into `cmd/mavend/main.go` — starts separate goroutine for MQTT/WebSocket event loop
8. Test with a local MQTT broker and simulated sensor messages — verify facts are written and loop predicates can read them
