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."}