@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
296 lines (295 loc) • 9 kB
JavaScript
import { useMemo as h, useCallback as B, useState as H, useEffect as _ } from "react";
import { snakeCase as M, omit as D, cloneDeep as U, isEmpty as V } from "lodash";
import { useSubscribeEntity as q } from "./es/hooks/useSubscribeEntity/index.js";
import { useService as L } from "./es/hooks/useService/index.js";
import { useHistory as R } from "./es/hooks/useHistory/index.js";
import { j as I } from "./Provider-Cu7u27au.js";
import { Icon as y } from "@iconify/react";
import "./es/utils/light/index.js";
import { computeDomain as k } from "./es/utils/computeDomain.js";
import "./es/hooks/useLocale/locales/index.js";
import { u as X } from "./index-DnHwXFlq.js";
import "./es/HassConnect/HassContext.js";
import { useThrottledCallback as G } from "use-debounce";
import { getCssColorValue as J } from "./es/utils/colors.js";
import { diff as K } from "deep-object-diff";
import { timeAgo as Q } from "./es/utils/time/time-ago.js";
import "home-assistant-js-websocket";
import { binarySensorIcon as W } from "./es/hooks/useIcon/domains/binary_sensor.js";
import { sensorIcon as Y } from "./es/hooks/useIcon/domains/sensor.js";
import { numberIcon as Z } from "./es/hooks/useIcon/domains/number.js";
import { coverIcon as P } from "./es/hooks/useIcon/domains/cover.js";
import { alarmPanelIcon as tt } from "./es/hooks/useIcon/domains/alarm.js";
import { weatherIcon as et } from "./es/hooks/useIcon/domains/weather.js";
import { FIXED_DOMAIN_ICONS as p } from "./es/hooks/useIcon/domains/constants.js";
import "./es/hooks/useIcon/domains/battery.js";
function Dt(r, e) {
const t = h(() => {
const i = M(r);
return p[i] ? p[i] : "mdi:information-outline";
}, [r]);
return t === null ? null : /* @__PURE__ */ I(
y,
{
style: {
fontSize: e?.fontSize ?? "24px"
},
icon: t,
...e
}
);
}
function Ot(r, e) {
return h(() => r === null ? null : /* @__PURE__ */ I(
y,
{
style: {
fontSize: e?.fontSize ?? "24px"
},
icon: r || "octicon:info-24",
...e
}
), [r, e]);
}
function O(r, e) {
const t = e.state;
switch (r) {
case "alarm_control_panel":
return tt(t);
case "automation":
return t === "unavailable" ? "mdi:robot-confused" : t === "off" ? "mdi:robot-off" : "mdi:robot";
case "binary_sensor":
return W(e);
case "button":
switch (e.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 t === "off" ? "mdi:video-off" : "mdi:video";
case "cover":
return P(e);
case "device_tracker":
return e.attributes.source_type === "router" ? t === "home" ? "mdi:lan-connect" : "mdi:lan-disconnect" : ["bluetooth", "bluetooth_le"].includes(e.attributes.source_type) ? t === "home" ? "mdi:bluetooth-connect" : "mdi:bluetooth" : t === "not_home" ? "mdi:account-arrow-right" : "mdi:account";
case "event":
switch (e.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 t === "off" ? "mdi:fan-off" : "mdi:fan";
case "humidifier":
return t === "off" ? "mdi:air-humidifier-off" : "mdi:air-humidifier";
case "input_boolean":
return t === "on" ? "mdi:check-circle-outline" : "mdi:close-circle-outline";
case "input_datetime":
if (!e.attributes.has_date)
return "mdi:clock";
if (!e.attributes.has_time)
return "mdi:calendar";
break;
case "lock":
switch (t) {
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 (e.attributes.device_class) {
case "speaker":
switch (t) {
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 (t) {
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 (t) {
case "off":
return "mdi:audio-video-off";
default:
return "mdi:audio-video";
}
default:
switch (t) {
case "playing":
case "paused":
return "mdi:cast-connected";
case "off":
return "mdi:cast-off";
default:
return "mdi:cast";
}
}
case "number": {
const i = Z(e);
if (i)
return i;
break;
}
case "person":
return t === "not_home" ? "mdi:account-arrow-right" : "mdi:account";
case "switch":
switch (e.attributes.device_class) {
case "outlet":
return t === "on" ? "mdi:power-plug" : "mdi:power-plug-off";
case "switch":
return t === "on" ? "mdi:toggle-switch-variant" : "mdi:toggle-switch-variant-off";
default:
return "mdi:toggle-switch-variant";
}
case "sensor": {
const i = Y(e);
if (i)
return i;
break;
}
case "sun":
return e.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 t === "off" ? "mdi:water-boiler-off" : "mdi:water-boiler";
case "weather":
return et(e.state);
}
if (r in p)
return p[r];
}
function xt(r, e) {
const t = rt(r || "unknown", {
returnNullIfNotFound: !0
});
return h(() => {
if (t === null) return null;
const d = t.attributes.icon ?? O(k(r), t);
return d ? /* @__PURE__ */ I(
y,
{
style: {
fontSize: e?.fontSize ?? "24px"
},
icon: d,
...e
}
) : null;
}, [t, r, e]);
}
const v = {
throttle: 25,
returnNullIfNotFound: !1,
historyOptions: {
hoursToShow: 24,
significantChangesOnly: !0,
minimalResponse: !0,
disable: !0
}
};
function rt(r, e = v) {
const { throttle: t, returnNullIfNotFound: i, historyOptions: d } = {
...v,
...e,
historyOptions: {
...v.historyOptions,
...e.historyOptions
}
}, b = q(r), s = b(i), x = k(r), S = L(x, r), E = R(r, d), { useStore: z } = X(), C = z((o) => o.config?.language), c = B(
(o) => {
const g = /* @__PURE__ */ new Date(), a = new Date(o.attributes.last_triggered ?? o.last_updated), m = Q(a, C), w = Math.abs(g.getTime() - a.getTime()), l = m === "just now", { hexColor: f, rgbColor: T, brightness: F, brightnessValue: $, rgbaColor: j, color: A } = J(o);
return {
...o,
custom: {
color: A,
relativeTime: m,
timeDiff: w,
active: l,
hexColor: f,
rgbColor: T,
brightness: F,
brightnessValue: $,
rgbaColor: j
}
};
},
[C]
), N = G(
(o) => {
u(c(o));
},
t,
{
leading: !0,
trailing: !0
}
), [n, u] = H(s !== null ? c(s) : null);
return _(() => {
u((o) => o === null ? null : c(o));
}, [c]), _(() => {
const o = b(!0);
if (o && n) {
const g = K(
D(o, "custom", "last_changed", "last_updated", "context", "attributes.icon"),
D(n, "custom", "last_changed", "last_updated", "context", "attributes.icon")
), a = U(o), m = typeof a.attributes.icon == "string", w = typeof n.attributes.icon == "string";
let l = !V(g);
if (m && w && a.attributes.icon !== n.attributes.icon)
l = !0;
else if (!m) {
const f = O(k(a.entity_id), a);
f !== n.attributes.icon && (a.attributes.icon = f, l = !0);
}
l && N(a);
}
}, [n, N, b]), _(() => {
s && !n && u(c(s)), !s && n && u(null), s && n && s.entity_id !== n.entity_id && u(c(s));
}, [s, n, c]), h(() => n === null ? null : {
...n,
history: E,
service: S
}, [n, E, S]);
}
export {
Dt as a,
Ot as b,
xt as c,
O as g,
rt as u
};
//# sourceMappingURL=index-4TfmyaLW.js.map