[HOME] Панель: план рисуется по стенам из размеров чертежа (14 зон, без фото)
lint / hygiene (push) Failing after 24s

This commit is contained in:
root committed 2026-11-01 22:40:00 +03:00
1 parent 5709f5b837
commit cbcd218c8f
2 files changed
+217 -214

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

+217 -214
View File
@@ -1,214 +1,217 @@
/* Покомнатная панель: план-подложка (img) + слой зон (SVG) по контурам комнат. */ /* Покомнатная панель: план рисуется по стенам (SVG), зоны = комнаты. */
const ROOMS = [ const ROOMS = [
{ id: "terrasa", name: "Терраса", icon: "mdi:deck", { id: "terrasa", name: "Терраса", icon: "mdi:deck",
poly: [[0.00, 0.00], [2.45, 0.00], [2.45, 8.35], [0.00, 8.35]] }, poly: [[0.00, 0.00], [2.45, 0.00], [2.45, 8.60], [0.00, 8.60]] },
{ id: "kitchen", name: "Кухня-гостиная", icon: "mdi:silverware-fork-knife", { id: "kitchen", name: "Кухня-гостиная", icon: "mdi:silverware-fork-knife",
poly: [[2.45, 0.00], [6.16, 0.00], [6.16, 8.35], [2.45, 8.35]] }, poly: [[2.45, 0.00], [6.21, 0.00], [6.21, 8.60], [2.45, 8.60]] },
{ id: "postirochnaia", name: "Тех.пом.", icon: "mdi:server", { id: "postirochnaia", name: "Тех.пом.", icon: "mdi:server",
poly: [[5.71, -1.50], [7.62, -1.50], [7.62, 0.00], [5.71, 0.00]] }, poly: [[5.71, -1.50], [7.62, -1.50], [7.62, 0.00], [5.71, 0.00]] },
{ id: "sanuzel", name: "Санузел", icon: "mdi:shower", { id: "sanuzel", name: "Санузел", icon: "mdi:shower",
poly: [[5.83, 0.30], [7.74, 0.30], [7.74, 1.55], [7.30, 1.55], poly: [[5.83, 0.00], [7.95, 0.00], [7.95, 2.97], [5.83, 2.97]] },
[7.30, 3.07], [5.83, 3.07]] }, { id: "tualet", name: "Туалет", icon: "mdi:toilet",
{ id: "tualet", name: "Туалет", icon: "mdi:toilet", poly: [[7.95, 1.55], [9.00, 1.55], [9.00, 2.97], [7.95, 2.97]] },
poly: [[7.74, 1.90], [8.62, 1.90], [8.62, 3.55], [7.74, 3.55]] }, { id: "garderob", name: "Гардероб", icon: "mdi:hanger",
{ id: "garderob", name: "Гардероб", icon: "mdi:hanger", poly: [[9.00, 0.00], [10.81, 0.00], [10.81, 1.55], [9.00, 1.55]] },
poly: [[9.05, 0.05], [10.95, 0.05], [10.95, 1.50], [9.05, 1.50]] }, { id: "kladovaia", name: "Кладовая", icon: "mdi:package-variant",
{ id: "kladovaia", name: "Кладовая", icon: "mdi:package-variant", poly: [[9.00, 1.55], [10.81, 1.55], [10.81, 2.40], [9.00, 2.40]] },
poly: [[9.29, 1.50], [10.95, 1.50], [10.95, 2.83], [9.29, 2.83]] }, { id: "koridor", name: "Холл", icon: "mdi:door-open",
{ id: "koridor", name: "Холл", icon: "mdi:door-open", poly: [[6.21, 2.97], [10.81, 2.97], [10.81, 5.29], [6.21, 5.29]] },
poly: [[7.74, 3.07], [9.05, 3.07], [9.05, 2.83], [11.07, 2.83], { id: "prihozhaya", name: "Прихожая", icon: "mdi:door",
[11.07, 5.48], [7.74, 5.48]] }, poly: [[6.21, 5.29], [7.74, 5.29], [7.74, 7.35], [6.21, 7.35]] },
{ id: "prihozhaya", name: "Прихожая", icon: "mdi:door", { id: "tambur", name: "Тамбур", icon: "mdi:door",
poly: [[5.95, 5.48], [7.74, 5.48], [7.74, 7.41], [5.95, 7.41]] }, poly: [[6.21, 7.35], [7.74, 7.35], [7.74, 8.60], [6.21, 8.60]] },
{ id: "tambur", name: "Тамбур", icon: "mdi:door", { id: "kabinet", name: "Комната 11,45", icon: "mdi:desk",
poly: [[5.95, 7.41], [7.74, 7.41], [7.74, 8.38], [5.95, 8.38]] }, poly: [[11.33, 0.00], [14.52, 0.00], [14.52, 3.41], [11.33, 3.41]] },
{ id: "kabinet", name: "Комната 11,45", icon: "mdi:desk", { id: "danina_komnata", name: "Комната 12,04", icon: "mdi:teddy-bear",
poly: [[11.07, 0.05], [14.55, 0.05], [14.55, 3.31], [11.07, 3.31]] }, poly: [[7.74, 5.29], [11.33, 5.29], [11.33, 8.60], [7.74, 8.60]] },
{ id: "danina_komnata", name: "Комната 12,04", icon: "mdi:teddy-bear", { id: "spalnia", name: "Комната 14,34", icon: "mdi:bed",
poly: [[7.74, 5.48], [11.07, 5.48], [11.07, 8.38], [7.74, 8.38]] }, poly: [[11.33, 3.41], [14.52, 3.41], [14.52, 8.60], [11.33, 8.60]] },
{ id: "spalnia", name: "Комната 14,34", icon: "mdi:bed", { id: "kryltso", name: "Крыльцо", icon: "mdi:stairs",
poly: [[11.07, 3.55], [14.55, 3.55], [14.55, 8.60], [11.07, 8.60]] }, poly: [[6.21, 8.60], [7.74, 8.60], [7.74, 10.10], [6.21, 10.10]] },
{ id: "kryltso", name: "Крыльцо", icon: "mdi:stairs", ];
poly: [[5.95, 8.38], [7.74, 8.38], [7.74, 9.70], [5.95, 9.70]] }, const KEY_DOMAINS = ["light", "switch", "climate", "media_player", "cover", "fan"];
]; const SENSOR_HINT = /temperature|humidity|co2|illuminance/;
const KEY_DOMAINS = ["light", "switch", "climate", "media_player", "cover", "fan"]; /* рамка плана в метрах */
const SENSOR_HINT = /temperature|humidity|co2|illuminance/; const VB = [-0.4, -1.9, 15.0, 10.5];
const IMG_W = 916, IMG_H = 768, PX0 = 180, PY0 = 173, PX_M = 42.0, PY_M = 41.4;
class RoomsCard extends HTMLElement {
class RoomsCard extends HTMLElement { constructor() {
constructor() { super();
super(); this._areas = [];
this._areas = []; this._ent2area = {};
this._ent2area = {}; this._selected = null;
this._selected = null; this._loaded = false;
this._loaded = false; this._mode = "plan";
this._mode = "plan"; this.attachShadow({ mode: "open" });
this.attachShadow({ mode: "open" }); }
}
setConfig(config) { this._config = config || {}; }
setConfig(config) { this._config = config || {}; }
set hass(hass) {
set hass(hass) { this._hass = hass;
this._hass = hass; if (!this._loaded) { this._loaded = true; this._loadRegistries(); }
if (!this._loaded) { this._loaded = true; this._loadRegistries(); } else this._render();
else this._render(); }
}
async _loadRegistries() {
async _loadRegistries() { try {
try { const [areas, devices, entities] = await Promise.all([
const [areas, devices, entities] = await Promise.all([ this._hass.callWS({ type: "config/area_registry/list" }),
this._hass.callWS({ type: "config/area_registry/list" }), this._hass.callWS({ type: "config/device_registry/list" }),
this._hass.callWS({ type: "config/device_registry/list" }), this._hass.callWS({ type: "config/entity_registry/list" }),
this._hass.callWS({ type: "config/entity_registry/list" }), ]);
]); this._areas = areas;
this._areas = areas; const devArea = {};
const devArea = {}; for (const d of devices) if (d.area_id) devArea[d.id] = d.area_id;
for (const d of devices) if (d.area_id) devArea[d.id] = d.area_id; this._ent2area = {};
this._ent2area = {}; for (const e of entities) {
for (const e of entities) { const a = e.area_id || devArea[e.device_id];
const a = e.area_id || devArea[e.device_id]; if (a) this._ent2area[e.entity_id] = a;
if (a) this._ent2area[e.entity_id] = a; }
} } catch (err) { this._error = String(err); }
} catch (err) { this._error = String(err); } this._render();
this._render(); }
}
_entitiesOf(areaId) {
_entitiesOf(areaId) { if (!areaId) return [];
if (!areaId) return []; return Object.entries(this._ent2area).filter(([, a]) => a === areaId)
return Object.entries(this._ent2area).filter(([, a]) => a === areaId) .map(([e]) => e).filter((e) => this._hass.states[e]);
.map(([e]) => e).filter((e) => this._hass.states[e]); }
}
_domain(e) { return e.split(".")[0]; }
_domain(e) { return e.split(".")[0]; }
_on(areaId) {
_on(areaId) { return this._entitiesOf(areaId).filter((e) => {
return this._entitiesOf(areaId).filter((e) => { const st = this._hass.states[e];
const st = this._hass.states[e]; return (this._domain(e) === "light" || this._domain(e) === "switch") && st.state === "on";
return (this._domain(e) === "light" || this._domain(e) === "switch") && st.state === "on"; }).length;
}).length; }
}
_sensors(areaId) {
_sensors(areaId) { const out = [];
const out = []; for (const e of this._entitiesOf(areaId)) {
for (const e of this._entitiesOf(areaId)) { if (this._domain(e) !== "sensor" || !SENSOR_HINT.test(e)) continue;
if (this._domain(e) !== "sensor" || !SENSOR_HINT.test(e)) continue; const st = this._hass.states[e];
const st = this._hass.states[e]; out.push(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${st.attributes.unit_of_measurement || ""}`);
out.push(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${st.attributes.unit_of_measurement || ""}`); }
} return out.slice(0, 2);
return out.slice(0, 2); }
}
_toggle(e) {
_toggle(e) { const d = this._domain(e);
const d = this._domain(e); if (d === "light" || d === "switch") this._hass.callService(d, "toggle", { entity_id: e });
if (d === "light" || d === "switch") this._hass.callService(d, "toggle", { entity_id: e }); }
}
_planSvg(areaId) {
_planHtml(areaId) { const [vx, vy, vw, vh] = VB;
const pts = (poly) => poly.map(([x, y]) => let shapes = "";
`${(x * PX_M + PX0).toFixed(1)},${(y * PY_M + PY0).toFixed(1)}`).join(" "); let labels = "";
const shapes = ROOMS.map((r) => { for (const r of ROOMS) {
const sel = r.id === areaId; const pts = r.poly.map(([x, y]) => `${x},${y}`).join(" ");
return `<polygon class="zone ${sel ? "sel" : ""}" data-a="${r.id}" points="${pts(r.poly)}"><title>${r.name}</title></polygon>`; const sel = r.id === areaId;
}).join(""); const on = this._on(r.id);
return `<div class="plan-wrap"> shapes += `<polygon class="zone ${sel ? "sel" : ""}" data-a="${r.id}" points="${pts}"><title>${r.name}</title></polygon>`;
<img class="plan-img" src="/local/house3d/plan.png?v=6" alt="План дома"/> const cx = r.poly.reduce((s, p) => s + p[0], 0) / r.poly.length;
<svg class="plan-layer" viewBox="0 0 ${IMG_W} ${IMG_H}" preserveAspectRatio="none">${shapes}</svg> const cy = r.poly.reduce((s, p) => s + p[1], 0) / r.poly.length;
</div>`; labels += `<text class="lbl" x="${cx}" y="${cy}" text-anchor="middle">${r.name}${on ? " · " + on : ""}</text>`;
} }
return `<svg class="plan" viewBox="${vx} ${vy} ${vw} ${vh}">
_render() { <rect x="${vx}" y="${vy}" width="${vw}" height="${vh}" fill="none"/>
if (!this._hass) return; ${shapes}${labels}</svg>`;
const areaId = this._selected; }
const tiles = ROOMS.map((r) => {
const ens = this._entitiesOf(r.id); _render() {
const on = this._on(r.id); if (!this._hass) return;
const sensors = this._sensors(r.id).join(" · "); const areaId = this._selected;
return `<button class="tile ${r.id === areaId ? "sel" : ""}" data-a="${r.id}"> const tiles = ROOMS.map((r) => {
<div class="tile-top"><ha-icon icon="${r.icon}"></ha-icon><span class="cnt">${ens.length}</span></div> const ens = this._entitiesOf(r.id);
<div class="tile-name">${r.name}</div> const on = this._on(r.id);
${on ? `<div class="tile-meta">💡 ${on}</div>` : ""} const sensors = this._sensors(r.id).join(" · ");
${sensors ? `<div class="tile-sens">${sensors}</div>` : ""}</button>`; return `<button class="tile ${r.id === areaId ? "sel" : ""}" data-a="${r.id}">
}).join(""); <div class="tile-top"><ha-icon icon="${r.icon}"></ha-icon><span class="cnt">${ens.length}</span></div>
<div class="tile-name">${r.name}</div>
let detail = ""; ${on ? `<div class="tile-meta">💡 ${on}</div>` : ""}
if (areaId) { ${sensors ? `<div class="tile-sens">${sensors}</div>` : ""}</button>`;
const room = ROOMS.find((r) => r.id === areaId); }).join("");
const ens = this._entitiesOf(areaId);
const groups = {}; let detail = "";
for (const e of ens) (groups[this._domain(e)] ||= []).push(e); if (areaId) {
const rows = Object.keys(groups).sort((a, b) => { const room = ROOMS.find((r) => r.id === areaId);
const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0); const ens = this._entitiesOf(areaId);
const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0); const groups = {};
return ia - ib; for (const e of ens) (groups[this._domain(e)] ||= []).push(e);
}).map((dom) => { const rows = Object.keys(groups).sort((a, b) => {
const items = groups[dom].map((e) => { const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0);
const st = this._hass.states[e]; const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0);
const nm = st.attributes.friendly_name || e; return ia - ib;
const val = st.state + (st.attributes.unit_of_measurement || ""); }).map((dom) => {
const act = ["light", "switch"].includes(dom) const items = groups[dom].map((e) => {
? `<span class="sw ${st.state === "on" ? "on" : ""}" data-e="${e}">${st.state === "on" ? "вкл" : "выкл"}</span>` : ""; const st = this._hass.states[e];
return `<div class="row"><span class="nm">${nm}</span><span class="vl">${val}</span>${act}</div>`; const nm = st.attributes.friendly_name || e;
}).join(""); const val = st.state + (st.attributes.unit_of_measurement || "");
return `<div class="grp"><div class="grp-h">${dom} · ${groups[dom].length}</div>${items}</div>`; const act = ["light", "switch"].includes(dom)
}).join(""); ? `<span class="sw ${st.state === "on" ? "on" : ""}" data-e="${e}">${st.state === "on" ? "вкл" : "выкл"}</span>` : "";
detail = `<div class="detail"><div class="d-head">${room ? room.name : areaId} return `<div class="row"><span class="nm">${nm}</span><span class="vl">${val}</span>${act}</div>`;
<button class="close" data-close="1">×</button></div>${rows || "<div class='row'>устройств пока нет</div>"}</div>`; }).join("");
} return `<div class="grp"><div class="grp-h">${dom} · ${groups[dom].length}</div>${items}</div>`;
}).join("");
this.shadowRoot.innerHTML = `<style> detail = `<div class="detail"><div class="d-head">${room ? room.name : areaId}
:host { display:block; font-family: var(--paper-font-body1_-_font-family, system-ui); } <button class="close" data-close="1">×</button></div>${rows || "<div class='row'>устройств пока нет</div>"}</div>`;
.bar { display:flex; gap:6px; margin-bottom:8px; } }
.bar button { background:rgba(30,36,44,.9); color:#cfe0f0; border:1px solid #39424e;
border-radius:9px; padding:4px 10px; font-size:12px; cursor:pointer; } this.shadowRoot.innerHTML = `<style>
.bar button.on { border-color:#4aa3ff; color:#dceaff; background:rgba(74,163,255,.16); } :host { display:block; font-family: var(--paper-font-body1_-_font-family, system-ui); }
.plan-wrap { position:relative; line-height:0; border-radius:12px; overflow:hidden; background:#f7f7f4; } .bar { display:flex; gap:6px; margin-bottom:8px; }
.plan-img { display:block; width:100%; height:auto; } .bar button { background:rgba(30,36,44,.9); color:#cfe0f0; border:1px solid #39424e;
.plan-layer { position:absolute; inset:0; width:100%; height:100%; } border-radius:9px; padding:4px 10px; font-size:12px; cursor:pointer; }
.zone { fill:rgba(47,127,224,0); stroke:rgba(47,127,224,0); stroke-width:2; cursor:pointer; transition:.12s; } .bar button.on { border-color:#4aa3ff; color:#dceaff; background:rgba(74,163,255,.16); }
.zone:hover { fill:rgba(47,127,224,.18); stroke:rgba(47,127,224,.9); } .plan { display:block; width:100%; height:auto; background:#f4f3ee; border-radius:12px; }
.zone.sel { fill:rgba(47,127,224,.30); stroke:rgba(20,90,200,1); stroke-width:3; } .zone { fill:#e9e4d8; stroke:#2b2b2b; stroke-width:0.06; cursor:pointer; transition:.12s; }
.grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap:10px; } .zone:hover { fill:#bcd6f5; stroke:#2f7fe0; }
.tile { text-align:left; border:1px solid #2a2f36; border-radius:14px; .zone.sel { fill:#9ec4f0; stroke:#1f6fd0; stroke-width:0.09; }
background: linear-gradient(160deg, rgba(30,36,44,.95), rgba(18,22,27,.95)); .lbl { font-size:0.30px; fill:#2a2a2a; pointer-events:none; }
color: #e8eef5; padding:12px; cursor:pointer; } .grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap:10px; }
.tile:hover { border-color:#4aa3ff; } .tile { text-align:left; border:1px solid #2a2f36; border-radius:14px;
.tile.sel { border-color:#4aa3ff; box-shadow:0 0 0 1px #4aa3ff inset; } background: linear-gradient(160deg, rgba(30,36,44,.95), rgba(18,22,27,.95));
.tile-top { display:flex; justify-content:space-between; align-items:center; opacity:.85; } color: #e8eef5; padding:12px; cursor:pointer; }
.cnt { font-size:11px; padding:1px 7px; border-radius:9px; background:rgba(74,163,255,.18); color:#8fc4ff; } .tile:hover { border-color:#4aa3ff; }
.tile-name { font-weight:600; margin-top:6px; font-size:14px; } .tile.sel { border-color:#4aa3ff; box-shadow:0 0 0 1px #4aa3ff inset; }
.tile-meta { font-size:11px; opacity:.7; margin-top:2px; } .tile-top { display:flex; justify-content:space-between; align-items:center; opacity:.85; }
.tile-sens { font-size:11px; opacity:.85; margin-top:6px; color:#9ed3ff; } .cnt { font-size:11px; padding:1px 7px; border-radius:9px; background:rgba(74,163,255,.18); color:#8fc4ff; }
.detail { border:1px solid #2a2f36; border-radius:14px; padding:12px; margin-top:10px; .tile-name { font-weight:600; margin-top:6px; font-size:14px; }
background: rgba(14,17,21,.92); max-height:420px; overflow:auto; } .tile-meta { font-size:11px; opacity:.7; margin-top:2px; }
.d-head { font-weight:700; margin-bottom:8px; display:flex; justify-content:space-between; } .tile-sens { font-size:11px; opacity:.85; margin-top:6px; color:#9ed3ff; }
.close { background:none;border:none;color:#9aa7b4;font-size:18px;cursor:pointer; } .detail { border:1px solid #2a2f36; border-radius:14px; padding:12px; margin-top:10px;
.grp-h { font-size:11px; text-transform:uppercase; letter-spacing:.06em; color:#7d8b99; margin:10px 0 4px; } background: rgba(14,17,21,.92); max-height:420px; overflow:auto; }
.row { display:flex; gap:8px; align-items:center; font-size:12px; padding:3px 0; .d-head { font-weight:700; margin-bottom:8px; display:flex; justify-content:space-between; }
border-bottom:1px dashed rgba(255,255,255,.05); } .close { background:none;border:none;color:#9aa7b4;font-size:18px;cursor:pointer; }
.nm { flex:1; opacity:.9; } .vl { opacity:.65; } .grp-h { font-size:11px; text-transform:uppercase; letter-spacing:.06em; color:#7d8b99; margin:10px 0 4px; }
.sw { font-size:11px; padding:1px 8px; border-radius:8px; border:1px solid #3a4350; cursor:pointer; } .row { display:flex; gap:8px; align-items:center; font-size:12px; padding:3px 0;
.sw.on { background:rgba(74,163,255,.2); border-color:#4aa3ff; color:#bcdcff; } border-bottom:1px dashed rgba(255,255,255,.05); }
</style> .nm { flex:1; opacity:.9; } .vl { opacity:.65; }
<div class="bar"> .sw { font-size:11px; padding:1px 8px; border-radius:8px; border:1px solid #3a4350; cursor:pointer; }
<button class="${this._mode === "plan" ? "on" : ""}" data-mode="plan">План</button> .sw.on { background:rgba(74,163,255,.2); border-color:#4aa3ff; color:#bcdcff; }
<button class="${this._mode === "tiles" ? "on" : ""}" data-mode="tiles">Плитки</button> </style>
</div> <div class="bar">
${this._mode === "plan" ? this._planHtml(areaId) : `<div class="grid">${tiles}</div>`} <button class="${this._mode === "plan" ? "on" : ""}" data-mode="plan">План</button>
${detail}`; <button class="${this._mode === "tiles" ? "on" : ""}" data-mode="tiles">Плитки</button>
</div>
this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) => ${this._mode === "plan" ? this._planSvg(areaId) : `<div class="grid">${tiles}</div>`}
b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); })); ${detail}`;
this.shadowRoot.querySelectorAll(".zone, .tile").forEach((el) =>
el.addEventListener("click", () => { this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) =>
this._selected = this._selected === el.dataset.a ? null : el.dataset.a; this._render(); b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); }));
})); this.shadowRoot.querySelectorAll(".zone, .tile").forEach((el) =>
this.shadowRoot.querySelectorAll(".sw").forEach((s) => el.addEventListener("click", () => {
s.addEventListener("click", (ev) => { ev.stopPropagation(); this._toggle(s.dataset.e); })); this._selected = this._selected === el.dataset.a ? null : el.dataset.a; this._render();
const close = this.shadowRoot.querySelector(".close"); }));
if (close) close.addEventListener("click", () => { this._selected = null; 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");
getCardSize() { return 12; } if (close) close.addEventListener("click", () => { this._selected = null; this._render(); });
} }
customElements.define("rooms-card", RoomsCard); getCardSize() { return 12; }
}
customElements.define("rooms-card", RoomsCard);