{this._showBeerDialog=!0}}
@@ -453,4 +453,4 @@ function e(e,t,r,s){var i,a=arguments.length,o=a<3?t:null===s?s=Object.getOwnPro
padding: 16px;
color: var(--error-color, #db4437);
}
- `}};e([pe({attribute:!1})],Ce.prototype,"hass",void 0),e([ge()],Ce.prototype,"_config",void 0),e([ge()],Ce.prototype,"_beer",void 0),e([ge()],Ce.prototype,"_glassSize",void 0),e([ge()],Ce.prototype,"_showGlassDialog",void 0),e([ge()],Ce.prototype,"_showBeerDialog",void 0),e([ge()],Ce.prototype,"_beerSearchQuery",void 0),Ce=e([ce("perfectdraft-card")],Ce),window.customCards=window.customCards||[],window.customCards.push({type:"perfectdraft-card",name:"PerfectDraft Card",description:"Display PerfectDraft Pro keg status with beer emojis",preview:!0,documentationURL:"https://github.com/Falkvinge/hassio-component-perfectdraft-pro"}),console.info("%c PERFECTDRAFT-CARD %c v0.1.0 ","background: #f5a623; color: #000; font-weight: bold;","background: #333; color: #f5a623;");export{Ce as PerfectDraftCard};
+ `}};e([pe({attribute:!1})],ke.prototype,"hass",void 0),e([ge()],ke.prototype,"_config",void 0),e([ge()],ke.prototype,"_beer",void 0),e([ge()],ke.prototype,"_glassSize",void 0),e([ge()],ke.prototype,"_showGlassDialog",void 0),e([ge()],ke.prototype,"_showBeerDialog",void 0),e([ge()],ke.prototype,"_beerSearchQuery",void 0),ke=e([ce("perfectdraft-card")],ke),window.customCards=window.customCards||[],window.customCards.push({type:"perfectdraft-card",name:"PerfectDraft Card",description:"Display PerfectDraft Pro keg status with beer emojis",preview:!0,documentationURL:"https://github.com/Falkvinge/hassio-component-perfectdraft-pro"}),console.info("%c PERFECTDRAFT-CARD %c v0.1.0 ","background: #f5a623; color: #000; font-weight: bold;","background: #333; color: #f5a623;");export{ke as PerfectDraftCard};
diff --git a/src/editor.ts b/src/editor.ts
index bff2a0d..2c55dca 100644
--- a/src/editor.ts
+++ b/src/editor.ts
@@ -8,7 +8,7 @@ import { getAllBeers } from "./beer-catalog.js";
interface DiscoveredDevice {
deviceId: string;
name: string;
- entityIds: string[];
+ entities: Map;
}
@customElement("perfectdraft-card-editor")
@@ -31,25 +31,23 @@ export class PerfectDraftCardEditor extends LitElement {
private _discoverDevices(): void {
if (!this.hass) return;
- const pdEntities = Object.keys(this.hass.states).filter(
- (e) => e.startsWith("sensor.") && e.includes(DOMAIN),
- );
-
+ const entityReg: Record = this.hass.entities || {};
+ const deviceReg: Record = this.hass.devices || {};
const deviceMap = new Map();
- for (const eid of pdEntities) {
- const match = eid.match(/^sensor\.(.+?)_(?:temperature|keg_remaining|keg_freshness|connection|door|pours|last_pour|firmware|mode)$/);
- if (!match) continue;
+ for (const [entityId, entry] of Object.entries(entityReg)) {
+ if (entry.platform !== DOMAIN) continue;
+ const devId = entry.device_id;
+ if (!devId) continue;
- const prefix = match[1];
- if (!deviceMap.has(prefix)) {
- deviceMap.set(prefix, {
- deviceId: prefix,
- name: prefix.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()),
- entityIds: [],
- });
+ 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() });
}
- deviceMap.get(prefix)!.entityIds.push(eid);
+
+ const key = entry.translation_key || entry.original_name?.toLowerCase()?.replace(/\s+/g, "_") || entityId;
+ deviceMap.get(devId)!.entities.set(key, entityId);
}
this._devices = [...deviceMap.values()];
diff --git a/src/perfectdraft-card.ts b/src/perfectdraft-card.ts
index a05a754..3a67143 100644
--- a/src/perfectdraft-card.ts
+++ b/src/perfectdraft-card.ts
@@ -78,13 +78,15 @@ export class PerfectDraftCard extends LitElement {
}
public static getStubConfig(hass: any): Record {
- const entities = Object.keys(hass.states).filter(
- (e) => e.startsWith("sensor.") && e.includes("perfectdraft"),
- );
- if (entities.length > 0) {
- return { device_id: "", beer_name: "Stella Artois", glass_size: DEFAULT_GLASS_SIZE };
+ const entityReg: Record = hass.entities || {};
+ const deviceIds = new Set();
+ for (const entry of Object.values(entityReg)) {
+ if (entry.platform === DOMAIN && entry.device_id) {
+ deviceIds.add(entry.device_id);
+ }
}
- return { device_id: "", beer_name: "Stella Artois", glass_size: DEFAULT_GLASS_SIZE };
+ const firstDevice = deviceIds.size === 1 ? [...deviceIds][0] : "";
+ return { device_id: firstDevice, beer_name: "Stella Artois", glass_size: DEFAULT_GLASS_SIZE };
}
public static getConfigElement(): HTMLElement {
@@ -108,21 +110,18 @@ export class PerfectDraftCard extends LitElement {
if (this._entityIds.temperature) return;
- const allEntities = Object.keys(this.hass.states);
- const pdEntities = allEntities.filter(
- (e) => e.startsWith("sensor.") && e.includes(DOMAIN),
- );
+ const entityReg: Record = this.hass.entities || {};
- for (const eid of pdEntities) {
- const entityObj = this.hass.states[eid];
- if (!entityObj) continue;
+ for (const [entityId, entry] of Object.entries(entityReg)) {
+ if (entry.platform !== DOMAIN || entry.device_id !== this._config.device_id) continue;
- if (eid.endsWith("_temperature")) {
- this._entityIds.temperature = eid;
- } else if (eid.endsWith("_keg_remaining")) {
- this._entityIds.kegRemaining = eid;
- } else if (eid.endsWith("_keg_freshness")) {
- this._entityIds.kegFreshness = eid;
+ const key = entry.translation_key;
+ if (key === "temperature") {
+ this._entityIds.temperature = entityId;
+ } else if (key === "keg_remaining") {
+ this._entityIds.kegRemaining = entityId;
+ } else if (key === "keg_freshness") {
+ this._entityIds.kegFreshness = entityId;
}
}
}