Skip to content

Commit 6cb0eaa

Browse files
authored
Only allow DETAIL_ALL events to be added into the entity list (#126)
1 parent 029029c commit 6cb0eaa

File tree

1 file changed

+83
-36
lines changed

1 file changed

+83
-36
lines changed

packages/v3/src/esp-entity-table.ts

+83-36
Original file line numberDiff line numberDiff line change
@@ -84,47 +84,17 @@ export class EntityTable extends LitElement implements RestAction {
8484
"Diagnostic",
8585
];
8686

87+
private _unknown_state_events: {[key: string]: number} = {};
88+
8789
connectedCallback() {
8890
super.connectedCallback();
89-
window.source?.addEventListener("state", (e: Event) => {
91+
92+
window.source?.addEventListener('state', (e: Event) => {
9093
const messageEvent = e as MessageEvent;
9194
const data = JSON.parse(messageEvent.data);
9295
let idx = this.entities.findIndex((x) => x.unique_id === data.id);
93-
if (idx === -1 && data.id) {
94-
// Dynamically add discovered..
95-
let parts = data.id.split("-");
96-
let entity = {
97-
...data,
98-
domain: parts[0],
99-
unique_id: data.id,
100-
id: parts.slice(1).join("-"),
101-
entity_category: data.entity_category,
102-
sorting_group: data.sorting_group ?? (EntityTable.ENTITY_CATEGORIES[parseInt(data.entity_category)] || EntityTable.ENTITY_UNDEFINED),
103-
value_numeric_history: [data.value],
104-
} as entityConfig;
105-
entity.has_action = this.hasAction(entity);
106-
if (entity.has_action) {
107-
this.has_controls = true;
108-
}
109-
this.entities.push(entity);
110-
this.entities.sort((a, b) => {
111-
const sortA = a.sorting_weight ?? a.name;
112-
const sortB = b.sorting_weight ?? b.name;
113-
return a.sorting_group < b.sorting_group
114-
? -1
115-
: a.sorting_group === b.sorting_group
116-
? sortA === sortB
117-
? a.name.toLowerCase() < b.name.toLowerCase()
118-
? -1
119-
: 1
120-
: sortA < sortB
121-
? -1
122-
: 1
123-
: 1
124-
});
125-
this.requestUpdate();
126-
} else {
127-
if (typeof data.value === "number") {
96+
if (idx != -1 && data.id) {
97+
if (typeof data.value === 'number') {
12898
let history = [...this.entities[idx].value_numeric_history];
12999
history.push(data.value);
130100
this.entities[idx].value_numeric_history = history.splice(-50);
@@ -135,6 +105,44 @@ export class EntityTable extends LitElement implements RestAction {
135105
delete data.unique_id;
136106
Object.assign(this.entities[idx], data);
137107
this.requestUpdate();
108+
} else {
109+
// is it a `detail_all` event already?
110+
if (data?.name) {
111+
this.addEntity(data);
112+
} else {
113+
if (this._unknown_state_events[data.id]) {
114+
this._unknown_state_events[data.id]++;
115+
} else {
116+
this._unknown_state_events[data.id] = 1;
117+
}
118+
// ignore the first few events, maybe the esp will send a detail_all
119+
// event soon
120+
if (this._unknown_state_events[data.id] < 1) {
121+
return;
122+
}
123+
124+
let parts = data.id.split('-');
125+
let domain = parts[0];
126+
let id = parts.slice(1).join('-');
127+
128+
fetch(`${this._basePath}/${domain}/${id}?detail=all`, {
129+
method: 'GET',
130+
})
131+
.then((r) => {
132+
console.log(r);
133+
if (!r.ok) {
134+
throw new Error(`HTTP error! Status: ${r.status}`);
135+
}
136+
return r.json();
137+
})
138+
.then((data) => {
139+
console.log(data);
140+
this.addEntity(data);
141+
})
142+
.catch((error) => {
143+
console.error('Fetch error:', error);
144+
});
145+
}
138146
}
139147
});
140148

@@ -166,6 +174,45 @@ export class EntityTable extends LitElement implements RestAction {
166174
});
167175
}
168176

177+
addEntity(data: any) {
178+
let idx = this.entities.findIndex((x) => x.unique_id === data.id);
179+
if (idx === -1 && data.id) {
180+
// Dynamically add discovered..
181+
let parts = data.id.split("-");
182+
let entity = {
183+
...data,
184+
domain: parts[0],
185+
unique_id: data.id,
186+
id: parts.slice(1).join("-"),
187+
entity_category: data.entity_category,
188+
sorting_group: data.sorting_group ?? (EntityTable.ENTITY_CATEGORIES[parseInt(data.entity_category)] || EntityTable.ENTITY_UNDEFINED),
189+
value_numeric_history: [data.value],
190+
} as entityConfig;
191+
entity.has_action = this.hasAction(entity);
192+
if (entity.has_action) {
193+
this.has_controls = true;
194+
}
195+
this.entities.push(entity);
196+
this.entities.sort((a, b) => {
197+
const sortA = a.sorting_weight ?? a.name;
198+
const sortB = b.sorting_weight ?? b.name;
199+
return a.sorting_group < b.sorting_group
200+
? -1
201+
: a.sorting_group === b.sorting_group
202+
? sortA === sortB
203+
? a.name.toLowerCase() < b.name.toLowerCase()
204+
? -1
205+
: 1
206+
: sortA < sortB
207+
? -1
208+
: 1
209+
: 1
210+
});
211+
this.requestUpdate();
212+
}
213+
214+
}
215+
169216
hasAction(entity: entityConfig): boolean {
170217
return `render_${entity.domain}` in this._actionRenderer;
171218
}

0 commit comments

Comments
 (0)