/* Панель для настенного планшета (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(``); } } return out.join(""); } _plan() { const px = ([x, y]) => `${x},${y}`; return ` ${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 ``; }).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 `${TC_ROOM_NAMES[id].split(" ")[0]}`; }).join("")} `; } _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(`
${s.attributes.friendly_name || e}
`); } for (const e of groups.switch || []) { const s = st(e); const on = s.state === "on"; out.push(`
${s.attributes.friendly_name || e}
`); } for (const e of groups.climate || []) { const s = st(e); const cur = s.attributes.current_temperature; const tgt = s.attributes.temperature; out.push(`
${s.attributes.friendly_name || e}
${cur !== undefined ? cur + "°" : "—"}
${tgt !== undefined ? tgt + "°" : ""}
`); } for (const e of groups.cover || []) { const s = st(e); out.push(`
${s.attributes.friendly_name || e}
`); } for (const e of groups.media_player || []) { const s = st(e); out.push(`
${s.attributes.friendly_name || e}
${s.state}
`); } const sensors = (groups.sensor || []).filter((e) => /temperature|humidity/.test(e)).slice(0, 4); for (const e of sensors) { const s = st(e); out.push(`
${(s.attributes.friendly_name || e).slice(0, 22)}
${s.state}${s.attributes.unit_of_measurement || ""}
`); } if (!out.length) out.push(`
В этой комнате пока нет устройств
`); 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 = `
--:--
свет: ${onLights}
комнат: ${ROOM_ORDER.length}
устройств: ${ens.length}
${this._plan()}
${TC_ROOM_NAMES[area] || area}
${this._tiles(area)}
`; 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);