UNPKG

@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) 1.57 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),O=require("../useSubscribeEntity/index.js"),C=require("../../../index-CUxh5LWn.cjs"),g=require("./history.js"),R=require("./coordinates.js"),_=(i,e)=>{const{useStore:l}=C.useHass(),a=l(t=>t.connection),h=l(t=>t.config),r=n.useRef(!1),f=O.useSubscribeEntity(i),[c,d]=n.useState({}),[m,b]=n.useState({loading:!e?.disable,timeline:[],entityHistory:[],coordinates:[]}),s=n.useMemo(()=>({disable:e?.disable,significantChangesOnly:e?.significantChangesOnly,minimalResponse:e?.minimalResponse,hoursToShow:e?.hoursToShow,limits:e?.limits}),[e?.disable,e?.significantChangesOnly,e?.minimalResponse,e?.hoursToShow,e?.limits]);return n.useEffect(()=>{if(!a||s?.disable)return;let t=!0,o=null;return g.subscribeHistory({connection:a,entityIds:[i],significantChangesOnly:s?.significantChangesOnly,minimalResponse:s?.minimalResponse,hoursToShow:s?.hoursToShow,callbackFunction:u=>{t&&(r.current=!0,d(u))}}).then(u=>{o=u}).catch(()=>{o?.(),r.current=!1}),()=>{t=!1,o?.(),r.current=!1}},[i,s,a]),n.useEffect(()=>{if(!s?.disable&&r.current){const t=f(!0);if(!t)return;const o={[i]:t},y=g.computeHistory(h,o,c).timeline.filter(({entity_id:H})=>H===i),S=R.coordinatesMinimalResponseCompressedState(c[i],s?.hoursToShow??24,500,typeof s?.significantChangesOnly>"u"||s?.significantChangesOnly===!0?1:2,s?.limits)??[];b({loading:!1,timeline:y.length>0?y[0].data:[],entityHistory:c[i],coordinates:S})}},[i,h,s,f,c]),n.useMemo(()=>m,[m])};exports.useHistory=_; //# sourceMappingURL=index.js.map