@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
143 lines (142 loc) • 3.83 kB
JavaScript
import "lodash";
import "./light/index.js";
import { computeDomain as c } from "./computeDomain.js";
import { l as _ } from "../../index-DnHwXFlq.js";
import { formatNumber as l } from "./number.js";
import { formatDuration as d, isDate as y, isTimestamp as h, checkValidDate as p } from "./date.js";
import "../HassConnect/HassContext.js";
import "react";
import "@iconify/react";
import "use-debounce";
import "deep-object-diff";
import "home-assistant-js-websocket";
import "../hooks/useLocale/locales/index.js";
const f = /* @__PURE__ */ new Set([
"temperature",
"current_temperature",
"target_temperature",
"target_temp_temp",
"target_temp_high",
"target_temp_low",
"target_temp_step",
"min_temp",
"max_temp"
]), g = {
light: {
brightness: (e) => Math.round(e / 255 * 100).toString()
},
media_player: {
volume_level: (e) => Math.round(e * 100).toString(),
media_duration: (e) => d(e.toString(), "s")
}
}, T = {
climate: {
humidity: "%",
current_humidity: "%",
target_humidity_low: "%",
target_humidity_high: "%",
target_humidity_step: "%",
min_humidity: "%",
max_humidity: "%"
},
cover: {
current_position: "%",
current_tilt_position: "%"
},
fan: {
percentage: "%"
},
humidifier: {
humidity: "%",
current_humidity: "%",
min_humidity: "%",
max_humidity: "%"
},
light: {
color_temp: "mired",
max_mireds: "mired",
min_mireds: "mired",
color_temp_kelvin: "K",
min_color_temp_kelvin: "K",
max_color_temp_kelvin: "K",
brightness: "%"
},
sun: {
azimuth: "°",
elevation: "°"
},
vacuum: {
battery_level: "%"
},
valve: {
current_position: "%"
},
sensor: {
battery_level: "%"
},
media_player: {
volume_level: "%"
}
}, v = (e, n, m) => {
const i = e.unit_system.length || "";
switch (m) {
case "visibility":
return n.attributes.visibility_unit || i;
case "precipitation":
return n.attributes.precipitation_unit || (i === "km" ? "mm" : "in");
case "pressure":
return n.attributes.pressure_unit || (i === "km" ? "hPa" : "inHg");
case "temperature":
case "templow":
return n.attributes.temperature_unit || e.unit_system.temperature;
case "wind_speed":
return n.attributes.wind_speed_unit || `${i}/h`;
case "humidity":
case "precipitation_probability":
return "%";
default: {
const a = e.unit_system;
return m in a ? a[m] : "";
}
}
}, A = (e, n, m, i, a) => {
const t = a !== void 0 ? a : e.attributes[i];
if (t === null)
return _("unknown");
if (typeof t == "number") {
const r = c(e.entity_id), o = g[r]?.[i], s = o ? o(t) : l(t);
let u = T[r]?.[i];
return r === "weather" ? u = v(n, e, i) : f.has(i) && (u = n.unit_system.temperature), u ? `${s}${u}` : s;
}
if (typeof t == "string" && y(t, !0)) {
if (h(t)) {
const o = new Date(t);
if (p(o))
return new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "2-digit",
second: "2-digit",
hourCycle: "h12"
}).format(o);
}
const r = new Date(t);
if (p(r))
return new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "long",
day: "numeric"
}).format(r);
}
return Array.isArray(t) && t.some((r) => r instanceof Object) || !Array.isArray(t) && t instanceof Object ? JSON.stringify(t) : Array.isArray(t) ? t.map((r) => A(e, n, m, i, r)).join(", ") : _(t);
};
export {
g as DOMAIN_ATTRIBUTES_FORMATERS,
T as DOMAIN_ATTRIBUTES_UNITS,
f as TEMPERATURE_ATTRIBUTES,
A as computeAttributeValueDisplay,
v as getWeatherUnit
};
//# sourceMappingURL=computeAttributeDisplay.js.map