@@ -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 ( / ^ d e f a u l t s $ / ) ) 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 ( / ^ e n t i t i e s \. \d $ / ) ) this . fnParam . entityIdx = key ;
137137 if (
138138 path . match ( / ^ e n t i t i e s \. \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 ( / e n t i t i e s \. ( \d + ) / ) ! [ 1 ] ;
411419export { ConfigParser } ;
0 commit comments