@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.13 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("lodash");require("../../utils/light/index.js");const t=require("react");require("../useLocale/locales/index.js");const x=require("../../../index-CUxh5LWn.cjs"),I=require("../../utils/supports-feature.js"),R=require("../../data/entity.js");require("../../HassConnect/HassContext.js");const L=require("../../../index-DvzmKbw2.cjs");require("@iconify/react");require("use-debounce");require("home-assistant-js-websocket");const c=require("./constants.js"),_=require("./camera.js");function W(M,s){const e=L.useEntity(M),{useStore:b,joinHassUrl:a}=x.useHass(),n=b(r=>r.connection),l=t.useRef(!1),f=t.useRef(!1),[S,T]=t.useState(void 0),[h,p]=t.useState(void 0),[u,A]=t.useState(void 0),[E,k]=t.useState(void 0),[g,d]=t.useState(s?.stream!==!1),[q,m]=t.useState(s?.poster!==!1),y=t.useMemo(()=>a(_.computeMJPEGStreamUrl(e)),[e,a]),o=t.useCallback(async()=>{if(s?.poster!==!1&&n&&!R.isUnavailableState(e.state)&&!f.current){f.current=!0,m(!0);try{const r=Math.ceil((s?.imageWidth??c.MAX_IMAGE_WIDTH)*devicePixelRatio),C=Math.ceil(r*(s?.aspectRatio??c.ASPECT_RATIO_DEFAULT)),P=await _.fetchThumbnailUrlWithCache(n,e.entity_id,r,C);T(a(P)),m(!1)}catch(r){m(!1),r instanceof Error&&k(r)}}},[e.entity_id,a,e.state,n,s?.poster,s?.aspectRatio,s?.imageWidth]),i=t.useCallback(async()=>{if(s?.stream!==!1&&n&&!R.isUnavailableState(e.state)&&!l.current){l.current=!0,d(!0);try{const r=await _.fetchStreamUrl(n,e.entity_id);p(a(r)),d(!1)}catch(r){d(!1),console.error(r),r instanceof Error&&A(r)}}},[e.entity_id,a,e.state,n,s?.stream]),U=t.useCallback(()=>u||!I.supportsFeatureFromAttributes(e.attributes,c.CAMERA_SUPPORT_STREAM)?!0:e.attributes.frontend_stream_type===c.STREAM_TYPE_WEB_RTC?typeof RTCPeerConnection>"u":!1,[e.attributes,u]);return t.useEffect(()=>{i(),o()},[i,o]),t.useMemo(()=>({...e,stream:{url:h,loading:g,error:u,refresh:async()=>(l.current=!1,i())},poster:{url:S,loading:q,error:E,refresh:async()=>(f.current=!1,o())},mjpeg:{url:y,shouldRenderMJPEG:U()}}),[e,h,U,g,u,i,S,q,E,y,o])}exports.useCamera=W;
//# sourceMappingURL=index.js.map