@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
91 lines (90 loc) • 2.9 kB
JavaScript
import "lodash";
import "./light/index.js";
import { computeDomain as y } from "./computeDomain.js";
import { l } from "../../index-BfdcdUoC.js";
import { isNumericFromAttributes as N, formatNumber as p, getNumberFormatOptions as _ } from "./number.js";
import { UNIT_TO_MILLISECOND_CONVERT as v, formatDuration as w, formatDateTime as f, formatDate as T, formatTime as h } from "./date.js";
import { UNKNOWN as C, UNAVAILABLE as O } from "../data/entity.js";
import "../HassConnect/HassContext.js";
import "react";
import "@iconify/react";
import "use-debounce";
import "home-assistant-js-websocket";
import "../hooks/useLocale/locales/index.js";
const B = (i, m, t, s, r) => {
const e = s?.[i.entity_id];
return g(
m,
t,
e,
i.entity_id,
i.attributes,
r !== void 0 ? r : i.state
);
};
let u;
const S = async (i) => u || (u = i.sendMessagePromise({
type: "sensor/numeric_device_classes"
}), u);
let d = [];
const g = (i, m, t, s, r, e) => {
if (e === C || e === O)
return l(e);
const o = y(s);
if (S(i).then((n) => d = n?.numeric_device_classes ?? []), N(r, o === "sensor" ? d : [])) {
const n = r.unit_of_measurement;
if (r.device_class === "duration" && r.unit_of_measurement && v[n] && t?.display_precision === void 0)
try {
return w(e, n);
} catch {
}
if (r.device_class === "monetary")
try {
return p(e, {
style: "currency",
currency: r.unit_of_measurement,
minimumFractionDigits: 2,
// Override monetary options with number format
..._({ state: e, attributes: r }, t)
});
} catch {
}
const c = p(e, _({ state: e, attributes: r }, t)), a = r.unit_of_measurement;
return a ? `${c}${a}` : c;
}
if (o === "datetime") {
const n = new Date(e);
return f(n, m);
}
if (["date", "input_datetime", "time"].includes(o))
try {
const n = e.split(" ");
if (n.length === 2)
return f(new Date(n.join("T")), m);
if (n.length === 1) {
if (e.includes("-"))
return T(/* @__PURE__ */ new Date(`${e}T00:00`), m);
if (e.includes(":")) {
const c = /* @__PURE__ */ new Date();
return h(/* @__PURE__ */ new Date(`${c.toISOString().split("T")[0]}T${e}`), m);
}
}
return e;
} catch {
return e;
}
if (o === "counter" || o === "number" || o === "input_number")
return p(e, _({ state: e, attributes: r }, t));
if (["button", "conversation", "event", "image", "input_button", "notify", "scene", "stt", "tag", "tts", "wake_word"].includes(o) || o === "sensor" && r.device_class === "timestamp")
try {
return f(new Date(e), m);
} catch {
return e;
}
return l(e);
};
export {
B as computeStateDisplay,
g as computeStateDisplayFromEntityAttributes
};
//# sourceMappingURL=computeStateDisplay.js.map