home-assistant-javascript-templates 3.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,13 +62,18 @@ The package exposes a class that needs to be instantiated and is this instance t
62
62
  Main class of the library, it is the `default` export in the package.
63
63
 
64
64
  ```typescript
65
- new HomeAssistantJavaScriptTemplates(ha, throwErrors = false);
65
+ new HomeAssistantJavaScriptTemplates(
66
+ ha,
67
+ throwErrors = false,
68
+ trackNonExistent = false
69
+ );
66
70
  ```
67
71
 
68
- | Parameter | Optional | Description |
69
- | ------------- | ------------- | -------------------------------------------------- |
70
- | `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
71
- | `throwErrors` | yes | Indicates if the library should throw if the template contains any error. If not it will log the errors as a warning in the console and return `undefined` instead. |
72
+ | Parameter | Optional | Description |
73
+ | ------------------ | ------------- | -------------------------------------------------- |
74
+ | `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
75
+ | `throwErrors` | yes | Indicates if the library should throw if the template contains any error. If not it will log the errors as a warning in the console and return `undefined` instead. |
76
+ | `trackNonExistent` | yes | Indicates if the library should track those domains and entities that doesn't exist. |
72
77
 
73
78
  ### Properties
74
79
 
@@ -83,9 +88,12 @@ interface Tracked {
83
88
  get tracked(): Tracked
84
89
  ```
85
90
 
86
- This property will return an object with two properties (`entities` and `domains`). Each of these properties will be an array containing the entities or ids that have been tracked when the templates have been rendered. If some domain or entity was not reached because it was inside a condition that never met, then it will not be included in the `tracked` property. Only those entities or domains that were called during the rendering by the code using [states](#states), [is_state](#is_state), [state_attr](#state_attr), [is_state_attr](#is_state_attr) or [has_value](#has_value) will be included.
91
+ This property will return an object with two properties (`entities` and `domains`). Each of these properties will be an array containing the entities or ids that have been tracked when the templates have been rendered. If some domain or entity was not reached because it was inside a condition that never met, then it will not be included in the `tracked` property. Only those entities or domains that were called during the rendering by the code using [states](#states), [is_state](#is_state), [state_attr](#state_attr), [is_state_attr](#is_state_attr), [has_value](#has_value) [entities](#entities), [entity_prop](#entity_prop), [is_entity_prop](#is_entity_prop) or [device_id](#device_id) will be included.
87
92
 
88
- >Note: take into account that the domains will be only tracked if the `states` object is used accesing a domain. For example `states('device_tracker.paulus')` or `states['device_tracker.paulus']` will track the entity `device_tracker.paulus` but not the domain `device_tracker` but `states.device_tracker.paulus` will track both, the domain `device_tracker` and the entity `device_tracker.paulus`. The rest of the methods will track only entities.
93
+ >Notes:
94
+ > 1. Take into account that in the case of `states`, the domains will be tracked only if `states` is used as an object to acces a domain, for example `states('device_tracker.paulus')` or `states['device_tracker.paulus']` will track the entity `device_tracker.paulus` but not the domain `device_tracker` but `states.device_tracker.paulus` will track both, the domain `device_tracker` and the entity `device_tracker.paulus`.
95
+ > 2. In the case of `entities`, both, the method and the object will track a domain if a domain is used, for example `entities('device_tracker.paulus')` or `entities['device_tracker.paulus']` will track the entity `device_tracker.paulus` but not the domain `device_tracker`. On the other hand, `entities('device_tracker')` will track the domain `device_tracker` and `entities.device_tracker.paulus` will track both, the domain `device_tracker` and the entity `device_tracker.paulus`.
96
+ > 3. The rest of the methods will track only entities.
89
97
 
90
98
  ### Methods
91
99
 
@@ -129,7 +137,7 @@ The `hass` object
129
137
 
130
138
  #### states
131
139
 
132
- `states` could be used in two ways, as a function or as an object. When using it as function it only allows an entity id (containing the domain) as a parameter and it will return the state of that entity. As an object it allows you to access a domain or the full entity id.
140
+ `states` could be used in two ways, as a function or as an object. When using it as function it only allows an entity id (containing the domain) as a parameter and it will return the state of that entity. As an object it allows you to access a domain or the full entity state object.
133
141
 
134
142
  >Note: If you try to use `states` as a function sending a domain it will throw an error.
135
143
 
@@ -140,7 +148,7 @@ states('device_tracker.paulus') // returns the state of the entity id 'device_tr
140
148
  // Using states as an object
141
149
  states['device_tracker.paulus'].state // returns the state of the entity id 'device_tracker.paulus'
142
150
  states.device_tracker.paulus.state // returns the state of the entity id 'device_tracker.paulus'
143
- states.device_tracker // returns an object constaining all the entities of the 'device_tracker' domain
151
+ states.device_tracker // returns an object containing all the entities states of the 'device_tracker' domain
144
152
  ```
145
153
 
146
154
  >Note: Avoid using `states['device_tracker.paulus'].state` or `states.device_tracker.paulus.state`, instead use `states('device_tracker.paulus')` which will return `undefined` if the device id doesn‘t exist or the entity isn’t ready yet (the former will throw an error). If you still want to use them it is advisable to use the [Optional chaining operator], e.g. `states['device_tracker.paulus']?.state` or `states.device_tracker?.paulus?.state`.
@@ -177,6 +185,51 @@ Method to test if the given entity is not unknown or unavailable. It returns a `
177
185
  has_value('sensor.my_sensor')
178
186
  ```
179
187
 
188
+ #### entities
189
+
190
+ `entities` could be used in two ways, as a function or as an object.
191
+
192
+ ```javascript
193
+ // Using entities as a function
194
+ entities() // return all the entities
195
+ entities('device_tracker') // returns an object containing all the entities of the 'device_tracker' domain
196
+ entities('device_tracker.paulus') // returns the entity 'device_tracker.paulus'
197
+
198
+ // Using entities as an object
199
+ entities.device_tracker // returns an object containing all the entities of the 'device_tracker' domain
200
+ entities['device_tracker.paulus'] // returns the entity 'device_tracker.paulus'
201
+ entities.device_tracker.paulus // returns the entity 'device_tracker.paulus'
202
+ ```
203
+
204
+ #### entity_prop
205
+
206
+ Method that returns the value of a property of an entity or `undefined` if it doesn’t exist.
207
+
208
+ ```javascript
209
+ entity_prop('device_tracker.paulus', 'platform')
210
+ ```
211
+
212
+ #### is_entity_prop
213
+
214
+ Method to test if the value of an entity property matches a value. It returns a `boolean`, if the entity id or the property don‘t exist it returns `false`.
215
+
216
+ ```javascript
217
+ is_entity_prop('device_tracker.paulus', 'platform', 'hacs')
218
+ ```
219
+
220
+ #### devices
221
+
222
+ `devices` could be used in two ways, as a function or as an object.
223
+
224
+ ```javascript
225
+ // Using devices as a function
226
+ devices() // returns all the devices
227
+ devices('706ad0ebe27e105d7cd0b73386deefdd') // returns the device that matches the device id
228
+
229
+ // Using devices as an object
230
+ devices['706ad0ebe27e105d7cd0b73386deefdd'] // returns the device that matches the device id
231
+ ```
232
+
180
233
  #### device_attr
181
234
 
182
235
  Method that returns the value of an attribute for the given device id or `undefined` if it doesn’t exist.
@@ -271,6 +324,14 @@ Property to return if the user logged in in Home Assistant is the owner. It retu
271
324
  user_is_owner
272
325
  ```
273
326
 
327
+ #### user_agent
328
+
329
+ Property to return the user agent of the browser in which Home Assistant is running.
330
+
331
+ ```javascript
332
+ user_agent
333
+ ```
334
+
274
335
  ## Examples
275
336
 
276
337
  #### Get a device attribute and return a formatted text with it
@@ -15,6 +15,7 @@ interface State {
15
15
  interface Entity {
16
16
  area_id: string | null;
17
17
  device_id: string;
18
+ [key: string]: unknown;
18
19
  }
19
20
  interface User {
20
21
  name: string;
@@ -36,7 +37,7 @@ interface Tracked {
36
37
  domains: string[];
37
38
  }
38
39
  declare class HomeAssistantJavaScriptTemplates {
39
- constructor(ha: HomeAssistant, throwErrors?: boolean);
40
+ constructor(ha: HomeAssistant, throwErrors?: boolean, trackNonExistent?: boolean);
40
41
  private _scopped;
41
42
  private _errors;
42
43
  renderTemplate(template: string): any;
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- var e,t,i=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat("[home-assistant-javascript-templates]",": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return r().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().find((function(t){return t[1].name===e}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.entities).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(e),this._errors=t}return e.prototype.renderTemplate=function(e){try{var t=e.includes("return")?e:"return ".concat(e);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","".concat('"use strict";'," ").concat(t))(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();export{r as default};
1
+ var e,t,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.reduce((function(e,t){var i=t[0],s=t[1];return e[i.replace(n,"$2")]=s,e}),{})},r=function(e){return e.includes(".")};function a(n,a){var c=function(){return Object.entries(n.hass.areas)},o=function(){return Object.entries(n.hass.entities)},d=new Set,u=new Set,_=function(e){(a||n.hass.states[e])&&d.add(e)},p=function(e){u.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(r(e))return _(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(r(t))return _(t),n.hass.states[t];var i=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return(a||i.length)&&p(t),new Proxy(s(i),{get:function(e,i){return _("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return _(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return _(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},entities:new Proxy((function(e){if(void 0===e)return n.hass.entities;if(r(e))return _(e),n.hass.entities[e];var t=o().filter((function(t){return t[0].startsWith(e)}));return(a||t.length)&&p(e),new Proxy(s(t),{get:function(t,i){return _("".concat(e,".").concat(i)),t[i]}})}),{get:function(e,t){return e(t)}}),entity_prop:function(e,t){var i;return _(e),null===(i=n.hass.entities[e])||void 0===i?void 0:i[t]},is_entity_prop:function(e,t,i){return this.entity_prop(e,t)===i},devices:new Proxy((function(e){if(void 0===e)return n.hass.devices;if(r(e))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[e]}),{get:function(e,t){if(r(t))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[t]}}),device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return _(e),null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return c().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var r=c().find((function(t){return t[1].name===e}));return null===(i=null==r?void 0:r[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var r=this.device_id(e);r&&(s=this.device_attr(r,t.AREA_ID));var a=c().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=c().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?o().filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=c().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},tracked:{get entities(){return Array.from(d)},get domains(){return Array.from(u)}},cleanTrackedEntities:function(){d.clear()},cleanTrackedDomains:function(){u.clear()}}}var c=function(){function e(e,t,i){void 0===t&&(t=!1),void 0===i&&(i=!1),this._scopped=a(e,i),this._errors=t}return e.prototype.renderTemplate=function(e){try{var t=e.includes("return")?e:"return ".concat(e);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","".concat('"use strict";'," ").concat(t))(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner,this._scopped.user_agent)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();export{c as default};
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ interface State {
15
15
  interface Entity {
16
16
  area_id: string | null;
17
17
  device_id: string;
18
+ [key: string]: unknown;
18
19
  }
19
20
  interface User {
20
21
  name: string;
@@ -36,7 +37,7 @@ interface Tracked {
36
37
  domains: string[];
37
38
  }
38
39
  declare class HomeAssistantJavaScriptTemplates {
39
- constructor(ha: HomeAssistant, throwErrors?: boolean);
40
+ constructor(ha: HomeAssistant, throwErrors?: boolean, trackNonExistent?: boolean);
40
41
  private _scopped;
41
42
  private _errors;
42
43
  renderTemplate(template: string): any;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e,t,i=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat("[home-assistant-javascript-templates]",": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return r().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().find((function(t){return t[1].name===e}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.entities).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(e),this._errors=t}return e.prototype.renderTemplate=function(e){try{var t=e.includes("return")?e:"return ".concat(e);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","".concat('"use strict";'," ").concat(t))(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();module.exports=r;
1
+ "use strict";var e,t,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.reduce((function(e,t){var i=t[0],s=t[1];return e[i.replace(n,"$2")]=s,e}),{})},r=function(e){return e.includes(".")};function a(n,a){var c=function(){return Object.entries(n.hass.areas)},o=function(){return Object.entries(n.hass.entities)},d=new Set,u=new Set,_=function(e){(a||n.hass.states[e])&&d.add(e)},p=function(e){u.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(r(e))return _(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(r(t))return _(t),n.hass.states[t];var i=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return(a||i.length)&&p(t),new Proxy(s(i),{get:function(e,i){return _("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return _(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return _(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},entities:new Proxy((function(e){if(void 0===e)return n.hass.entities;if(r(e))return _(e),n.hass.entities[e];var t=o().filter((function(t){return t[0].startsWith(e)}));return(a||t.length)&&p(e),new Proxy(s(t),{get:function(t,i){return _("".concat(e,".").concat(i)),t[i]}})}),{get:function(e,t){return e(t)}}),entity_prop:function(e,t){var i;return _(e),null===(i=n.hass.entities[e])||void 0===i?void 0:i[t]},is_entity_prop:function(e,t,i){return this.entity_prop(e,t)===i},devices:new Proxy((function(e){if(void 0===e)return n.hass.devices;if(r(e))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[e]}),{get:function(e,t){if(r(t))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[t]}}),device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return _(e),null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return c().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var r=c().find((function(t){return t[1].name===e}));return null===(i=null==r?void 0:r[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var r=this.device_id(e);r&&(s=this.device_attr(r,t.AREA_ID));var a=c().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=c().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?o().filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=c().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},tracked:{get entities(){return Array.from(d)},get domains(){return Array.from(u)}},cleanTrackedEntities:function(){d.clear()},cleanTrackedDomains:function(){u.clear()}}}var c=function(){function e(e,t,i){void 0===t&&(t=!1),void 0===i&&(i=!1),this._scopped=a(e,i),this._errors=t}return e.prototype.renderTemplate=function(e){try{var t=e.includes("return")?e:"return ".concat(e);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","".concat('"use strict";'," ").concat(t))(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner,this._scopped.user_agent)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();module.exports=c;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "home-assistant-javascript-templates",
3
- "version": "3.1.1",
3
+ "version": "4.0.0",
4
4
  "description": "A JavaScript utility to render Home Assistant JavaScript templates",
5
5
  "keywords": [
6
6
  "home-assistant",
@@ -39,7 +39,6 @@
39
39
  "test:ts": "tsc --noEmit",
40
40
  "test:unit": "jest --verbose",
41
41
  "test:all": "pnpm test:ts && pnpm test:unit",
42
- "preinstall": "npx -y only-allow pnpm",
43
42
  "prepare": "pnpm build",
44
43
  "prepublishOnly": "pnpm test:all",
45
44
  "version": "git add .",
@@ -48,12 +47,13 @@
48
47
  "devDependencies": {
49
48
  "@rollup/plugin-terser": "^0.4.4",
50
49
  "@types/jest": "^29.5.12",
51
- "@types/node": "^20.11.30",
50
+ "@types/node": "^22.5.0",
52
51
  "jest": "^29.7.0",
53
- "rollup": "^4.13.0",
52
+ "jest-environment-jsdom": "^29.7.0",
53
+ "rollup": "^4.21.0",
54
54
  "rollup-plugin-ts": "^3.4.5",
55
- "ts-jest": "^29.1.2",
56
- "tslib": "^2.6.2",
57
- "typescript": "^5.4.3"
55
+ "ts-jest": "^29.2.5",
56
+ "tslib": "^2.7.0",
57
+ "typescript": "^5.5.4"
58
58
  }
59
59
  }