@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.12 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react");require("lodash");const v=require("../../../index-iGClY3ec.cjs");require("../useLocale/locales/index.js");require("home-assistant-js-websocket");require("../../utils/date.js");const y=require("../../../index-kv21QUm0.cjs");require("@iconify/react");const S=require("use-debounce"),d=require("./helpers.js");function W(n,E){const a=v.useHass(e=>e.connection),c=y.useEntity(n),[b,_]=r.useState(null),[l,g]=r.useState(null),s=r.useRef(!1),t=r.useRef(void 0),{type:f="daily"}=E||{},h=r.useCallback((e,o)=>d.subscribeForecast(a,e,o,u=>{s.current&&g(u)}).catch(u=>{s.current=!1,u instanceof Error&&_(u.message)}),[a]),q=S.useDebouncedCallback(async(e,o)=>{if(t.current){const i=await t.current;i&&i(),t.current=void 0}s.current=!0,t.current=await h(e,o)},100,{trailing:!0,leading:!0});if(r.useEffect(()=>(q(n,f),()=>{s.current=!1,t.current&&t.current()}),[f,q,h,n]),b)throw b;return r.useMemo(()=>{const e=d.getForecast(c.attributes,l);return{...c,forecast:e}},[c,l])}exports.useWeather=W;
//# sourceMappingURL=index.js.map