UNPKG

@hakit/core

Version:

A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.

261 lines (260 loc) 7.79 kB
import { X as _, ac as h } from "./index-BDZyTvOW.js"; import { useMemo as a } from "react"; import { Icon as b } from "@iconify/react"; import { snakeCase as I } from "lodash"; import { computeDomain as g } from "./es/utils/computeDomain.js"; import "./es/hooks/useLocale/locales/index.js"; import "home-assistant-js-websocket"; import "./es/utils/date.js"; import { getCssColorValue as y } from "./es/utils/colors.js"; import { timeAgo as S } from "./es/utils/time/time-ago.js"; import { useService as N } from "./es/hooks/useService/index.js"; import "use-debounce"; import { binarySensorIcon as E } from "./es/hooks/useIcon/domains/binary_sensor.js"; import { sensorIcon as O } from "./es/hooks/useIcon/domains/sensor.js"; import { numberIcon as D } from "./es/hooks/useIcon/domains/number.js"; import { coverIcon as z } from "./es/hooks/useIcon/domains/cover.js"; import { alarmPanelIcon as C } from "./es/hooks/useIcon/domains/alarm.js"; import { weatherIcon as x } from "./es/hooks/useIcon/domains/weather.js"; import { FIXED_DOMAIN_ICONS as l } from "./es/hooks/useIcon/domains/constants.js"; import "./es/hooks/useIcon/domains/battery.js"; import { useHistory as T } from "./es/hooks/useHistory/index.js"; import { useSubscribeEntity as F } from "./es/hooks/useSubscribeEntity/index.js"; const p = { returnNullIfNotFound: !1, historyOptions: { hoursToShow: 24, significantChangesOnly: !0, minimalResponse: !0, disable: !0 } }; function H(r, t) { const e = /* @__PURE__ */ new Date(), o = new Date(r.attributes.last_triggered ?? r.last_updated), n = S(o, t), i = Math.abs(e.getTime() - o.getTime()), f = n === "just now", { hexColor: c, rgbColor: u, brightness: m, brightnessValue: s, rgbaColor: w, color: d } = y(r), k = v(g(r.entity_id), r); return { ...r, attributes: { ...r.attributes, icon: r.attributes?.icon || k }, custom: { color: d, relativeTime: n, timeDiff: i, active: f, hexColor: c, rgbColor: u, brightness: m, brightnessValue: s, rgbaColor: w } }; } function $(r, t = p) { const { returnNullIfNotFound: e, historyOptions: o } = { ...p, ...t, historyOptions: { ...p.historyOptions, ...t.historyOptions } }, i = F(r)(e), f = g(r), c = N(f, r), u = T(r, o), m = _((d) => d.config?.language), s = a(() => i ? H(i, m) : null, [i, m]); return a(() => s == null ? null : { ...s, service: c, history: u }, [s, c, u]); } function ne(r, t) { const e = a(() => { const o = I(r); return l[o] ? l[o] : "mdi:information-outline"; }, [r]); return e === null ? null : /* @__PURE__ */ h( b, { style: { fontSize: t?.fontSize ?? "24px" }, icon: e, ...t } ); } function ie(r, t) { return a(() => r === null ? null : /* @__PURE__ */ h( b, { style: { fontSize: t?.fontSize ?? "24px" }, icon: r || "octicon:info-24", ...t } ), [r, t]); } function v(r, t) { const e = t.state; switch (r) { case "alarm_control_panel": return C(e); case "automation": return e === "unavailable" ? "mdi:robot-confused" : e === "off" ? "mdi:robot-off" : "mdi:robot"; case "binary_sensor": return E(t); case "button": switch (t.attributes.device_class) { case "identify": return "mdi:crosshairs-question"; case "restart": return "mdi:restart"; case "update": return "mdi:package-up"; default: return "mdi:button-pointer"; } case "camera": return e === "off" ? "mdi:video-off" : "mdi:video"; case "cover": return z(t); case "device_tracker": return t.attributes.source_type === "router" ? e === "home" ? "mdi:lan-connect" : "mdi:lan-disconnect" : ["bluetooth", "bluetooth_le"].includes(t.attributes.source_type) ? e === "home" ? "mdi:bluetooth-connect" : "mdi:bluetooth" : e === "not_home" ? "mdi:account-arrow-right" : "mdi:account"; case "event": switch (t.attributes.device_class) { case "doorbell": return "mdi:doorbell"; case "button": return "mdi:gesture-tap-button"; case "motion": return "mdi:motion-sensor"; default: return "mdi:eye-check"; } case "fan": return e === "off" ? "mdi:fan-off" : "mdi:fan"; case "humidifier": return e === "off" ? "mdi:air-humidifier-off" : "mdi:air-humidifier"; case "input_boolean": return e === "on" ? "mdi:check-circle-outline" : "mdi:close-circle-outline"; case "input_datetime": if (!t.attributes.has_date) return "mdi:clock"; if (!t.attributes.has_time) return "mdi:calendar"; break; case "lock": switch (e) { case "unlocked": return "mdi:lock-open"; case "jammed": return "mdi:lock-alert"; case "locking": case "unlocking": return "mdi:lock-clock"; default: return "mdi:lock"; } case "media_player": switch (t.attributes.device_class) { case "speaker": switch (e) { case "playing": return "mdi:speaker-play"; case "paused": return "mdi:speaker-pause"; case "off": return "mdi:speaker-off"; default: return "mdi:speaker"; } case "tv": switch (e) { case "playing": return "mdi:television-play"; case "paused": return "mdi:television-pause"; case "off": return "mdi:television-off"; default: return "mdi:television"; } case "receiver": switch (e) { case "off": return "mdi:audio-video-off"; default: return "mdi:audio-video"; } default: switch (e) { case "playing": case "paused": return "mdi:cast-connected"; case "off": return "mdi:cast-off"; default: return "mdi:cast"; } } case "number": { const o = D(t); if (o) return o; break; } case "person": return e === "not_home" ? "mdi:account-arrow-right" : "mdi:account"; case "switch": switch (t.attributes.device_class) { case "outlet": return e === "on" ? "mdi:power-plug" : "mdi:power-plug-off"; case "switch": return e === "on" ? "mdi:toggle-switch-variant" : "mdi:toggle-switch-variant-off"; default: return "mdi:toggle-switch-variant"; } case "sensor": { const o = O(t); if (o) return o; break; } case "sun": return t.state === "above_horizon" ? "mdi:white-balance-sunny" : "mdi:weather-night"; case "switch_as_x": return "mdi:swap-horizontal"; case "threshold": return "mdi:chart-sankey"; case "update": return "mdi:package"; case "water_heater": return e === "off" ? "mdi:water-boiler-off" : "mdi:water-boiler"; case "weather": return x(t.state); } if (r in l) return l[r]; } function se(r, t) { const e = $(r || "unknown", { returnNullIfNotFound: !0 }); return a(() => { if (e === null) return null; const n = e.attributes.icon ?? v(g(r), e); return n ? /* @__PURE__ */ h( b, { style: { fontSize: t?.fontSize ?? "24px" }, icon: n, ...t } ) : null; }, [e, r, t]); } export { ne as a, ie as b, se as c, v as g, $ as u }; //# sourceMappingURL=index-CPm3GSga.js.map