diff --git a/ops/home/rooms-card.js b/ops/home/rooms-card.js new file mode 100644 index 0000000..8f55c2a --- /dev/null +++ b/ops/home/rooms-card.js @@ -0,0 +1,230 @@ +/* Покомнатная панель: ПЛАН (кликабельная схема) + плитки зон + список устройств. */ +const AREA_M2 = { + kitchen: 27.65, danina_komnata: 11.14, spalnia: 14.34, postirochnaia: 4.32, + tambur: 2.96, koridor: 6.83, kabinet: 11.45, sanuzel: 6.29, tualet: 1.43, + garderob: 2.99, kladovaia: 1.30, terrasa: 20.09, kryltso: 3.00, +}; +/* черновые прямоугольники: [x0, y0, x1, y1] в метрах (x — длина, y — ширина) */ +const AREA_RECT = { + kitchen: [0, 0, 3.37, 8.2], + koridor: [3.37, 0, 4.35, 8.2], + tambur: [4.35, 0, 5.85, 1.75], + tualet: [4.35, 1.75, 5.15, 3.55], + garderob: [4.35, 3.55, 5.85, 5.9], + kladovaia: [5.85, 0, 7.1, 1.75], + sanuzel: [5.85, 1.75, 7.95, 4.3], + spalnia: [5.85, 4.3, 8.95, 8.2], + danina_komnata: [8.95, 0, 12.1, 4.05], + kabinet: [8.95, 4.05, 12.1, 8.2], + postirochnaia: [3.21, 8.6, 5.29, 10.1], + terrasa: [-2.45, 0, 0, 8.2], + kryltso: [3.71, -1.5, 5.51, 0], +}; +const AREA_ICON = { + kitchen: "mdi:silverware-fork-knife", danina_komnata: "mdi:teddy-bear", + spalnia: "mdi:bed", postirochnaia: "mdi:server", tambur: "mdi:door", + koridor: "mdi:door-open", kabinet: "mdi:desk", sanuzel: "mdi:shower", + tualet: "mdi:toilet", garderob: "mdi:hanger", kladovaia: "mdi:package-variant", + terrasa: "mdi:deck", kryltso: "mdi:stairs", +}; +const ROOM_ORDER = ["kitchen", "koridor", "spalnia", "danina_komnata", "kabinet", + "sanuzel", "tualet", "garderob", "kladovaia", "tambur", "postirochnaia", + "terrasa", "kryltso"]; +const KEY_DOMAINS = ["light", "switch", "climate", "media_player", "cover", "fan"]; +const SENSOR_HINT = /temperature|humidity|co2|illuminance/; + +class RoomsCard extends HTMLElement { + constructor() { + super(); + this._areas = []; + this._ent2area = {}; + this._selected = null; + this._loaded = false; + this._mode = "plan"; + this.attachShadow({ mode: "open" }); + } + + setConfig(config) { this._config = config || {}; } + + set hass(hass) { + this._hass = hass; + if (!this._loaded) { this._loaded = true; this._loadRegistries(); } + else this._render(); + } + + async _loadRegistries() { + try { + const [areas, devices, entities] = await Promise.all([ + this._hass.callWS({ type: "config/area_registry/list" }), + this._hass.callWS({ type: "config/device_registry/list" }), + this._hass.callWS({ type: "config/entity_registry/list" }), + ]); + this._areas = areas; + const devArea = {}; + for (const d of devices) if (d.area_id) devArea[d.id] = d.area_id; + this._ent2area = {}; + for (const e of entities) { + const a = e.area_id || devArea[e.device_id]; + if (a) this._ent2area[e.entity_id] = a; + } + } catch (err) { this._error = String(err); } + this._render(); + } + + _entitiesOf(areaId) { + return Object.entries(this._ent2area).filter(([, a]) => a === areaId) + .map(([e]) => e).filter((e) => this._hass.states[e]); + } + + _domain(e) { return e.split(".")[0]; } + + _on(areaId) { + return this._entitiesOf(areaId).filter((e) => { + const st = this._hass.states[e]; + return (this._domain(e) === "light" || this._domain(e) === "switch") && st.state === "on"; + }).length; + } + + _sensors(areaId) { + const out = []; + for (const e of this._entitiesOf(areaId)) { + if (this._domain(e) !== "sensor" || !SENSOR_HINT.test(e)) continue; + const st = this._hass.states[e]; + out.push({ n: (st.attributes.friendly_name || e).split(" ").slice(-1)[0], v: st.state, u: st.attributes.unit_of_measurement || "" }); + } + return out.slice(0, 2); + } + + _toggle(e) { + const d = this._domain(e); + if (d === "light" || d === "switch") this._hass.callService(d, "toggle", { entity_id: e }); + } + + _planSvg(areaId) { + const S = 34; + const x0 = -2.8, y0 = -1.9, x1 = 12.4, y1 = 10.4; + const W = (x1 - x0) * S, H = (y1 - y0) * S; + const px = (x) => (x - x0) * S; + const py = (y) => (y1 - y) * S; + let rects = ""; + for (const id of ROOM_ORDER) { + const r = AREA_RECT[id]; + if (!r) continue; + const name = (this._areas.find((a) => a.area_id === id) || {}).name || id; + const on = this._on(id); + const sel = id === areaId; + const [a0, b0, a1, b1] = r; + const w = (a1 - a0) * S, h = (b1 - b0) * S; + rects += ` + + ${name} + ${AREA_M2[id] || ""} м²${on ? " · 💡" + on : ""} + `; + } + return ` + + ${rects}`; + } + + _render() { + if (!this._hass) return; + const areaId = this._selected; + const tiles = ROOM_ORDER.filter((a) => this._areas.some((x) => x.area_id === a)).map((id) => { + const area = this._areas.find((x) => x.area_id === id); + const ens = this._entitiesOf(id); + const on = this._on(id); + const sensors = this._sensors(id).map((s) => `${s.n}: ${s.v}${s.u}`).join(" · "); + return ``; + }).join(""); + + let detail = ""; + if (areaId) { + const area = this._areas.find((x) => x.area_id === areaId); + const ens = this._entitiesOf(areaId); + const groups = {}; + for (const e of ens) (groups[this._domain(e)] ||= []).push(e); + const order = Object.keys(groups).sort((a, b) => { + const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 50 + a.charCodeAt(0); + const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 50 + b.charCodeAt(0); + return ia - ib; + }); + const rows = order.map((dom) => { + const items = groups[dom].map((e) => { + const st = this._hass.states[e]; + const nm = st.attributes.friendly_name || e; + const val = st.state + (st.attributes.unit_of_measurement || ""); + const act = ["light", "switch"].includes(dom) + ? `${st.state === "on" ? "вкл" : "выкл"}` : ""; + return `
${nm}${val}${act}
`; + }).join(""); + return `
${dom} · ${groups[dom].length}
${items}
`; + }).join(""); + detail = `
${area ? area.name : areaId} +
${rows || "
нет устройств
"}
`; + } + + this.shadowRoot.innerHTML = ` +
+ + +
+ ${this._mode === "plan" ? this._planSvg(areaId) : `
${tiles}
`} + ${detail}`; + + this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) => + b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); })); + this.shadowRoot.querySelectorAll(".room").forEach((g) => + g.addEventListener("click", () => { + this._selected = this._selected === g.dataset.a ? null : g.dataset.a; this._render(); + })); + this.shadowRoot.querySelectorAll(".tile").forEach((b) => + b.addEventListener("click", () => { + this._selected = this._selected === b.dataset.a ? null : b.dataset.a; this._render(); + })); + this.shadowRoot.querySelectorAll(".sw").forEach((s) => + s.addEventListener("click", (ev) => { ev.stopPropagation(); this._toggle(s.dataset.e); })); + const close = this.shadowRoot.querySelector(".close"); + if (close) close.addEventListener("click", () => { this._selected = null; this._render(); }); + } + + getCardSize() { return 8; } +} + +customElements.define("rooms-card", RoomsCard);