Skip to content

Commit f72a37d

Browse files
committed
docs and better css_vars for ha_theme
1 parent c53da6c commit f72a37d

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

readme.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ entities:
481481
# either statistics or states will be available, depending on if "statistics" are fetched or not
482482
# attributes will be available inside states only if an attribute is picked in the trace
483483
return {
484-
ys: states.map(state => +state?.attributes?.current_temperature - state?.attributes?.target_temperature + hass.states.get("sensor.inside_temp")),
484+
ys: states.map(state => +state?.attributes?.current_temperature - state?.attributes?.target_temperature + hass.states["sensor.temperature"].state,
485485
meta: { unit_of_measurement: "delta" }
486486
};
487487
},
@@ -688,6 +688,32 @@ entities:
688688

689689
Javascript functions allowed everywhere in the yaml. Evaluation is top to bottom and shallow to deep (depth first traversal).
690690

691+
The returned value will be used as value for the property where it is found. E.g:
692+
693+
```js
694+
name: $fn ({ hass }) => hass.states["sensor.garden_temperature"].state
695+
```
696+
697+
### Available parameters:
698+
699+
Remember you can add a `console.log(the_object_you_want_to_inspect)` and see its content in the devTools console.
700+
701+
#### Everywhere:
702+
703+
- `getFromConfig: (path) => value;` Pass a path (e.g `entities.0.name`) and get back its value
704+
- `hass: HomeAssistant object;` For example: `hass.states["sensor.garden_temperature"].state` to get its current state
705+
- `vars: Record<string, any>;` You can communicate between functions with this. E.g `vars.temperatures = ys`
706+
- `path: string;` The path of the current function
707+
- `css_vars: HATheme;` The colors set by the active Home Assistant theme (see #ha_theme)
708+
709+
#### Only iniside entities
710+
711+
- `xs: Date[];` Array of timestamps
712+
- `ys: YValue[];` Array of values of the sensor/attribute/statistic
713+
- `statistics: StatisticValue[];` Array of statistics objects
714+
- `states: HassEntity[];` Array of state objects
715+
- `meta: HassEntity["attributes"];` The current attributes of the sensor
716+
691717
#### Gotchas
692718

693719
- The following entity attributes are required for fetching, so if another function needs the entity data it needs to be declared below them. `entity`,`attribute`,`offset`,`statistic`,`period`
@@ -791,7 +817,13 @@ Anything from https://plotly.com/javascript/reference/layout/.
791817

792818
### Home Assistant theming:
793819

794-
Toggle Home Assistant theme colors
820+
Toggle Home Assistant theme colors:
821+
822+
- card-background-color
823+
- primary-background-color
824+
- primary-color
825+
- primary-text-color
826+
- secondary-text-color
795827

796828
```yaml
797829
type: custom:plotly-graph

src/parse-config/parse-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { HATheme } from "./themed-layout";
33

44
import propose from "propose";
55

6-
import merge from "lodash/merge";
76
import get from "lodash/get";
87
import { addPreParsingDefaults, addPostParsingDefaults } from "./defaults";
98
import { parseTimeDuration } from "../duration/duration";

src/parse-config/themed-layout.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export type HATheme = {
2-
"--card-background-color": string;
3-
"--primary-background-color": string;
4-
"--primary-color": string;
5-
"--primary-text-color": string;
6-
"--secondary-text-color": string;
2+
"card-background-color": string;
3+
"primary-background-color": string;
4+
"primary-color": string;
5+
"primary-text-color": string;
6+
"secondary-text-color": string;
77
};
88

99
const defaultExtraYAxes: Partial<Plotly.LayoutAxis> = {
@@ -98,10 +98,10 @@ export default function getThemedLayout(
9898
haTheme: HATheme
9999
): Partial<Plotly.Layout> {
100100
return {
101-
paper_bgcolor: haTheme["--card-background-color"],
102-
plot_bgcolor: haTheme["--card-background-color"],
101+
paper_bgcolor: haTheme["card-background-color"],
102+
plot_bgcolor: haTheme["card-background-color"],
103103
font: {
104-
color: haTheme["--secondary-text-color"],
104+
color: haTheme["secondary-text-color"],
105105
size: 11,
106106
},
107107
xaxis: { ...themeAxisStyle },

src/plotly-graph-card.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ export class PlotlyGraph extends HTMLElement {
289289
getCSSVars() {
290290
const styles = window.getComputedStyle(this.contentEl);
291291
let haTheme = {
292-
"--card-background-color": "red",
293-
"--primary-background-color": "red",
294-
"--primary-color": "red",
295-
"--primary-text-color": "red",
296-
"--secondary-text-color": "red",
292+
"card-background-color": "red",
293+
"primary-background-color": "red",
294+
"primary-color": "red",
295+
"primary-text-color": "red",
296+
"secondary-text-color": "red",
297297
};
298-
return mapValues(haTheme, (_, key) => styles.getPropertyValue(key));
298+
return mapValues(haTheme, (_, key) => styles.getPropertyValue("--" + key));
299299
}
300300
fetchScheduled = false;
301301
plot = async (

0 commit comments

Comments
 (0)