Skip to content

Commit 1467b18

Browse files
committed
Fix lambdas in entity defaults. Fixes #84
1 parent c9e1b3c commit 1467b18

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/plotly-graph-card.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,43 +204,42 @@ export class PlotlyGraph extends HTMLElement {
204204
const newConfig: Config = {
205205
hours_to_show: config.hours_to_show ?? 1,
206206
refresh_interval: config.refresh_interval ?? 0,
207-
208207
entities: config.entities.map((entityIn, entityIdx) => {
209208
if (typeof entityIn === "string") entityIn = { entity: entityIn };
210209

211-
const entityOut = merge(
210+
const entity = merge(
212211
{
213212
hovertemplate: `<b>%{customdata.name}</b><br><i>%{x}</i><br>%{y}%{customdata.unit_of_measurement}<extra></extra>`,
214213
mode: "lines",
214+
show_value: false,
215215
line: {
216216
width: 1,
217217
shape: "hv",
218218
color: colorScheme[entityIdx % colorScheme.length],
219219
},
220220
},
221221
config.defaults?.entity,
222-
entityIn,
223-
{
224-
show_value: entityIn.show_value ?? false,
225-
lambda: entityIn.lambda ? window.eval(entityIn.lambda) : undefined,
226-
}
222+
entityIn
227223
);
228-
if ("statistic" in entityOut || "period" in entityOut) {
224+
if (entity.lambda) {
225+
entity.lambda = window.eval(entity.lambda);
226+
}
227+
if ("statistic" in entity || "period" in entity) {
229228
const validStatistic = ["mean", "min", "max", "sum"].includes(
230-
entityOut.statistic || ""
229+
entity.statistic || ""
231230
);
232-
if (!validStatistic) entityOut.statistic = "mean";
231+
if (!validStatistic) entity.statistic = "mean";
233232
const validPeriod = ["5minute", "hour", "day", "month"].includes(
234-
entityOut.period || ""
233+
entity.period || ""
235234
);
236-
if (!validPeriod) entityOut.period = "hour";
235+
if (!validPeriod) entity.period = "hour";
237236
}
238-
const [oldAPI_entity, oldAPI_attribute] = entityOut.entity.split("::");
237+
const [oldAPI_entity, oldAPI_attribute] = entity.entity.split("::");
239238
if (oldAPI_attribute) {
240-
entityOut.entity = oldAPI_entity;
241-
entityOut.attribute = oldAPI_attribute;
239+
entity.entity = oldAPI_entity;
240+
entity.attribute = oldAPI_attribute;
242241
}
243-
return entityOut;
242+
return entity as EntityConfig;
244243
}),
245244
layout: merge(
246245
{

0 commit comments

Comments
 (0)