import { useEffect, useState, type FormEvent } from "react"; import { api, type ControlKind, type DeviceConfig } from "./api"; const emptyForm = { entityId: "", name: "", kind: "switch" as ControlKind, priority: 1, minValue: 0, maxValue: 1, dwellSeconds: 300, manualOverride: false, onlyWhenSun: false, fromTime: null as string | null, toTime: null as string | null, enableUrl: null as string | null, disableUrl: null as string | null, }; export function Devices() { const [devices, setDevices] = useState([]); const [form, setForm] = useState(emptyForm); const [error, setError] = useState(null); async function refresh() { setDevices(await api.getDevices()); } useEffect(() => { refresh(); }, []); async function handleCreate(e: FormEvent) { e.preventDefault(); try { await api.createDevice(form); setForm(emptyForm); await refresh(); setError(null); } catch (err) { setError(err instanceof Error ? err.message : "Failed to create device"); } } async function toggleOverride(d: DeviceConfig) { await api.updateDevice(d.id, { manualOverride: !d.manualOverride }); await refresh(); } async function remove(d: DeviceConfig) { if (!confirm(`Remove ${d.name}?`)) return; await api.deleteDevice(d.id); await refresh(); } return ( <>

Add device

{form.kind === "number" && ( <> )}

Set both custom endpoints to control this device over plain HTTP (POST) instead of Home Assistant. The entity ID above is still used as the device's unique key.

{error && (

{error}

)}

Configured devices

{devices.length === 0 ? (

No devices configured yet.

) : ( devices .slice() .sort((a, b) => a.priority - b.priority) .map((d) => (
{d.name}
{d.entityId} · {d.kind} · priority {d.priority} · dwell{" "} {d.dwellSeconds}s{d.onlyWhenSun && " · sun only"} {d.fromTime && d.toTime && ` · ${d.fromTime}–${d.toTime}`} {d.enableUrl && d.disableUrl && " · custom endpoints"}
{d.enableUrl}
{d.disableUrl}
{d.manualOverride && ( Manual )}
)) )}
); }