UNPKG

@hakit/core

Version:

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

91 lines (90 loc) 2.94 kB
import "lodash"; import "../../utils/light/index.js"; import { useRef as M, useState as a, useMemo as T, useCallback as f, useEffect as b } from "react"; import "../useLocale/locales/index.js"; import { u as j } from "../../../index-BfdcdUoC.js"; import { supportsFeatureFromAttributes as F } from "../../utils/supports-feature.js"; import { isUnavailableState as A } from "../../data/entity.js"; import "../../HassConnect/HassContext.js"; import { u as H } from "../../../index-CG1EQGWs.js"; import "@iconify/react"; import { useStore as J } from "../useStore/index.js"; import "use-debounce"; import "home-assistant-js-websocket"; import { MAX_IMAGE_WIDTH as q, ASPECT_RATIO_DEFAULT as v, CAMERA_SUPPORT_STREAM as D, STREAM_TYPE_WEB_RTC as O } from "./constants.js"; import { computeMJPEGStreamUrl as k, fetchThumbnailUrlWithCache as B, fetchStreamUrl as X } from "./camera.js"; function oe(L, r) { const e = H(L), { joinHassUrl: n } = j(), s = J((t) => t.connection), m = M(!1), l = M(!1), [_, W] = a(void 0), [y, w] = a(void 0), [i, x] = a(void 0), [h, G] = a(void 0), [E, d] = a(r?.stream !== !1), [g, p] = a(r?.poster !== !1), P = T(() => n(k(e)), [e, n]), [o, I] = a({ frontend_stream_types: [] }), c = f(async () => { if (r?.poster !== !1 && s && !A(e.state) && !l.current) { l.current = !0, p(!0); try { const t = Math.ceil((r?.imageWidth ?? q) * devicePixelRatio), R = Math.ceil(t * (r?.aspectRatio ?? v)), C = await B(s, e.entity_id, t, R); W(n(C)), p(!1); } catch (t) { p(!1), t instanceof Error && G(t); } } }, [e.entity_id, n, e.state, s, r?.poster, r?.aspectRatio, r?.imageWidth]), S = f( async () => s?.sendMessagePromise({ type: "camera/capabilities", entity_id: e.entity_id }), [s, e.entity_id] ); b(() => { async function t() { I(await S() || { frontend_stream_types: [] }); } t(); }, [S]); const u = f(async () => { if (r?.stream !== !1 && s && !A(e.state) && !m.current) { m.current = !0, d(!0); try { const t = await X(s, e.entity_id); w(n(t)), d(!1); } catch (t) { d(!1), console.error(t), t instanceof Error && x(t); } } }, [e.entity_id, n, e.state, s, r?.stream]), U = f(() => i || !F(e.attributes, D) ? !0 : o.frontend_stream_types.includes(O) ? typeof RTCPeerConnection > "u" : !1, [e.attributes, o, i]); return b(() => { u(), c(); }, [u, c]), T(() => ({ ...e, stream: { url: y, loading: E, error: i, refresh: async () => (m.current = !1, u()) }, poster: { url: _, loading: g, error: h, refresh: async () => (l.current = !1, c()) }, mjpeg: { url: P, shouldRenderMJPEG: U() }, ...o }), [ e, y, U, E, i, u, _, g, h, P, c, o ]); } export { oe as useCamera }; //# sourceMappingURL=index.js.map