From ab7d16f184ab5c337fa148fb12d9c050625f5880 Mon Sep 17 00:00:00 2001 From: Nerwyn Singh Date: Sat, 21 Sep 2024 07:23:01 -0400 Subject: [PATCH] disable _states, may be causing performance issues --- dist/index.js | 3 +- src/index.ts | 3 +- tests/renderTemplate.test.ts | 4 +- tests/utils/distance.test.ts | 114 ++++++++++++------------ tests/utils/groups.test.ts | 168 +++++++++++++++++------------------ tests/utils/states.test.ts | 16 ++-- 6 files changed, 153 insertions(+), 155 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2d12d16..c703d31 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2,7 +2,6 @@ import nunjucks from 'nunjucks'; import { addFilters } from './filters'; import { addGlobals } from './globals'; import { addTests } from './tests'; -import { buildStatesObject } from './utils/states'; nunjucks.installJinjaCompat(); const env = addTests(addFilters(addGlobals(new nunjucks.Environment()))); /** @@ -19,7 +18,7 @@ export function renderTemplate(hass, str, context) { str = env .renderString(structuredClone(str), { hass, - _states: buildStatesObject(hass), + // _states: buildStatesObject(hass), ...context, }) .trim(); diff --git a/src/index.ts b/src/index.ts index ad9ed84..e059194 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import nunjucks from 'nunjucks'; import { addFilters } from './filters'; import { addGlobals } from './globals'; import { addTests } from './tests'; -import { buildStatesObject } from './utils/states'; nunjucks.installJinjaCompat(); const env = addTests(addFilters(addGlobals(new nunjucks.Environment()))); @@ -29,7 +28,7 @@ export function renderTemplate( str = env .renderString(structuredClone(str), { hass, - _states: buildStatesObject(hass), + // _states: buildStatesObject(hass), ...context, }) .trim(); diff --git a/tests/renderTemplate.test.ts b/tests/renderTemplate.test.ts index a1cc51d..d6d9724 100644 --- a/tests/renderTemplate.test.ts +++ b/tests/renderTemplate.test.ts @@ -132,7 +132,7 @@ describe('renderTemplate', () => { assert.equal( renderTemplate( hass, - '{{ _states.light.lounge.attributes.min_mireds }}', + '{{ hass.states["light.lounge"].attributes.min_mireds }}', context, ), 153, @@ -140,7 +140,7 @@ describe('renderTemplate', () => { assert.equal( renderTemplate( hass, - 'The {{ min }} color temperature is {{ _states.light.lounge.attributes.min_mireds }} mireds. Also I\'m {{ doThing("my taxes") }}', + 'The {{ min }} color temperature is {{ hass.states["light.lounge"].attributes.min_mireds }} mireds. Also I\'m {{ doThing("my taxes") }}', context, ), "The minimum color temperature is 153 mireds. Also I'm doing my taxes!", diff --git a/tests/utils/distance.test.ts b/tests/utils/distance.test.ts index 4f0935f..bf46951 100644 --- a/tests/utils/distance.test.ts +++ b/tests/utils/distance.test.ts @@ -83,22 +83,22 @@ describe('closest', () => { ); }); - it('should return the closest entity to home if given _states', () => { - assert.equal( - renderTemplate(hass, '{{ closest(_states).entity_id }}'), - 'person.john_doe', - ); - }); + // it('should return the closest entity to home if given _states', () => { + // assert.equal( + // renderTemplate(hass, '{{ closest(_states).entity_id }}'), + // 'person.john_doe', + // ); + // }); - it('should return the closest entity to home if given _states.domain', () => { - assert.equal( - renderTemplate( - hass, - '{{ closest(_states.device_tracker).entity_id }}', - ), - 'device_tracker.ark_of_the_covenent', - ); - }); + // it('should return the closest entity to home if given _states.domain', () => { + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest(_states.device_tracker).entity_id }}', + // ), + // 'device_tracker.ark_of_the_covenent', + // ); + // }); it('should return the closest entity in a domain if given one', () => { assert.equal( @@ -129,13 +129,13 @@ describe('closest', () => { ), 'person.jane_doe', ); - assert.equal( - renderTemplate( - hass, - '{{ closest(_states.zone.danger).entity_id }}', - ), - 'person.jane_doe', - ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest(_states.zone.danger).entity_id }}', + // ), + // 'person.jane_doe', + // ); }); }); @@ -148,20 +148,20 @@ describe('closest', () => { ), 'device_tracker.null_island', ); - assert.equal( - renderTemplate( - hass, - '{{ closest("zone.danger", _states).entity_id }}', - ), - 'device_tracker.null_island', - ); - assert.equal( - renderTemplate( - hass, - '{{ closest("zone.danger", _states.person).entity_id }}', - ), - 'person.john_doe', - ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest("zone.danger", _states).entity_id }}', + // ), + // 'device_tracker.null_island', + // ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest("zone.danger", _states.person).entity_id }}', + // ), + // 'person.john_doe', + // ); assert.equal( renderTemplate( hass, @@ -186,20 +186,20 @@ describe('closest', () => { ), 'device_tracker.ark_of_the_covenent', ); - assert.equal( - renderTemplate( - hass, - '{{ closest(31, 31, _states).entity_id }}', - ), - 'device_tracker.ark_of_the_covenent', - ); - assert.equal( - renderTemplate( - hass, - '{{ closest(31, 31, _states.person).entity_id }}', - ), - 'person.jane_doe', - ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest(31, 31, _states).entity_id }}', + // ), + // 'device_tracker.ark_of_the_covenent', + // ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest(31, 31, _states.person).entity_id }}', + // ), + // 'person.jane_doe', + // ); assert.equal( renderTemplate( hass, @@ -232,13 +232,13 @@ describe('closest', () => { ), 'person.jane_doe', ); - assert.equal( - renderTemplate( - hass, - '{{ closest(31, 31, _states.zone.danger).entity_id }}', - ), - 'person.jane_doe', - ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ closest(31, 31, _states.zone.danger).entity_id }}', + // ), + // 'person.jane_doe', + // ); }); }); }); diff --git a/tests/utils/groups.test.ts b/tests/utils/groups.test.ts index d0fa622..e11b15c 100644 --- a/tests/utils/groups.test.ts +++ b/tests/utils/groups.test.ts @@ -51,57 +51,57 @@ describe('expand', () => { ); }); - it('should return a sorted array of state objects if given a list of _states objects', () => { - assert.equal( - renderTemplate( - hass, - '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[0].entity_id }}', - ), - 'device_tracker.null_island', - ); - assert.equal( - renderTemplate( - hass, - '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[1].entity_id }}', - ), - 'input_number.volume', - ); - assert.equal( - renderTemplate( - hass, - '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[2].entity_id }}', - ), - 'person.john_doe', - ); - }); + // it('should return a sorted array of state objects if given a list of _states objects', () => { + // assert.equal( + // renderTemplate( + // hass, + // '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[0].entity_id }}', + // ), + // 'device_tracker.null_island', + // ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[1].entity_id }}', + // ), + // 'input_number.volume', + // ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ expand(_states.device_tracker.null_island, _states.person.john_doe, _states.input_number.volume)[2].entity_id }}', + // ), + // 'person.john_doe', + // ); + // }); - it('should return a sorted array of state objects if given one or more _states domain object', () => { - assert.equal( - renderTemplate( - hass, - '{{ expand(_states.device_tracker)[0].entity_id }}', - ), - 'device_tracker.ark_of_the_covenent', - ); - assert.equal( - renderTemplate( - hass, - '{{ expand(_states.device_tracker, _states.zone)[3].entity_id }}', - ), - 'zone.home', - ); - }); + // it('should return a sorted array of state objects if given one or more _states domain object', () => { + // assert.equal( + // renderTemplate( + // hass, + // '{{ expand(_states.device_tracker)[0].entity_id }}', + // ), + // 'device_tracker.ark_of_the_covenent', + // ); + // assert.equal( + // renderTemplate( + // hass, + // '{{ expand(_states.device_tracker, _states.zone)[3].entity_id }}', + // ), + // 'zone.home', + // ); + // }); - it('should return all state objects if given _states', () => { - assert.equal( - renderTemplate(hass, '{{ expand(_states).length }}'), - Object.keys(hass.states) - .filter( - (entityId) => !hass.states[entityId].attributes?.persons, - ) - .length.toString(), - ); - }); + // it('should return all state objects if given _states', () => { + // assert.equal( + // renderTemplate(hass, '{{ expand(_states).length }}'), + // Object.keys(hass.states) + // .filter( + // (entityId) => !hass.states[entityId].attributes?.persons, + // ) + // .length.toString(), + // ); + // }); it('should expand group entities into their children and not include the parent entity', () => { assert.match( @@ -118,13 +118,13 @@ describe('expand', () => { ) as string, /person\.jane_doe/g, ); - assert.match( - renderTemplate( - hass, - '{{ expand("input_number.volume", _states.zone.home, "light.lounge") | to_json }}', - ) as string, - /person\.jane_doe/g, - ); + // assert.match( + // renderTemplate( + // hass, + // '{{ expand("input_number.volume", _states.zone.home, "light.lounge") | to_json }}', + // ) as string, + // /person\.jane_doe/g, + // ); assert.match( renderTemplate( hass, @@ -153,20 +153,20 @@ describe('expand', () => { ) as string, /light\.ceiling_bulb_2/g, ); - assert.match( - renderTemplate( - hass, - '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', - ) as string, - /light\.ceiling_bulb_1/g, - ); - assert.match( - renderTemplate( - hass, - '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', - ) as string, - /light\.ceiling_bulb_2/g, - ); + // assert.match( + // renderTemplate( + // hass, + // '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', + // ) as string, + // /light\.ceiling_bulb_1/g, + // ); + // assert.match( + // renderTemplate( + // hass, + // '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', + // ) as string, + // /light\.ceiling_bulb_2/g, + // ); assert.doesNotMatch( renderTemplate( hass, @@ -181,13 +181,13 @@ describe('expand', () => { ) as string, /zone\.home/g, ); - assert.doesNotMatch( - renderTemplate( - hass, - '{{ expand("input_number.volume", _states.zone.home, "light.lounge") | to_json }}', - ) as string, - /zone\.home/g, - ); + // assert.doesNotMatch( + // renderTemplate( + // hass, + // '{{ expand("input_number.volume", _states.zone.home, "light.lounge") | to_json }}', + // ) as string, + // /zone\.home/g, + // ); assert.doesNotMatch( renderTemplate( hass, @@ -202,12 +202,12 @@ describe('expand', () => { ) as string, /light\.lounge/g, ); - assert.doesNotMatch( - renderTemplate( - hass, - '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', - ) as string, - /light\.lounge/g, - ); + // assert.doesNotMatch( + // renderTemplate( + // hass, + // '{{ expand("input_number.volume", "zone.home", _states.light.lounge) | to_json }}', + // ) as string, + // /light\.lounge/g, + // ); }); }); diff --git a/tests/utils/states.test.ts b/tests/utils/states.test.ts index 3348d2c..eb266f3 100644 --- a/tests/utils/states.test.ts +++ b/tests/utils/states.test.ts @@ -190,11 +190,11 @@ describe('has_value', () => { }); }); -describe('states object', () => { - it('should allow you to access state objects using just dot notation', () => { - assert.equal( - renderTemplate(hass, '{{ _states.light.ceiling_bulb_1.state }}'), - 'off', - ); - }); -}); +// describe('states object', () => { +// it('should allow you to access state objects using just dot notation', () => { +// assert.equal( +// renderTemplate(hass, '{{ _states.light.ceiling_bulb_1.state }}'), +// 'off', +// ); +// }); +// });