UNPKG

@hakit/core

Version:

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

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