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