Skip to content

Commit 33012bc

Browse files
committed
pass path instead of entityIdx as fnParam
1 parent 5a93ed9 commit 33012bc

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

src/parse-config/defaults.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseColorScheme } from "./parse-color-scheme";
2+
import { getEntityIndex } from "./parse-config";
23

34
export const defaultEntity = {
45
entity: "",
@@ -8,31 +9,28 @@ export const defaultEntity = {
89
line: {
910
width: 1,
1011
shape: "hv",
11-
color: ({ getFromConfig, entityIdx }) => {
12+
color: ({ getFromConfig, path }) => {
1213
const color_scheme = parseColorScheme(getFromConfig("color_scheme"));
13-
return color_scheme[entityIdx % color_scheme.length];
14+
return color_scheme[getEntityIndex(path) % color_scheme.length];
1415
},
1516
},
1617
internal: false,
1718
offset: "0s",
1819
// extend_to_present: true unless using statistics. Defined inside parse-config.ts to avoid forward depndency
1920
unit_of_measurement: ({ meta }) => meta.unit_of_measurement || "",
20-
name: ({ meta, entityIdx, getFromConfig }) => {
21-
let name =
22-
meta.friendly_name || getFromConfig(`entities.${entityIdx}.entity`);
23-
const attribute = getFromConfig(`entities.${entityIdx}.attribute`);
21+
name: ({ meta, getFromConfig }) => {
22+
let name = meta.friendly_name || getFromConfig(`.entity`);
23+
const attribute = getFromConfig(`.attribute`);
2424
if (attribute) name += ` (${attribute}) `;
2525
return name;
2626
},
27-
hovertemplate: ({ getFromConfig, entityIdx }) =>
28-
`<b>${getFromConfig(
29-
`entities.${entityIdx}.name`
30-
)}</b><br><i>%{x}</i><br>%{y} ${getFromConfig(
31-
`entities.${entityIdx}.unit_of_measurement`
27+
hovertemplate: ({ getFromConfig }) =>
28+
`<b>${getFromConfig(".name")}</b><br><i>%{x}</i><br>%{y} ${getFromConfig(
29+
".unit_of_measurement"
3230
)}<extra></extra>`,
33-
yaxis: ({ getFromConfig, entityIdx }) => {
31+
yaxis: ({ getFromConfig, path }) => {
3432
const units: string[] = [];
35-
for (let i = 0; i <= entityIdx; i++) {
33+
for (let i = 0; i <= getEntityIndex(path); i++) {
3634
const unit = getFromConfig(`entities.${i}.unit_of_measurement`);
3735
const internal = getFromConfig(`entities.${i}.internal`);
3836
if (!internal && !units.includes(unit)) units.push(unit);
@@ -65,7 +63,8 @@ export const defaultYaml = {
6563
r: ({ getFromConfig }) => {
6664
const entities = getFromConfig(`entities`);
6765
const usesRightAxis = entities.some(({ yaxis }) => yaxis === "y2");
68-
return usesRightAxis ? 60 : 30;
66+
const usesShowValue = entities.some(({ show_value }) => show_value);
67+
return usesRightAxis | usesShowValue ? 60 : 30;
6968
},
7069
},
7170
},

src/parse-config/parse-config.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class ConfigParser {
6565
// 2nd pass: evaluate functions
6666
this.fnParam = {
6767
vars: {},
68+
path: "",
6869
hass: input.hass,
69-
entityIdx: "",
7070
getFromConfig: () => "",
7171
};
7272
this.partiallyParsedConfig = {};
@@ -130,10 +130,10 @@ class ConfigParser {
130130
}) {
131131
errorIfDeprecated(path);
132132
if (path.match(/^defaults$/)) return;
133-
this.fnParam.getFromConfig = (aPath: string) =>
134-
this.getEvaledPath({ path: aPath, callingPath: path });
133+
this.fnParam.path = path;
134+
this.fnParam.getFromConfig = (pathQuery: string) =>
135+
this.getEvaledPath(pathQuery, path /* caller */);
135136

136-
if (path.match(/^entities\.\d$/)) this.fnParam.entityIdx = key;
137137
if (
138138
path.match(/^entities\.\d+\./) && //isInsideEntity
139139
!path.match(
@@ -270,9 +270,10 @@ class ConfigParser {
270270
visible_range[1] - offset,
271271
];
272272
const fetch_mask = this.fnParam.getFromConfig("fetch_mask");
273+
const i = getEntityIndex(path);
273274
const data =
274275
// TODO: decide about minimal response
275-
fetch_mask[this.fnParam.entityIdx] === false // also fetch if it is undefined. This means the entity is new
276+
fetch_mask[i] === false // also fetch if it is undefined. This means the entity is new
276277
? this.cache.getData(fetchConfig)
277278
: await this.cache.fetch(range_to_fetch, fetchConfig, this.hass!);
278279
const extend_to_present =
@@ -300,17 +301,23 @@ class ConfigParser {
300301
public resetObservedRange() {
301302
this.observed_range = [Date.now(), Date.now()];
302303
}
303-
private getEvaledPath(p: { path: string; callingPath: string }) {
304-
if (has(this.partiallyParsedConfig, p.path))
305-
return get(this.partiallyParsedConfig, p.path);
304+
private getEvaledPath(path: string, callingPath: string) {
305+
if (path.startsWith("."))
306+
path = callingPath
307+
.split(".")
308+
.slice(0, -1)
309+
.concat(path.slice(1).split("."))
310+
.join(".");
311+
if (has(this.partiallyParsedConfig, path))
312+
return get(this.partiallyParsedConfig, path);
306313

307314
let value = this.inputConfig;
308-
for (const key of p.path.split(".")) {
315+
for (const key of path.split(".")) {
309316
if (value === undefined) return undefined;
310317
value = value[key];
311318
if (is$fn(value)) {
312319
throw new Error(
313-
`Since [${p.path}] is a $fn, it has to be defined before [${p.callingPath}]`
320+
`Since [${path}] is a $fn, it has to be defined before [${callingPath}]`
314321
);
315322
}
316323
}
@@ -387,7 +394,7 @@ type FnParam = {
387394
) => ReturnType<InstanceType<typeof ConfigParser>["getEvaledPath"]>;
388395
hass: HomeAssistant;
389396
vars: Record<string, any>;
390-
entityIdx: string;
397+
path: string;
391398
xs?: Date[];
392399
ys?: YValue[];
393400
statistics?: StatisticValue[];
@@ -407,5 +414,6 @@ function errorIfDeprecated(path: string) {
407414
"minimal_response was removed, if you need attributes use the 'attribute' parameter instead."
408415
);
409416
}
410-
417+
export const getEntityIndex = (path: string) =>
418+
+path.match(/entities\.(\d+)/)![1];
411419
export { ConfigParser };

0 commit comments

Comments
 (0)