@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
3 lines (2 loc) • 2.43 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("lodash");require("../../utils/light/index.js");const e=require("react");require("../useLocale/locales/index.js");const W=require("../../../index-DKqwfDib.cjs"),j=require("../../utils/supports-feature.js"),R=require("../../data/entity.js");require("../../HassConnect/HassContext.js");const v=require("../../../index-CqE3ydBk.cjs");require("@iconify/react");const w=require("../useStore/index.js");require("use-debounce");require("home-assistant-js-websocket");const l=require("./constants.js"),y=require("./camera.js");function F(M,s){const t=v.useEntity(M),{joinHassUrl:n}=W.useHass(),a=w.useStore(r=>r.connection),f=e.useRef(!1),d=e.useRef(!1),[S,T]=e.useState(void 0),[h,A]=e.useState(void 0),[i,k]=e.useState(void 0),[E,x]=e.useState(void 0),[p,m]=e.useState(s?.stream!==!1),[C,_]=e.useState(s?.poster!==!1),g=e.useMemo(()=>n(y.computeMJPEGStreamUrl(t)),[t,n]),[u,L]=e.useState({frontend_stream_types:[]}),o=e.useCallback(async()=>{if(s?.poster!==!1&&a&&!R.isUnavailableState(t.state)&&!d.current){d.current=!0,_(!0);try{const r=Math.ceil((s?.imageWidth??l.MAX_IMAGE_WIDTH)*devicePixelRatio),P=Math.ceil(r*(s?.aspectRatio??l.ASPECT_RATIO_DEFAULT)),U=await y.fetchThumbnailUrlWithCache(a,t.entity_id,r,P);T(n(U)),_(!1)}catch(r){_(!1),r instanceof Error&&x(r)}}},[t.entity_id,n,t.state,a,s?.poster,s?.aspectRatio,s?.imageWidth]),q=e.useCallback(async()=>a?.sendMessagePromise({type:"camera/capabilities",entity_id:t.entity_id}),[a,t.entity_id]);e.useEffect(()=>{async function r(){L(await q()||{frontend_stream_types:[]})}r()},[q]);const c=e.useCallback(async()=>{if(s?.stream!==!1&&a&&!R.isUnavailableState(t.state)&&!f.current){f.current=!0,m(!0);try{const r=await y.fetchStreamUrl(a,t.entity_id);A(n(r)),m(!1)}catch(r){m(!1),console.error(r),r instanceof Error&&k(r)}}},[t.entity_id,n,t.state,a,s?.stream]),b=e.useCallback(()=>i||!j.supportsFeatureFromAttributes(t.attributes,l.CAMERA_SUPPORT_STREAM)?!0:u.frontend_stream_types.includes(l.STREAM_TYPE_WEB_RTC)?typeof RTCPeerConnection>"u":!1,[t.attributes,u,i]);return e.useEffect(()=>{c(),o()},[c,o]),e.useMemo(()=>({...t,stream:{url:h,loading:p,error:i,refresh:async()=>(f.current=!1,c())},poster:{url:S,loading:C,error:E,refresh:async()=>(d.current=!1,o())},mjpeg:{url:g,shouldRenderMJPEG:b()},...u}),[t,h,b,p,i,c,S,C,E,g,o,u])}exports.useCamera=F;
//# sourceMappingURL=index.js.map