Fix device discovery to use HA entity registry instead of entity ID patterns
Entity IDs don't contain "perfectdraft" — they use the user-chosen device name (e.g., sensor.balcony_tap_1). The editor and card now use hass.entities registry to find devices by platform === "perfectdraft" and resolve sensors by their translation_key (temperature, keg_remaining, keg_freshness) instead of entity ID suffixes. Made-with: Cursor
This commit is contained in:
Vendored
+3
-3
File diff suppressed because one or more lines are too long
+14
-16
@@ -8,7 +8,7 @@ import { getAllBeers } from "./beer-catalog.js";
|
||||
interface DiscoveredDevice {
|
||||
deviceId: string;
|
||||
name: string;
|
||||
entityIds: string[];
|
||||
entities: Map<string, string>;
|
||||
}
|
||||
|
||||
@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<string, any> = this.hass.entities || {};
|
||||
const deviceReg: Record<string, any> = this.hass.devices || {};
|
||||
const deviceMap = new Map<string, DiscoveredDevice>();
|
||||
|
||||
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()];
|
||||
|
||||
+18
-19
@@ -78,13 +78,15 @@ export class PerfectDraftCard extends LitElement {
|
||||
}
|
||||
|
||||
public static getStubConfig(hass: any): Record<string, unknown> {
|
||||
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<string, any> = hass.entities || {};
|
||||
const deviceIds = new Set<string>();
|
||||
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<string, any> = 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user