generated from custom-cards/boilerplate-card
/
defaults.ts
218 lines (210 loc) · 5.78 KB
/
defaults.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { merge } from "lodash";
import { Config, InputConfig } from "../types";
import { parseColorScheme } from "./parse-color-scheme";
import { getEntityIndex } from "./parse-config";
import getThemedLayout, { HATheme } from "./themed-layout";
const noop$fn = () => () => {};
const defaultEntityRequired = {
entity: "",
show_value: false,
internal: false,
time_offset: "0s",
on_legend_click: noop$fn,
on_legend_dblclick: noop$fn,
on_click: noop$fn,
};
const defaultEntityOptional = {
mode: "lines",
line: {
width: 1,
shape: "hv",
color: ({ getFromConfig, path }) => {
const color_scheme = parseColorScheme(getFromConfig("color_scheme"));
return color_scheme[getEntityIndex(path) % color_scheme.length];
},
},
// extend_to_present: true unless using statistics. Defined inside parse-config.ts to avoid forward depndency
unit_of_measurement: ({ meta }) => meta.unit_of_measurement || "",
name: ({ meta, getFromConfig }) => {
let name = meta.friendly_name || getFromConfig(`.entity`);
const attribute = getFromConfig(`.attribute`);
if (attribute) name += ` (${attribute}) `;
return name;
},
hovertemplate: ({ getFromConfig }) =>
`<b>${getFromConfig(".name")}</b><br><i>%{x}</i><br>%{y} ${getFromConfig(
".unit_of_measurement"
)}<extra></extra>`,
yaxis: ({ getFromConfig, path }) => {
const units: string[] = [];
for (let i = 0; i <= getEntityIndex(path); i++) {
const unit = getFromConfig(`entities.${i}.unit_of_measurement`);
const internal = getFromConfig(`entities.${i}.internal`);
if (!internal && !units.includes(unit)) units.push(unit);
}
const yaxis_idx = units.length;
return "y" + (yaxis_idx === 1 ? "" : yaxis_idx);
},
};
const defaultYamlRequired = {
title: "",
hours_to_show: 1,
refresh_interval: "auto",
color_scheme: "category10",
time_offset: "0s",
raw_plotly_config: false,
ha_theme: true,
disable_pinch_to_zoom: false,
raw_plotly: false,
defaults: {
entity: {},
yaxes: {},
},
layout: {},
on_dblclick: noop$fn,
autorange_after_scroll: false,
};
//
const defaultExtraYAxes: Partial<Plotly.LayoutAxis> = {
// automargin: true, // it makes zooming very jumpy
side: "right",
overlaying: "y",
showgrid: false,
visible: false,
// This makes sure that the traces are rendered above the right y axis,
// including the marker and its text. Useful for show_value. See cliponaxis in entity
layer: "below traces",
};
const defaultYamlOptional: {
layout: Partial<Plotly.Layout>;
config: Partial<Plotly.Config>;
} = {
config: {
displaylogo: false,
scrollZoom: true,
modeBarButtonsToRemove: ["resetScale2d", "toImage", "lasso2d", "select2d"],
},
layout: {
height: 285,
dragmode: "pan",
xaxis: {
autorange: false,
type: "date",
// automargin: true, // it makes zooming very jumpy
},
yaxis: {
// automargin: true, // it makes zooming very jumpy
},
yaxis2: {
// automargin: true, // it makes zooming very jumpy
...defaultExtraYAxes,
visible: true,
},
...Object.fromEntries(
Array.from({ length: 27 }).map((_, i) => [
`yaxis${i + 3}`,
{ ...defaultExtraYAxes },
])
),
legend: {
orientation: "h",
bgcolor: "transparent",
x: 0,
y: 1,
yanchor: "bottom",
},
title: {
y: 1,
pad: {
t: 15,
},
},
modebar: {
// vertical so it doesn't occlude the legend
orientation: "v",
},
margin: {
b: 50,
t: 0,
l: 60,
// @ts-expect-error functions are not a plotly thing, only this card
r: ({ getFromConfig }) => {
const entities = getFromConfig(`entities`);
const usesRightAxis = entities.some(({ yaxis }) => yaxis === "y2");
const usesShowValue = entities.some(({ show_value }) => show_value);
return usesRightAxis | usesShowValue ? 60 : 30;
},
},
},
};
export function addPreParsingDefaults(
yaml_in: InputConfig,
css_vars: HATheme
): InputConfig {
// merging in two steps to ensure ha_theme and raw_plotly_config took its default value
let yaml = merge({}, yaml_in, defaultYamlRequired, yaml_in);
for (let i = 1; i < 31; i++) {
const yaxis = "yaxis" + (i == 1 ? "" : i);
yaml.layout[yaxis] = merge(
{},
yaml.layout[yaxis],
yaml.defaults.yaxes,
yaml.layout[yaxis]
);
}
yaml = merge(
{},
yaml,
{
layout: yaml.ha_theme ? getThemedLayout(css_vars) : {},
},
yaml.raw_plotly_config ? {} : defaultYamlOptional,
yaml
);
yaml.entities = yaml.entities.map((entity) => {
if (typeof entity === "string") entity = { entity };
entity.entity ??= "";
const [oldAPI_entity, oldAPI_attribute] = entity.entity.split("::");
if (oldAPI_attribute) {
entity.entity = oldAPI_entity;
entity.attribute = oldAPI_attribute;
}
entity = merge(
{},
entity,
defaultEntityRequired,
yaml.raw_plotly_config ? {} : defaultEntityOptional,
yaml.defaults?.entity,
entity
);
return entity;
});
return yaml;
}
export function addPostParsingDefaults(
yaml: Config & { visible_range: [number, number] }
): Config {
/**
* These cannot be done via defaults because they depend on the entities already being fully evaluated and filtered
* */
const yAxisTitles = Object.fromEntries(
yaml.entities.map(({ unit_of_measurement, yaxis }) => [
"yaxis" + yaxis?.slice(1),
{ title: unit_of_measurement },
])
);
const layout = merge(
{},
yaml.layout,
yaml.raw_plotly_config
? {}
: {
xaxis: {
range: yaml.visible_range,
},
},
yaml.raw_plotly_config ? {} : yAxisTitles,
yaml.layout
);
return merge({}, yaml, { layout }, yaml);
}