diff --git a/ops/home/plans/zones-check.png b/ops/home/plans/zones-check.png new file mode 100644 index 0000000..418a4aa Binary files /dev/null and b/ops/home/plans/zones-check.png differ diff --git a/ops/home/rooms-card.js b/ops/home/rooms-card.js index a58f48f..d1a62ca 100644 --- a/ops/home/rooms-card.js +++ b/ops/home/rooms-card.js @@ -1,224 +1,209 @@ -/* Покомнатная панель: точный план владельца как подложка + кликабельные зоны. */ -const AREA_M2 = { - kitchen: 27.65, danina_komnata: 12.04, spalnia: 14.34, postirochnaia: 4.32, - tambur: 2.94, koridor: 6.83, kabinet: 11.45, sanuzel: 6.29, tualet: 1.43, - garderob: 2.99, kladovaia: 1.30, terrasa: 20.09, kryltso: 3.00, -}; -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", -}; -/* [x0, y0, x1, y1] в метрах: x от западного края террасы, y от северной стены блока */ -const AREA_RECT = { - terrasa: [0.00, 0.00, 2.45, 8.35], - kitchen: [2.45, 0.00, 6.16, 8.35], - postirochnaia: [5.71, -1.50, 7.62, 0.00], - sanuzel: [5.83, 0.30, 7.74, 3.07], - tualet: [8.69, 2.71, 9.40, 3.55], - garderob: [9.05, 0.05, 10.95, 1.50], - kladovaia: [9.29, 1.50, 10.95, 2.83], - koridor: [9.05, 3.07, 11.07, 5.48], - kabinet: [11.07, 0.05, 14.55, 3.31], - danina_komnata: [7.74, 5.48, 11.07, 8.38], - spalnia: [11.07, 3.55, 14.55, 8.60], - tambur: [5.95, 7.41, 7.74, 8.38], - kryltso: [5.95, 8.38, 7.74, 9.70], -}; -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/; -/* подложка: план 916x768, дом 0..14.55 м по X и 0..8.6 м по Y от северной стены */ -const IMG_W = 916, IMG_H = 768, PX0 = 180, PY0 = 173, PX_M = 41.99, PY_M = 41.4; - -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(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${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 }); - } - - _hotspots(areaId) { - let out = ""; - for (const id of ROOM_ORDER) { - const r = AREA_RECT[id]; - if (!r) continue; - const [x0, y0, x1, y1] = r; - const left = ((x0 * PX_M + PX0) / IMG_W) * 100; - const top = ((y0 * PY_M + PY0) / IMG_H) * 100; - const w = (((x1 - x0) * PX_M) / IMG_W) * 100; - const h = (((y1 - y0) * PY_M) / IMG_H) * 100; - const name = (this._areas.find((a) => a.area_id === id) || {}).name || id; - out += `
`; - } - return out; - } - - _render() { - if (!this._hass) return; - const areaId = this._selected; - const plan = `
- План дома - ${this._hotspots(areaId)} -
`; - - 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).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 rows = Object.keys(groups).sort((a, b) => { - const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0); - const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0); - return ia - ib; - }).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" ? plan : `
${tiles}
`} - ${detail}`; - - this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) => - b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); })); - this.shadowRoot.querySelectorAll(".hs, .tile").forEach((el) => - el.addEventListener("click", () => { - this._selected = this._selected === el.dataset.a ? null : el.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 12; } -} - -customElements.define("rooms-card", RoomsCard); +/* Покомнатная панель: план владельца + зоны-полигоны по контурам комнат. */ +const ROOMS = [ + { id: "terrasa", name: "Терраса", icon: "mdi:deck", + poly: [[0, 0], [2.45, 0], [2.45, 8.35], [0, 8.35]] }, + { id: "kitchen", name: "Кухня-гостиная", icon: "mdi:silverware-fork-knife", + poly: [[2.45, 0], [6.16, 0], [6.16, 8.35], [2.45, 8.35]] }, + { id: "postirochnaia", name: "Тех.пом.", icon: "mdi:server", + poly: [[5.71, -1.5], [7.62, -1.5], [7.62, 0], [5.71, 0]] }, + { id: "sanuzel", name: "Санузел", icon: "mdi:shower", + poly: [[5.83, 0.30], [7.74, 0.30], [7.74, 3.07], [5.83, 3.07]] }, + { id: "tualet", name: "Туалет", icon: "mdi:toilet", + poly: [[8.69, 2.71], [9.40, 2.71], [9.40, 3.55], [8.69, 3.55]] }, + { id: "garderob", name: "Гардероб", icon: "mdi:hanger", + poly: [[9.05, 0.05], [10.95, 0.05], [10.95, 1.50], [9.05, 1.50]] }, + { id: "kladovaia", name: "Кладовая", icon: "mdi:package-variant", + poly: [[9.29, 1.50], [10.95, 1.50], [10.95, 2.83], [9.29, 2.83]] }, + { id: "koridor", name: "Холл", icon: "mdi:door-open", + poly: [[7.74, 3.07], [11.07, 3.07], [11.07, 5.48], [7.74, 5.48]] }, + { id: "prihozhaya", name: "Прихожая", icon: "mdi:door", + poly: [[5.95, 5.48], [7.74, 5.48], [7.74, 7.41], [5.95, 7.41]] }, + { id: "tambur", name: "Тамбур", icon: "mdi:door", + poly: [[5.95, 7.41], [7.74, 7.41], [7.74, 8.38], [5.95, 8.38]] }, + { id: "kabinet", name: "Комната 11,45", icon: "mdi:desk", + poly: [[11.07, 0.05], [14.55, 0.05], [14.55, 3.31], [11.07, 3.31]] }, + { id: "danina_komnata", name: "Комната 12,04", icon: "mdi:teddy-bear", + poly: [[7.74, 5.48], [11.07, 5.48], [11.07, 8.38], [7.74, 8.38]] }, + { id: "spalnia", name: "Комната 14,34", icon: "mdi:bed", + poly: [[11.07, 3.55], [14.55, 3.55], [14.55, 8.60], [11.07, 8.60]] }, + { 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 IMG_W = 916, IMG_H = 768, PX0 = 180, PY0 = 173, PX_M = 42.0, PY_M = 41.4; + +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) { + if (!areaId) return []; + 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(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${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 pts = (poly) => poly.map(([x, y]) => + `${(x * PX_M + PX0).toFixed(1)},${(y * PY_M + PY0).toFixed(1)}`).join(" "); + const shapes = ROOMS.map((r) => { + const sel = r.id === areaId; + return ``; + }).join(""); + return ` + + ${shapes}`; + } + + _render() { + if (!this._hass) return; + const areaId = this._selected; + const tiles = ROOMS.map((r) => { + const ens = this._entitiesOf(r.id); + const on = this._on(r.id); + const sensors = this._sensors(r.id).join(" · "); + return ``; + }).join(""); + + let detail = ""; + if (areaId) { + const room = ROOMS.find((r) => r.id === areaId); + const ens = this._entitiesOf(areaId); + const groups = {}; + for (const e of ens) (groups[this._domain(e)] ||= []).push(e); + const rows = Object.keys(groups).sort((a, b) => { + const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0); + const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0); + return ia - ib; + }).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 = `
${room ? room.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(".zone, .tile").forEach((el) => + el.addEventListener("click", () => { + this._selected = this._selected === el.dataset.a ? null : el.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 12; } +} + +customElements.define("rooms-card", RoomsCard);