import { LitElement, html, css, nothing, type CSSResultGroup, type TemplateResult } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import type { PerfectDraftCardConfig } from "./types.js"; import { GLASS_SIZES, DEFAULT_GLASS_SIZE, DOMAIN, LAYOUTS, DEFAULT_LAYOUT } from "./const.js"; import { getAllBeers, getBeerByKegId } from "./beer-catalog.js"; const EDITOR_VERSION = "0.4.2"; interface DiscoveredDevice { deviceId: string; name: string; entities: Map; } @customElement("perfectdraft-card-editor") export class PerfectDraftCardEditor extends LitElement { @property({ attribute: false }) public hass: any; @state() private _config!: PerfectDraftCardConfig; @state() private _devices: DiscoveredDevice[] = []; public setConfig(config: PerfectDraftCardConfig): void { this._config = { ...config }; } updated(changedProps: Map): void { super.updated(changedProps); if (changedProps.has("hass") && this.hass) { this._discoverDevices(); } } private _discoverDevices(): void { if (!this.hass) return; const entityReg: Record = this.hass.entities || {}; const deviceReg: Record = this.hass.devices || {}; const deviceMap = new Map(); for (const [entityId, entry] of Object.entries(entityReg)) { if (entry.platform !== DOMAIN) continue; const devId = entry.device_id; if (!devId) continue; if (!deviceMap.has(devId)) { const device = deviceReg[devId]; const name = device?.name_by_user || device?.name || devId; deviceMap.set(devId, { deviceId: devId, name, entities: new Map() }); } const key = entry.translation_key || entry.original_name?.toLowerCase()?.replace(/\s+/g, "_") || entityId; deviceMap.get(devId)!.entities.set(key, entityId); } this._devices = [...deviceMap.values()]; if (this._devices.length === 1 && !this._config.device_id) { this._updateConfig("device_id", this._devices[0].deviceId); } } /** The product ID the configured device reports right now, if any. */ private _currentProductId(): string | undefined { const device = this._devices.find((d) => d.deviceId === this._config?.device_id); const entityId = device?.entities.get("keg_product_id"); if (!entityId) return undefined; const state = this.hass?.states[entityId]?.state; return state && state !== "unavailable" && state !== "unknown" ? state : undefined; } /** Writes or clears one correction, keyed by the keg it corrects. */ private _setOverride(productId: string, beerName: string): void { const next = { ...(this._config.beer_overrides ?? {}) }; if (beerName) { next[productId] = beerName; } else { delete next[productId]; } this._updateConfig("beer_overrides", Object.keys(next).length > 0 ? next : undefined); } private _updateConfig(key: string, value: unknown): void { this._config = { ...this._config, [key]: value }; const event = new CustomEvent("config-changed", { detail: { config: this._config }, bubbles: true, composed: true, }); this.dispatchEvent(event); } /** * A correction is recorded against the product ID currently in the machine, so * it stops applying when that keg is swapped. Saved corrections are listed so * an entry left over from an earlier keg is visible and removable. */ private _renderOverrideSection(): TemplateResult { const productId = this._currentProductId(); const active = productId ? (this._config.beer_overrides?.[productId] ?? "") : ""; const detected = productId ? getBeerByKegId(productId) : undefined; const entries = Object.entries(this._config.beer_overrides ?? {}); return html`
Beer detection
${productId ? html` Applies only to the keg now in the machine — product ID ${productId}${detected ? `, detected as ${detected.name}` : ", not in the catalog"}. Swap kegs and it stops applying. ` : "No keg detected right now. A correction is saved against the keg it corrects, so tap a keg first."}
${entries.length > 0 ? html`
${entries.map(([id, name]) => { const catalog = getBeerByKegId(id); return html`
${name} ID ${id}${catalog ? ` · detected as ${catalog.name}` : ""}${id === productId ? " · applying now" : ""}
`; })}
Entries for kegs that are not in the machine have no effect. Remove them if you no longer want them.
` : nothing} `; } protected render(): TemplateResult { if (!this.hass) { return html`
Loading...
`; } return html`
PerfectDraft Card Editor v${EDITOR_VERSION} · ${this._devices.length} device(s) found
${this._devices.length === 0 ? html`
No PerfectDraft devices found. Please install and configure the PerfectDraft integration first.
` : html`
` }
${this._renderOverrideSection()}
Emoji matrix (advanced)
this._updateConfig("max_matrix_width", (e.target as HTMLInputElement).value || undefined)} />
`; } static get styles(): CSSResultGroup { return css` .editor { padding: 16px; } .field { margin-bottom: 16px; } .field label { display: block; font-weight: 500; margin-bottom: 4px; font-size: 0.9em; } .field select:disabled { opacity: 0.5; cursor: not-allowed; } .override-list { display: flex; flex-direction: column; gap: 6px; } .override-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--divider-color, #333); border-radius: 4px; } .override-text { flex: 1; min-width: 0; display: flex; flex-direction: column; } .override-beer { font-size: 0.95em; } .override-meta { font-size: 0.75em; opacity: 0.55; } .override-remove { flex: 0 0 auto; background: none; border: none; color: inherit; opacity: 0.6; font-size: 1em; cursor: pointer; padding: 4px 6px; border-radius: 4px; } .override-remove:hover { opacity: 1; background: rgba(255, 255, 255, 0.08); } .field select, .field input { width: 100%; padding: 8px; border: 1px solid var(--divider-color, #333); border-radius: 4px; background: var(--ha-card-background, var(--card-background-color, #1c1c1c)); color: var(--primary-text-color, #fff); font-size: 1em; box-sizing: border-box; } .version { font-size: 0.75em; opacity: 0.4; text-align: right; margin-bottom: 12px; } .hint { font-size: 0.8em; opacity: 0.55; margin-top: 4px; } .advanced-heading { font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.08em; opacity: 0.5; margin: 4px 0 8px; border-top: 1px solid var(--divider-color, #333); padding-top: 12px; } .warning { padding: 12px; background: rgba(245, 166, 35, 0.15); border-radius: 8px; color: #f5a623; margin-bottom: 16px; } `; } }