11import Cache from "../cache/Cache" ;
2- import getThemedLayout , { defaultLayout , HATheme } from "./themed-layout" ;
2+ import { HATheme } from "./themed-layout" ;
33
44import propose from "propose" ;
55
66import merge from "lodash/merge" ;
77import get from "lodash/get" ;
8- import {
9- defaultEntityRequired ,
10- defaultEntityOptional ,
11- defaultYamlRequired ,
12- defaultYamlOptional ,
13- } from "./defaults" ;
8+ import { addPreParsingDefaults , addPostParsingDefaults } from "./defaults" ;
149import { parseTimeDuration } from "../duration/duration" ;
1510import { parseStatistics } from "./parse-statistics" ;
1611import { HomeAssistant } from "custom-card-helpers" ;
1712import filters from "../filters/filters" ;
1813import bounds from "binary-search-bounds" ;
1914import { has } from "lodash" ;
2015import { StatisticValue } from "../recorder-types" ;
21- import { Config , HassEntity , YValue } from "../types" ;
16+ import { Config , HassEntity , InputConfig , YValue } from "../types" ;
2217
2318class ConfigParser {
2419 private yaml ?: any ;
@@ -29,7 +24,7 @@ class ConfigParser {
2924 private busy = false ;
3025 private fnParam ! : FnParam ;
3126
32- async update ( input : { yaml : any ; hass : HomeAssistant } ) {
27+ async update ( input : { yaml : any ; hass : HomeAssistant ; css_vars : HATheme } ) {
3328 if ( this . busy ) throw new Error ( "ParseConfig was updated while busy" ) ;
3429 this . busy = true ;
3530 try {
@@ -38,62 +33,30 @@ class ConfigParser {
3833 this . busy = false ;
3934 }
4035 }
41- private async _update ( input : {
36+ private async _update ( {
37+ yaml : input_yaml ,
38+ hass,
39+ css_vars,
40+ } : {
4241 yaml : any ;
4342 hass : HomeAssistant ;
43+ css_vars : HATheme ;
4444 } ) : Promise < { errors : Error [ ] ; parsed : Config } > {
45+ const old_uirevision = this . yaml ?. layout ?. uirevision ;
4546 this . yaml = { } ;
4647 this . errors = [ ] ;
47-
48- this . hass = input . hass ;
49- const old_uirevision = this . yaml ?. layout ?. uirevision ;
50- this . yaml_with_defaults = JSON . parse ( JSON . stringify ( input . yaml ) ) ;
51-
52- // 1st pass: add defaults
53- this . yaml_with_defaults = merge (
54- { } ,
55- this . yaml_with_defaults ,
56- defaultYamlRequired ,
57- this . yaml_with_defaults . raw_plotly_config ? { } : defaultYamlOptional ,
58- this . yaml_with_defaults
59- ) ;
60- for ( let i = 1 ; i < 31 ; i ++ ) {
61- const yaxis = "yaxis" + ( i == 1 ? "" : i ) ;
62- this . yaml_with_defaults . layout [ yaxis ] = merge (
63- { } ,
64- this . yaml_with_defaults . layout [ yaxis ] ,
65- this . yaml_with_defaults . defaults ?. yaxes ,
66- this . yaml_with_defaults . layout [ yaxis ]
67- ) ;
68- }
69- this . yaml_with_defaults . entities = this . yaml_with_defaults . entities . map (
70- ( entity ) => {
71- if ( typeof entity === "string" ) entity = { entity } ;
72- entity . entity ??= "" ;
73- const [ oldAPI_entity , oldAPI_attribute ] = entity . entity . split ( "::" ) ;
74- if ( oldAPI_attribute ) {
75- entity . entity = oldAPI_entity ;
76- entity . attribute = oldAPI_attribute ;
77- }
78- entity = merge (
79- { } ,
80- entity ,
81- defaultEntityRequired ,
82- this . yaml_with_defaults . raw_plotly_config
83- ? { }
84- : defaultEntityOptional ,
85- this . yaml_with_defaults . defaults ?. entity ,
86- entity
87- ) ;
88- return entity ;
89- }
90- ) ;
48+ this . hass = hass ;
49+ this . yaml_with_defaults = addPreParsingDefaults ( input_yaml ) ;
50+ const isBrowsing = ! ! input_yaml . visible_range ;
51+ console . log ( "isBrowsing" , isBrowsing ) ;
9152
9253 // 2nd pass: evaluate functions
54+
9355 this . fnParam = {
9456 vars : { } ,
9557 path : "" ,
96- hass : input . hass ,
58+ hass,
59+ css_vars,
9760 getFromConfig : ( ) => "" ,
9861 } ;
9962 for ( const [ key , value ] of Object . entries ( this . yaml_with_defaults ) ) {
@@ -109,35 +72,13 @@ class ConfigParser {
10972 this . errors ?. push ( e as Error ) ;
11073 }
11174 }
112-
113- // 3rd pass: decorate
114- /**
115- * These cannot be done via defaults because they are functions and
116- * functions would be overwritten if the user sets a configuration on a parent
117- * */
118- const isBrowsing = ! ! input . yaml . visible_range ;
119- const yAxisTitles = Object . fromEntries (
120- this . yaml . entities . map ( ( { unit_of_measurement, yaxis } ) => [
121- "yaxis" + yaxis ?. slice ( 1 ) ,
122- { title : unit_of_measurement } ,
123- ] )
124- ) ;
125- merge (
126- this . yaml . layout ,
127- this . yaml . raw_plotly_config ? { } : defaultLayout ,
128- this . yaml . ha_theme ? getThemedLayout ( this . yaml . css_vars ) : { } ,
129- this . yaml . raw_plotly_config
130- ? { }
131- : {
132- xaxis : {
133- range : this . yaml . visible_range ,
134- } ,
135- //changing the uirevision triggers a reset to the axes
136- uirevision : isBrowsing ? old_uirevision : Math . random ( ) ,
137- } ,
138- this . yaml . raw_plotly_config ? { } : yAxisTitles ,
139- this . yaml . layout
140- ) ;
75+ //TODO: mutates
76+ this . yaml = addPostParsingDefaults ( {
77+ yaml : this . yaml ,
78+ isBrowsing,
79+ old_uirevision,
80+ css_vars,
81+ } ) ;
14182
14283 return { errors : this . errors , parsed : this . yaml } ;
14384 }
@@ -158,11 +99,11 @@ class ConfigParser {
15899 this . getEvaledPath ( pathQuery , path /* caller */ ) ;
159100
160101 if (
161- path . match ( / ^ e n t i t i e s \. \d + \. / ) && //isInsideEntity
102+ ! this . fnParam . xs && // hasn't fetched yet
103+ path . match ( / ^ e n t i t i e s \. \d + \. / ) &&
162104 ! path . match (
163105 / ^ e n t i t i e s \. \d + \. ( e n t i t y | a t t r i b u t e | t i m e _ o f f s e t | s t a t i s t i c | p e r i o d ) /
164106 ) && //isInsideFetchParamNode
165- ! this . fnParam . xs && // alreadyFetchedData
166107 ( is$fn ( value ) || path . match ( / ^ e n t i t i e s \. \d + \. f i l t e r s \. \d + $ / ) ) // if function of filter
167108 )
168109 await this . fetchDataForEntity ( path ) ;
@@ -430,6 +371,7 @@ type FnParam = {
430371 hass : HomeAssistant ;
431372 vars : Record < string , any > ;
432373 path : string ;
374+ css_vars : HATheme ;
433375 xs ?: Date [ ] ;
434376 ys ?: YValue [ ] ;
435377 statistics ?: StatisticValue [ ] ;
0 commit comments