UNPKG

@hakit/core

Version:

A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.

40 lines (39 loc) 1.17 kB
import { useState as f, useRef as d, useMemo as m, useEffect as p } from "react"; import "lodash"; import "../../utils/light/index.js"; import "../useLocale/locales/index.js"; import { u as h } from "../../../index-DnHwXFlq.js"; import "../../HassConnect/HassContext.js"; import "@iconify/react"; import "use-debounce"; import "deep-object-diff"; import "home-assistant-js-websocket"; const x = (s) => { const { useStore: i } = h(), o = i((t) => t.connection), [c, n] = f(null), r = d(null), u = m(() => s, [s]); return p(() => () => { try { r.current?.(), r.current = null; } catch (t) { console.log("Error:", t), r.current = null; } }, []), p(() => { if (!o) return; const t = (e) => { e?.result && n((l) => l === e.result ? l : e.result); }, a = (e) => { e?.code === "template_error" ? n(e.error) : n(e?.error || "Could not process template request."); }; o.subscribeMessage(t, { type: "render_template", ...u }).then((e) => { r.current = e; }).catch((e) => { a(e); }); }, [o, u]), m(() => c, [c]); }; export { x as useTemplate }; //# sourceMappingURL=index.js.map