Files
charging_station/wiki/entities/wifi-relays.md
T
julian 56c9020d9f refactor(backend): single SQLite store — config.json eliminated
backend.db (renamed from sessions.db) now holds everything: the sessions
and samples tables plus a settings table with the app-state sections
(users, allowlist, relays, chargers, learn_mode, session_secret) as JSON
blobs. All mutations go through the API/panel and take effect immediately;
there is no config file and no hand-edit-then-restart ambiguity.

- legacy config.json auto-imports into the db on first start (renamed to
  config.json.migrated as a backup)
- new admin endpoints: PUT/DELETE /api/relays/{name} (how tinytuya wizard
  results are registered now) and PUT /api/chargers/{cp_id} (panel labels)
- BACKEND_DB env override so tests run on an isolated temp database
- smoke test: 45 checks, real db verified untouched
2026-07-11 20:15:38 +02:00

29 lines
2.2 KiB
Markdown

---
type: entity
updated: 2026-07-11
tags: [hardware, relays, tuya]
---
# WiFi Relays (SmartLife / Tuya)
Several WiFi relays scattered around the property, currently driven by the **SmartLife app** (owner report, 2026-07-11). Scope expansion: the [[backend]] should control them too, so one self-hosted system runs both the [[anfu-charger|charger]] and the relays.
## What they are
SmartLife = Tuya's consumer app, so these are **Tuya WiFi devices**. The "discovered by Bluetooth at first" behavior the owner observed is Tuya's standard BLE-assisted *pairing* (the phone passes WiFi credentials over BLE once); after provisioning the relays live on WiFi and speak **Tuya's local protocol on TCP 6668**, which the `tinytuya` library drives directly — **no Tuya cloud in the loop** for day-to-day control. SmartLife keeps working in parallel; local and app control don't conflict.
## Control path (implemented in `backend/relays.py`, ⚠️ awaiting keys)
Local control needs each device's **id + local key**, extracted once via Tuya's own API:
1. Create a (free) account at `iot.tuya.com` → Cloud → create a project (data center matching the SmartLife account region).
2. Link the SmartLife app account to the project (Devices → Link App Account → scan QR in SmartLife).
3. From the repo root: `venv\Scripts\python -m tinytuya wizard` — enter the project's API id/secret; it dumps every paired device's `id`, `key`, `ip`, and protocol `version`.
4. Register each relay via `PUT /api/relays/<name>` (admin; interactive form at `/docs`) with its `id`, `key`, `ip`, `version`. The Tuya cloud account is never needed again unless a device is re-paired (**re-pairing regenerates the local key**).
Recommended: DHCP reservations for the relays and pinned `"ip"` entries in the config — `"ip": "auto"` works via UDP broadcast discovery but is slower and only finds relays on the same L2 segment as the backend host (❓ verify which subnet the relays are on vs. the future always-on host, see [[network-topology]]).
## API (see [[backend]])
`GET /api/relays?live=true` (poll all), `GET /api/relays/{name}`, `POST /api/relays/{name}/on|off|toggle`. ⚠️ Untested against the physical relays until the keys are in the config.