320 lines
14 KiB
JavaScript
320 lines
14 KiB
JavaScript
/* Панель для настенного планшета (Galaxy Tab S5e, 2560×1600, горизонтально).
|
||
* Принципы: без прокрутки, тач-цели ≥ 64px, цвет только для состояния,
|
||
* слева — план дома как селектор комнаты, справа — крупные органы управления.
|
||
*/
|
||
const ZONES = {
|
||
terrasa: [[180,173],[283,173],[283,524],[180,524]],
|
||
kitchen: [[291,173],[425,173],[425,294],[441,294],[441,348],[441,524],[291,524]],
|
||
postirochnaia: [[425,114],[499,114],[499,229],[425,229]],
|
||
sanuzel: [[499,173],[499,229],[425,229],[425,294],[479,294],[558,294],[558,173]],
|
||
tualet: [[558,229],[609,229],[609,294],[558,294]],
|
||
garderob: [[558,173],[558,229],[656,229],[656,173]],
|
||
kladovaia: [[609,229],[656,229],[656,294],[609,294]],
|
||
koridor: [[441,294],[441,348],[684,348],[684,294]],
|
||
prihozhaya: [[441,348],[516,348],[516,442],[441,442]],
|
||
tambur: [[441,442],[516,442],[516,524],[441,524]],
|
||
kabinet: [[656,173],[656,294],[684,294],[684,348],[787,348],[787,173],[656,173]],
|
||
danina_komnata: [[516,348],[633,348],[633,524],[516,524]],
|
||
spalnia: [[633,348],[787,348],[787,524],[633,524]],
|
||
kryltso: [[441,524],[441,593],[516,593],[516,524]],
|
||
};
|
||
const TC_ROOM_NAMES = {
|
||
terrasa: "Терраса",
|
||
kitchen: "Кухня-гостиная",
|
||
postirochnaia: "Тех.пом.",
|
||
sanuzel: "СУ",
|
||
tualet: "СУ 2",
|
||
garderob: "Гардеробная",
|
||
kladovaia: "Кладовая",
|
||
koridor: "Холл",
|
||
prihozhaya: "Прихожая",
|
||
tambur: "Тамбур",
|
||
kabinet: "Спальня",
|
||
danina_komnata: "Кабинет",
|
||
spalnia: "Данина комната",
|
||
kryltso: "Крыльцо",
|
||
};
|
||
const ROOM_ORDER = ["terrasa", "kitchen", "postirochnaia", "sanuzel", "tualet", "garderob", "kladovaia", "koridor", "prihozhaya", "tambur", "kabinet", "danina_komnata", "spalnia", "kryltso"];
|
||
|
||
class TabletCard extends HTMLElement {
|
||
constructor() {
|
||
super();
|
||
this._areas = [];
|
||
this._ent2area = {};
|
||
this._selected = "kitchen";
|
||
this._loaded = false;
|
||
this._busy = false;
|
||
this.attachShadow({ mode: "open" });
|
||
}
|
||
|
||
setConfig(config) { this._config = config || {}; }
|
||
|
||
set hass(hass) {
|
||
this._hass = hass;
|
||
if (!this._loaded) { this._loaded = true; this._load(); }
|
||
else if (!this._busy) this._render();
|
||
}
|
||
|
||
async _load() {
|
||
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 (e) { this._err = String(e); }
|
||
this._render();
|
||
}
|
||
|
||
_of(area) {
|
||
return Object.entries(this._ent2area).filter(([, a]) => a === area)
|
||
.map(([e]) => e).filter((e) => this._hass.states[e]);
|
||
}
|
||
|
||
_on(area) {
|
||
return this._of(area).filter((e) => {
|
||
const s = this._hass.states[e];
|
||
return /^light\.|^switch\./.test(e) && s.state === "on";
|
||
}).length;
|
||
}
|
||
|
||
_call(domain, service, data) {
|
||
this._busy = true;
|
||
this._hass.callService(domain, service, data).finally(() => {
|
||
this._busy = false;
|
||
this._render();
|
||
});
|
||
}
|
||
|
||
_wallsT() {
|
||
const seen = {}, out = [];
|
||
for (const id of ROOM_ORDER) {
|
||
const poly = ZONES[id];
|
||
if (!poly) continue;
|
||
for (let i = 0; i < poly.length; i++) {
|
||
const a = poly[i], b = poly[(i + 1) % poly.length];
|
||
const k1 = a + "-" + b, k2 = b + "-" + a;
|
||
if (seen[k1] || seen[k2]) continue;
|
||
seen[k1] = true;
|
||
out.push(`<line class="wall" x1="${a[0]}" y1="${a[1]}" x2="${b[0]}" y2="${b[1]}"/>`);
|
||
}
|
||
}
|
||
return out.join("");
|
||
}
|
||
|
||
_plan() {
|
||
const px = ([x, y]) => `${x},${y}`;
|
||
return `<svg class="plan" viewBox="150 90 680 530" preserveAspectRatio="xMidYMid meet">
|
||
${this._wallsT()}
|
||
${ROOM_ORDER.map((id) => {
|
||
const p = ZONES[id];
|
||
if (!p) return "";
|
||
const on = this._on(id);
|
||
const cls = "zone" + (id === this._selected ? " sel" : "") + (on ? " act" : "");
|
||
return `<polygon class="${cls}" data-r="${id}" points="${p.map(px).join(" ")}"></polygon>`;
|
||
}).join("")}
|
||
${ROOM_ORDER.map((id) => {
|
||
const p = ZONES[id];
|
||
if (!p) return "";
|
||
const cx = p.reduce((s, q) => s + q[0], 0) / p.length;
|
||
const cy = p.reduce((s, q) => s + q[1], 0) / p.length;
|
||
return `<text x="${cx}" y="${cy}" text-anchor="middle">${TC_ROOM_NAMES[id].split(" ")[0]}</text>`;
|
||
}).join("")}
|
||
</svg>`;
|
||
}
|
||
|
||
_tiles(area) {
|
||
const ens = this._of(area);
|
||
const groups = {};
|
||
for (const e of ens) (groups[e.split(".")[0]] ||= []).push(e);
|
||
const out = [];
|
||
const st = (e) => this._hass.states[e];
|
||
|
||
for (const e of groups.light || []) {
|
||
const s = st(e);
|
||
const on = s.state === "on";
|
||
const br = s.attributes.brightness ? Math.round(s.attributes.brightness / 255 * 100) : 100;
|
||
out.push(`<div class="tile light ${on ? "on" : ""}">
|
||
<div class="tt">${s.attributes.friendly_name || e}</div>
|
||
<button class="big" data-toggle="${e}">${on ? "ВКЛ" : "ВЫКЛ"}</button>
|
||
<input class="br" type="range" min="1" max="100" value="${br}" data-br="${e}">
|
||
</div>`);
|
||
}
|
||
for (const e of groups.switch || []) {
|
||
const s = st(e);
|
||
const on = s.state === "on";
|
||
out.push(`<div class="tile ${on ? "on" : ""}">
|
||
<div class="tt">${s.attributes.friendly_name || e}</div>
|
||
<button class="big" data-toggle="${e}">${on ? "ВКЛ" : "ВЫКЛ"}</button>
|
||
</div>`);
|
||
}
|
||
for (const e of groups.climate || []) {
|
||
const s = st(e);
|
||
const cur = s.attributes.current_temperature;
|
||
const tgt = s.attributes.temperature;
|
||
out.push(`<div class="tile">
|
||
<div class="tt">${s.attributes.friendly_name || e}</div>
|
||
<div class="val">${cur !== undefined ? cur + "°" : "—"}</div>
|
||
<div class="row2">
|
||
<button data-clim="${e}" data-d="-0.5">−</button>
|
||
<span class="tgt">${tgt !== undefined ? tgt + "°" : ""}</span>
|
||
<button data-clim="${e}" data-d="0.5">+</button>
|
||
</div>
|
||
</div>`);
|
||
}
|
||
for (const e of groups.cover || []) {
|
||
const s = st(e);
|
||
out.push(`<div class="tile">
|
||
<div class="tt">${s.attributes.friendly_name || e}</div>
|
||
<div class="row3">
|
||
<button data-cover="${e}" data-a="open_cover">↑</button>
|
||
<button data-cover="${e}" data-a="stop_cover">■</button>
|
||
<button data-cover="${e}" data-a="close_cover">↓</button>
|
||
</div>
|
||
</div>`);
|
||
}
|
||
for (const e of groups.media_player || []) {
|
||
const s = st(e);
|
||
out.push(`<div class="tile">
|
||
<div class="tt">${s.attributes.friendly_name || e}</div>
|
||
<div class="val sm">${s.state}</div>
|
||
<button class="big" data-media="${e}">▶ / ⏸</button>
|
||
</div>`);
|
||
}
|
||
const sensors = (groups.sensor || []).filter((e) => /temperature|humidity/.test(e)).slice(0, 4);
|
||
for (const e of sensors) {
|
||
const s = st(e);
|
||
out.push(`<div class="tile sens">
|
||
<div class="tt">${(s.attributes.friendly_name || e).slice(0, 22)}</div>
|
||
<div class="val">${s.state}${s.attributes.unit_of_measurement || ""}</div>
|
||
</div>`);
|
||
}
|
||
if (!out.length) out.push(`<div class="tile empty">В этой комнате пока нет устройств</div>`);
|
||
return out.join("");
|
||
}
|
||
|
||
_render() {
|
||
if (!this._hass) return;
|
||
const area = this._selected;
|
||
const ens = this._of(area);
|
||
const lights = (ens.filter((e) => e.startsWith("light.")) || []);
|
||
const onLights = lights.filter((e) => this._hass.states[e].state === "on").length;
|
||
|
||
this.shadowRoot.innerHTML = `<style>
|
||
:host { display:block; height:100%; }
|
||
.app { height:100%; box-sizing:border-box; display:grid;
|
||
grid-template-rows:auto 1fr; gap:14px; padding:14px;
|
||
background:#0b1016; color:#e8eef5;
|
||
font-family:'Segoe UI',system-ui,sans-serif; }
|
||
.top { display:flex; align-items:center; gap:14px; }
|
||
.clock { font-size:30px; font-weight:700; letter-spacing:.02em; }
|
||
.date { font-size:14px; color:#7f93a8; }
|
||
.chips { display:flex; gap:10px; margin-left:auto; }
|
||
.chip { padding:8px 14px; border-radius:12px; background:#141c25; border:1px solid #22303d;
|
||
font-size:14px; color:#9fb6c9; }
|
||
.chip.on { border-color:#3d8bff; color:#cfe4ff; background:#12233a; }
|
||
.main { display:grid; grid-template-columns: 1.25fr 1fr; gap:14px; min-height:0; }
|
||
.plan-wrap { background:#0f151c; border:1px solid #1e2a36; border-radius:18px; padding:8px;
|
||
display:flex; align-items:center; justify-content:center; min-height:0; }
|
||
.plan { width:100%; height:100%; }
|
||
.zone { fill:#1a2431; stroke:none; cursor:pointer; transition:.15s; }
|
||
.zone.act { fill:#1e3448; }
|
||
.wall { stroke:#33506b; stroke-width:3; stroke-linecap:square; }
|
||
.zone:hover { fill:#24405c; }
|
||
.zone.sel { fill:#2c5580; }
|
||
text { font-size:15px; fill:#8fb0c8; pointer-events:none; }
|
||
.rooms { display:flex; flex-wrap:wrap; gap:8px; margin-top:10px; }
|
||
.rbtn { padding:10px 14px; border-radius:12px; background:#141c25; border:1px solid #22303d;
|
||
color:#a9c0d4; font-size:15px; cursor:pointer; min-height:44px; }
|
||
.rbtn.sel { border-color:#3d8bff; color:#dceaff; background:#12233a; }
|
||
.rbtn.on { box-shadow: inset 0 -3px 0 #3d8bff; }
|
||
.panel { background:#0f151c; border:1px solid #1e2a36; border-radius:18px; padding:14px;
|
||
display:flex; flex-direction:column; min-height:0; }
|
||
.phead { display:flex; align-items:baseline; gap:10px; margin-bottom:10px; }
|
||
.pname { font-size:24px; font-weight:700; }
|
||
.pmeta { color:#7f93a8; font-size:14px; }
|
||
.tiles { display:grid; grid-template-columns:repeat(auto-fill,minmax(190px,1fr)); gap:12px;
|
||
overflow:auto; padding-right:4px; }
|
||
.tile { background:#131b24; border:1px solid #22303d; border-radius:16px; padding:12px;
|
||
display:flex; flex-direction:column; gap:8px; min-height:130px; }
|
||
.tile.on { border-color:#3d8bff; background:#12233a; }
|
||
.tt { font-size:14px; color:#9fb6c9; line-height:1.25; }
|
||
.val { font-size:26px; font-weight:700; }
|
||
.val.sm { font-size:16px; color:#9fb6c9; font-weight:500; }
|
||
.big { min-height:56px; border-radius:12px; border:1px solid #2a3b4c; background:#0e1620;
|
||
color:#cfe4ff; font-size:16px; font-weight:600; cursor:pointer; }
|
||
.tile.on .big { background:#1d4d86; border-color:#4a9bff; }
|
||
button { touch-action:manipulation; }
|
||
.row2, .row3 { display:flex; gap:8px; align-items:center; }
|
||
.row2 button, .row3 button { flex:1; min-height:48px; border-radius:12px; border:1px solid #2a3b4c;
|
||
background:#0e1620; color:#cfe4ff; font-size:20px; cursor:pointer; }
|
||
.tgt { font-size:18px; color:#9fb6c9; min-width:52px; text-align:center; }
|
||
.br { width:100%; accent-color:#3d8bff; height:34px; }
|
||
.tile.sens .val { font-size:22px; }
|
||
.empty { color:#7f93a8; }
|
||
</style>
|
||
<div class="app">
|
||
<div class="top">
|
||
<div><div class="clock" id="clk">--:--</div><div class="date" id="dt"></div></div>
|
||
<div class="chips">
|
||
<div class="chip ${onLights ? "on" : ""}">свет: ${onLights}</div>
|
||
<div class="chip">комнат: ${ROOM_ORDER.length}</div>
|
||
<div class="chip">устройств: ${ens.length}</div>
|
||
</div>
|
||
</div>
|
||
<div class="main">
|
||
<div class="plan-wrap">${this._plan()}</div>
|
||
<div class="panel">
|
||
<div class="phead"><span class="pname">${TC_ROOM_NAMES[area] || area}</span></div>
|
||
<div class="tiles">${this._tiles(area)}</div>
|
||
</div>
|
||
</div>
|
||
</div>`;
|
||
|
||
const clk = this.shadowRoot.querySelector("#clk");
|
||
const dt = this.shadowRoot.querySelector("#dt");
|
||
const now = new Date();
|
||
if (clk) clk.textContent = now.toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit" });
|
||
if (dt) dt.textContent = now.toLocaleDateString("ru-RU", { weekday: "long", day: "numeric", month: "long" });
|
||
|
||
this.shadowRoot.querySelectorAll(".zone").forEach((el) =>
|
||
el.addEventListener("click", () => { this._selected = el.dataset.r; this._render(); }));
|
||
|
||
this.shadowRoot.querySelectorAll("[data-toggle]").forEach((b) =>
|
||
b.addEventListener("click", () => {
|
||
const e = b.dataset.toggle;
|
||
this._call(e.split(".")[0], "toggle", { entity_id: e });
|
||
}));
|
||
this.shadowRoot.querySelectorAll("[data-br]").forEach((r) =>
|
||
r.addEventListener("change", (ev) => {
|
||
this._call("light", "turn_on", {
|
||
entity_id: ev.target.dataset.br, brightness_pct: parseInt(ev.target.value, 10),
|
||
});
|
||
}));
|
||
this.shadowRoot.querySelectorAll("[data-clim]").forEach((b) =>
|
||
b.addEventListener("click", () => {
|
||
const e = b.dataset.clim;
|
||
const cur = this._hass.states[e].attributes.temperature || 20;
|
||
this._call("climate", "set_temperature", {
|
||
entity_id: e, temperature: Math.round((cur + parseFloat(b.dataset.d)) * 2) / 2,
|
||
});
|
||
}));
|
||
this.shadowRoot.querySelectorAll("[data-cover]").forEach((b) =>
|
||
b.addEventListener("click", () => this._call("cover", b.dataset.a, { entity_id: b.dataset.cover })));
|
||
this.shadowRoot.querySelectorAll("[data-media]").forEach((b) =>
|
||
b.addEventListener("click", () => this._call("media_player", "media_play_pause", { entity_id: b.dataset.media })));
|
||
}
|
||
|
||
getCardSize() { return 20; }
|
||
}
|
||
|
||
customElements.define("tablet-card", TabletCard);
|