home-assistant-javascript-templates 5.11.0 → 6.0.0-beta.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 +77 -10
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +13 -2
- package/dist/index.js +1 -1
- package/package.json +10 -4
package/README.md
CHANGED
|
@@ -91,7 +91,9 @@ new HomeAssistantJavaScriptTemplates(
|
|
|
91
91
|
| ------------------ | ------------- | ------- | -------------------------------------------------- |
|
|
92
92
|
| `throwErrors` | yes | false | 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. |
|
|
93
93
|
| `throwWarnings` | yes | true | Indicates if the library should throw warnings in the console, either when there is an error in the templates and `throwErrors` is configured in `false`, or when a non-existent entity or domain is used in the templates. |
|
|
94
|
-
| `variables` | yes | `{}` | An object holding custom global variables to be used inside all the templates. The values could be any type |
|
|
94
|
+
| `variables` | yes | `{}` | An object holding custom global variables to be used inside all the templates. The values could be of any type |
|
|
95
|
+
| `refs` | yes | `{}` | An object holding custom global refs variables to be used inside all the templates. The values could be of any type. Consult the [refs variables](#refs-variables) section for more info |
|
|
96
|
+
| `refsVariableName` | yes | `refs` | The name in the template of the object holding the `refs` variables. Consult the [refs variables](#refs-variables) section for more info |
|
|
95
97
|
| `autoReturn` | yes | true | Indicates if the library should add a `return` statement at the beginning of a template code if no `return` statements are contained in the code|
|
|
96
98
|
|
|
97
99
|
### Methods
|
|
@@ -110,6 +112,10 @@ This class is only exported as a type in the package, you cannot import it direc
|
|
|
110
112
|
|
|
111
113
|
This property gets and sets the global variables that will be available in all the templates.
|
|
112
114
|
|
|
115
|
+
#### refs
|
|
116
|
+
|
|
117
|
+
This property gets and sets the global refs variables that will be available in all the templates. If you assign a new `refs`, the previous `refs` variables and the template trackings that were using them will be cleaned. Consult the [refs variables](#refs-variables) section for more info.
|
|
118
|
+
|
|
113
119
|
### Methods
|
|
114
120
|
|
|
115
121
|
#### renderTemplate
|
|
@@ -117,14 +123,17 @@ This property gets and sets the global variables that will be available in all t
|
|
|
117
123
|
```typescript
|
|
118
124
|
renderTemplate(
|
|
119
125
|
template: string,
|
|
120
|
-
|
|
126
|
+
extras?: {
|
|
127
|
+
variables?: Record<string, unknown>,
|
|
128
|
+
refs?: Record<string, unknown>,
|
|
129
|
+
}
|
|
121
130
|
): any
|
|
122
131
|
```
|
|
123
132
|
|
|
124
133
|
This method renders a `JavaScript` template and return its result. It needs a string as a parameter. Inside this string you can use [several objects and methods](#objects-and-methods-available-in-the-templates). It returns whatever the `JavaScript` code returns, because of that it is typed as `any`.
|
|
125
134
|
|
|
126
135
|
>[!NOTE]
|
|
127
|
-
>This method accepts an optional second parameter with
|
|
136
|
+
>This method accepts an optional second parameter with an object. In this object it is possible to send a `variables` object, containing extra variables that will be appended to [the global variables](#variables) and a `refs` object, containing extra [refs variables](#refs-variables). The extra `refs` variables will be appended to the global ones, making them available even in templates that weere declared before the call to this method. You need to be aware, that if a ref variable already exists, sending it again in this method will not override it because it will be ignored.
|
|
128
137
|
|
|
129
138
|
#### trackTemplate
|
|
130
139
|
|
|
@@ -132,18 +141,21 @@ This method renders a `JavaScript` template and return its result. It needs a st
|
|
|
132
141
|
trackTemplate(
|
|
133
142
|
template: string,
|
|
134
143
|
renderingFunction: (result?: any) => void,
|
|
135
|
-
|
|
144
|
+
extras?: {
|
|
145
|
+
variables?: Record<string, unknown>,
|
|
146
|
+
refs?: Record<string, unknown>,
|
|
147
|
+
}
|
|
136
148
|
): () => void
|
|
137
149
|
```
|
|
138
150
|
|
|
139
151
|
This method registers a template tracking. It executes the `renderingFunction` sent to the method with the result of the rendered `template` and will execute `renderingFunction` with an updated result of the rendered `template` every time that the entities used in the template update. You can use [several objects and methods](#objects-and-methods-available-in-the-templates) inside the `template` string.
|
|
140
152
|
|
|
141
|
-
If some entity was not reached in the template code because it was inside a condition that never met, then it will not be tracked, so if its state changes it will not trigger the `renderingFunction` again. Only those entities that were called during the rendering 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.
|
|
153
|
+
If some entity was not reached in the template code because it was inside a condition that never met, then it will not be tracked, so if its state changes it will not trigger the `renderingFunction` again. Only those entities that were called during the rendering using [states](#states), [state_translated](#state_translated), [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), [device_attr](#device_attr), [is_device_attr](#is_device_attr), [device_id](#device_id), [device_name](#device_name), [area_id](#area_id), and [area_name](#area_name) will be included.
|
|
142
154
|
|
|
143
155
|
This method will return a function. When this function is executed, the tracking of that template/rendering function is removed and subsecuent changes in the entities of the template will not call the `renderingFunction`.
|
|
144
156
|
|
|
145
157
|
>[!NOTE]
|
|
146
|
-
>This method accepts an optional third parameter with
|
|
158
|
+
>This method accepts an optional third parameter with an object. In this object it is possible to send a `variables` object, containing extra variables that will be appended to [the global variables](#variables) and a `refs` object, containing extra [refs variables](#refs-variables). The extra `refs` variables will be appended to the global ones, making them available even in templates that weere declared before the call to this method. You need to be aware, that if a ref variable already exists, sending it again in this method will not override it because it will be ignored.
|
|
147
159
|
|
|
148
160
|
#### cleanTracked
|
|
149
161
|
|
|
@@ -153,6 +165,9 @@ cleanTracked(entityId?: string): void
|
|
|
153
165
|
|
|
154
166
|
This method will clean the template tracking for a specific entity or will clean all the template trackings if no entity id is specified.
|
|
155
167
|
|
|
168
|
+
>[!NOTE]
|
|
169
|
+
>With this method, it is possible to clean `refs` variables. To do so, you just need to send as the name of the entity the value of `refsVariableName` (by default `refs`) and the name of the variable separated by a dot, e.g `refs.my_variable`.
|
|
170
|
+
|
|
156
171
|
### Objects and methods available in the templates
|
|
157
172
|
|
|
158
173
|
#### hass
|
|
@@ -274,26 +289,38 @@ devices['706ad0ebe27e105d7cd0b73386deefdd'] // returns the device that matches t
|
|
|
274
289
|
|
|
275
290
|
#### device_attr
|
|
276
291
|
|
|
277
|
-
Method that returns the value of an attribute for the given device id
|
|
292
|
+
Method that returns the value of an attribute for the given entity or device id. It returns `undefined` if the entity or the device doesn’t exist.
|
|
278
293
|
|
|
279
294
|
```javascript
|
|
295
|
+
device_attr('sensor.my_sensor', 'manufacturer')
|
|
280
296
|
device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer')
|
|
281
297
|
```
|
|
282
298
|
|
|
283
299
|
#### is_device_attr
|
|
284
300
|
|
|
285
|
-
Method to test if the value of a device attribute matches a value. It returns a `boolean`, if the device
|
|
301
|
+
Method to test if the value of a device attribute matches a value for a given entity or device id. It returns a `boolean`, if the entity or device doesn‘t exist it returns `false`.
|
|
286
302
|
|
|
287
303
|
```javascript
|
|
304
|
+
is_device_attr('sensor.my_sensor', 'manufacturer', 'Synology')
|
|
288
305
|
is_device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer', 'Synology')
|
|
289
306
|
```
|
|
290
307
|
|
|
291
308
|
#### device_id
|
|
292
309
|
|
|
293
|
-
Method to return the device id for a given entity id or `undefined` if the entity doesn‘t exist.
|
|
310
|
+
Method to return the device id for a given entity id or device name. It returns `undefined` if the entity or name doesn‘t exist.
|
|
294
311
|
|
|
295
312
|
```javascript
|
|
296
313
|
device_id('sensor.my_sensor')
|
|
314
|
+
device_id('My lamp')
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
#### device_name
|
|
318
|
+
|
|
319
|
+
Method to return the device name for a given device id or entity id. It returns `undefined` if the entity or device doesn‘t exist.
|
|
320
|
+
|
|
321
|
+
```javascript
|
|
322
|
+
device_name('sensor.my_sensor')
|
|
323
|
+
device_name('706ad0ebe27e105d7cd0b73386deefdd')
|
|
297
324
|
```
|
|
298
325
|
|
|
299
326
|
#### areas
|
|
@@ -465,7 +492,7 @@ haJsTemplates.getRenderer()
|
|
|
465
492
|
const udatesEntities = states.update;
|
|
466
493
|
const updateEntitiesValues = Object.values(udatesEntities);
|
|
467
494
|
const updatesEntitiesOn = updateEntitiesValues.filter((entity) => entity.state === 'on');
|
|
468
|
-
return
|
|
495
|
+
return \`${PREFIX} ${updatesEntitiesOn.length}\`;
|
|
469
496
|
`,
|
|
470
497
|
(result) => {
|
|
471
498
|
element.innerHTML = result;
|
|
@@ -479,4 +506,44 @@ haJsTemplates.getRenderer()
|
|
|
479
506
|
|
|
480
507
|
```
|
|
481
508
|
|
|
509
|
+
### Refs variables
|
|
510
|
+
|
|
511
|
+
`Refs` refers to "reactive variables". If you use a reactive variable in a template, this template will get re-render if the value of that reactive variable changes. To access reactive variables in a template you just need to access the `refs` object. The name of this object can be changed through the `refsVariableName` option of the [HomeAssistantJavaScriptTemplates class](#homeassistantjavascripttemplates-class).
|
|
512
|
+
|
|
513
|
+
#### Declaring a global reactive variable
|
|
514
|
+
|
|
515
|
+
```javascript
|
|
516
|
+
const haJsTemplates = new HomeAssistantJavaScriptTemplates(
|
|
517
|
+
document.querySelector('home-assistant'),
|
|
518
|
+
{
|
|
519
|
+
refs: {
|
|
520
|
+
MY_VARIABLE: 'REACTIVE'
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
);
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
#### Accessing and modifying refs variables in the templates
|
|
527
|
+
|
|
528
|
+
```javascript
|
|
529
|
+
haJsTemplates.getRenderer()
|
|
530
|
+
.then((renderer) => {
|
|
531
|
+
// Render the initial value
|
|
532
|
+
const result = renderer.renderTemplate('refs.MY_VARIABLE');
|
|
533
|
+
console.log(result); // REACTIVE
|
|
534
|
+
|
|
535
|
+
// Track changes in a template with a reactive variable
|
|
536
|
+
const untrack = renderer.trackTemplate(
|
|
537
|
+
'return refs.MY_VARIABLE + "_RETURNED"',
|
|
538
|
+
(result) => {
|
|
539
|
+
console.log(result); // REACTIVE_RETURNED
|
|
540
|
+
}
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
// the renderingFunction above will be executed with REACTIVE_MODIFIED_RETURNED
|
|
544
|
+
renderer.renderTemplate('refs.MY_VARIABLE = "REACTIVE_MODIFIED"');
|
|
545
|
+
|
|
546
|
+
});
|
|
547
|
+
```
|
|
548
|
+
|
|
482
549
|
[Optional chaining operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ interface Options {
|
|
|
3
3
|
throwErrors?: boolean;
|
|
4
4
|
throwWarnings?: boolean;
|
|
5
5
|
variables?: Vars;
|
|
6
|
+
refs?: Vars;
|
|
7
|
+
refsVariableName?: string;
|
|
6
8
|
autoReturn?: boolean;
|
|
7
9
|
}
|
|
8
10
|
type RenderingFunction = (result?: any) => void;
|
|
@@ -12,6 +14,7 @@ interface Area {
|
|
|
12
14
|
}
|
|
13
15
|
interface Device {
|
|
14
16
|
id: string;
|
|
17
|
+
name: string | null;
|
|
15
18
|
area_id: string | null;
|
|
16
19
|
[key: string]: unknown;
|
|
17
20
|
}
|
|
@@ -53,11 +56,17 @@ declare global {
|
|
|
53
56
|
hassConnection: Promise<HassConnection>;
|
|
54
57
|
}
|
|
55
58
|
}
|
|
59
|
+
interface Extras {
|
|
60
|
+
variables?: Vars;
|
|
61
|
+
refs?: Vars;
|
|
62
|
+
}
|
|
56
63
|
declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
57
64
|
constructor(ha: HomeAssistant, options: Options);
|
|
58
65
|
private _throwErrors;
|
|
59
66
|
private _throwWarnings;
|
|
60
67
|
private _variables;
|
|
68
|
+
private _refs;
|
|
69
|
+
private _refsVariableName;
|
|
61
70
|
private _autoReturn;
|
|
62
71
|
private _clientSideEntitiesRegExp;
|
|
63
72
|
private _subscriptions;
|
|
@@ -70,11 +79,13 @@ declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
|
70
79
|
private _entityWatchCallback;
|
|
71
80
|
private _storeTracked;
|
|
72
81
|
private _untrackTemplate;
|
|
73
|
-
renderTemplate(template: string,
|
|
74
|
-
trackTemplate(template: string, renderingFunction: RenderingFunction,
|
|
82
|
+
renderTemplate(template: string, extras?: Extras): any;
|
|
83
|
+
trackTemplate(template: string, renderingFunction: RenderingFunction, extras?: Extras): () => void;
|
|
75
84
|
cleanTracked(entityId?: string): void;
|
|
76
85
|
get variables(): Vars;
|
|
77
86
|
set variables(value: Vars);
|
|
87
|
+
get refs(): Vars;
|
|
88
|
+
set refs(value: Vars);
|
|
78
89
|
}
|
|
79
90
|
declare class HomeAssistantJavaScriptTemplates {
|
|
80
91
|
constructor(ha: HomeAssistant, options?: Options);
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getPromisableResult as t}from"get-promisable-result";var e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,r=0,s=e.length;r<s;r++)!i&&r in e||(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,r,s,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(r||(r={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(s||(s={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,f="ref",l="value",v="toJSON",b=function(t){return"".concat(f,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},g=function(t){return y("Entity",t)},w=function(t){return y("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):g(e)},A=function(t){p.add(t)},N=function(e,n){var i=n.with_unit,r=void 0!==i&&i,s=n.rounded,a=void 0!==s&&s;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return r&&c?"".concat(d," ").concat(c):d}},T=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?N(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),d(e))return m(e),N(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),T(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||w(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),T(t[e])}})}}),state_translated:function(e){if(m(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return m(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,r;return m(e),null===(r=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===r?void 0:r[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(g(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,r.AREA_ID);var s=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(s)return this.device_attr(s,r.AREA_ID);var a=c().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 n,i,s;e in t.hass.devices&&(s=this.device_attr(e,r.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(s=this.device_attr(a,r.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===s}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var r=new Proxy(((n={})[l]=void 0,n[v]=function(){return this[l]},n),{get:function(t,e,n){if(e===l||e===v)return A(i),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(f," property. A ").concat(f,' only exposes a "').concat(l,'" property'))},set:function(e,n,r){if(n===l){var s=e[l];return e[l]=r,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(s)},new_state:{state:JSON.stringify(r)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(f)),!1}});return h.set(e,r),r},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(s).includes(i)&&A(i),i){case s.PANEL_URL:return location.pathname;case s.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(s).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,r=e.throwWarnings,a=void 0===r||r,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(s).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=_(t,i,a),this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(s.PANEL_URL)&&this._executeRenderingFunctions(s.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.STATE_CHANGE_EVENT})}))},t.prototype._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(s.LANG)&&t._executeRenderingFunctions(s.LANG)}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e,n){var i=this;this._scopped.tracked.forEach((function(r){var s=[e,n];if(i._subscriptions.has(r)){var a=i._subscriptions.get(r);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,s)}else a.set(t,new Map([s]))}else i._subscriptions.set(r,new Map([[t,new Map([s])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,r){if(i.has(t)){var s=i.get(t);s.delete(e),0===s.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(r))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var r=new Map(Object.entries(e(e({},this._variables),i))),s=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=s.includes("return")||!this._autoReturn?s:"return ".concat(s);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","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","clientSide","ref","unref"],Array.from(r.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(r.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var r=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(r),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();export{h as default};
|
|
1
|
+
import{getPromisableResult as t}from"get-promisable-result";var e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u="refs",d=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},_=function(t){return t.includes(".")},h="ref",p="value",f="toJSON",v=function(t,e){return void 0===e&&(e=!1),e?"".concat(u,".").concat(t):"".concat(h,".").concat(t)};function l(t,e,n){var c=function(){return Object.entries(t.hass.areas)},u=function(){return Object.entries(t.hass.devices)},l=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=new Map,m=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},w=function(t){return m("Entity",t)},E=function(t){return m("Domain",t)},A=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},N=function(e){t.hass.states[e]?b.add(e):w(e)},T=function(t){b.add(t)},S=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return s&&c?"".concat(d," ").concat(c):d}},O=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?S(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),_(e))return N(e),S(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(_(n))return N(n),O(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||E(n),new Proxy(d(i),{get:function(t,e){return N("".concat(n,".").concat(e)),O(t[e])}})}}),state_translated:function(e){if(N(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return N(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return N(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(w(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(_(e))return N(e),t.hass.entities[e];var n=l().filter((function(t){return t[0].startsWith(e)}));return n.length||E(e),new Proxy(d(n),{get:function(t,n){return N("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return N(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(_(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(_(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i,s,r;if(_(e)){N(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(_(e))return N(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=u().find((function(t){return t[1].name===e}));return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(_(e)){N(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.AREA_ID);var a=c().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 n,i,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===r}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?l().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?u().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return b},cleanTracked:function(){b.clear()},ref:function(t,e){var n,i=v(e);if(y.has(e))return y.get(e);var s=new Proxy(((n={})[p]=void 0,n[f]=function(){return this[p]},n),{get:function(t,e,n){if(e===p||e===f)return T(i),Reflect.get(t,e,n);A("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(p,'" property'))},set:function(e,n,s){if(n===p){var r=e[p];return e[p]=s,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(s)}}}),!0}return A('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,s),s},unref:function(t,e){var n=v(e);y.has(e)?(y.delete(e),t(n)):A("".concat(e," is not a ref or it has been unrefed already"))},get refsVariables(){return g},buildRefsVariables:function(t,e){return Object.entries(e).forEach((function(t){var e=t[0],n=t[1];g.has(e)||g.set(e,n)})),new Proxy(e,{get:function(t,e){var n=v(e);return T(n),g.get(e)},set:function(e,n,i){var s=v(n),r=g.get(n);return g.set(n,i),t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}})},cleanRefsVariables:function(){g.clear()},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&T(i),i){case r.PANEL_URL:return location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var b=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,d=e.refs,_=void 0===d?{}:d,h=e.refsVariableName,p=void 0===h?u:h,f=e.autoReturn,v=void 0===f||f;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refs=_,this._refsVariableName=p,this._autoReturn=v,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=l(t,i,a),this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.STATE_CHANGE_EVENT})}))},t.prototype._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e,n){var i=this;this._scopped.tracked.forEach((function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,s){if(i.has(t)){var r=i.get(t);r.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=e(e({},this._refs),o),d=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),_=d.includes("return")||!this._autoReturn?d:"return ".concat(d);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(_)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this)),this._scopped.buildRefsVariables.bind(this._scopped,this._entityWatchCallback.bind(this))(u)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._refs},set:function(t){var e=this,n=this._scopped.refsVariables;Array.from(n.keys()).forEach((function(t){e.cleanTracked(v(t,!0))})),this._scopped.cleanRefsVariables(),this._refs=t},enumerable:!1,configurable:!0}),t}(),y=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new b(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();export{y as default};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ interface Options {
|
|
|
3
3
|
throwErrors?: boolean;
|
|
4
4
|
throwWarnings?: boolean;
|
|
5
5
|
variables?: Vars;
|
|
6
|
+
refs?: Vars;
|
|
7
|
+
refsVariableName?: string;
|
|
6
8
|
autoReturn?: boolean;
|
|
7
9
|
}
|
|
8
10
|
type RenderingFunction = (result?: any) => void;
|
|
@@ -12,6 +14,7 @@ interface Area {
|
|
|
12
14
|
}
|
|
13
15
|
interface Device {
|
|
14
16
|
id: string;
|
|
17
|
+
name: string | null;
|
|
15
18
|
area_id: string | null;
|
|
16
19
|
[key: string]: unknown;
|
|
17
20
|
}
|
|
@@ -53,11 +56,17 @@ declare global {
|
|
|
53
56
|
hassConnection: Promise<HassConnection>;
|
|
54
57
|
}
|
|
55
58
|
}
|
|
59
|
+
interface Extras {
|
|
60
|
+
variables?: Vars;
|
|
61
|
+
refs?: Vars;
|
|
62
|
+
}
|
|
56
63
|
declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
57
64
|
constructor(ha: HomeAssistant, options: Options);
|
|
58
65
|
private _throwErrors;
|
|
59
66
|
private _throwWarnings;
|
|
60
67
|
private _variables;
|
|
68
|
+
private _refs;
|
|
69
|
+
private _refsVariableName;
|
|
61
70
|
private _autoReturn;
|
|
62
71
|
private _clientSideEntitiesRegExp;
|
|
63
72
|
private _subscriptions;
|
|
@@ -70,11 +79,13 @@ declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
|
70
79
|
private _entityWatchCallback;
|
|
71
80
|
private _storeTracked;
|
|
72
81
|
private _untrackTemplate;
|
|
73
|
-
renderTemplate(template: string,
|
|
74
|
-
trackTemplate(template: string, renderingFunction: RenderingFunction,
|
|
82
|
+
renderTemplate(template: string, extras?: Extras): any;
|
|
83
|
+
trackTemplate(template: string, renderingFunction: RenderingFunction, extras?: Extras): () => void;
|
|
75
84
|
cleanTracked(entityId?: string): void;
|
|
76
85
|
get variables(): Vars;
|
|
77
86
|
set variables(value: Vars);
|
|
87
|
+
get refs(): Vars;
|
|
88
|
+
set refs(value: Vars);
|
|
78
89
|
}
|
|
79
90
|
declare class HomeAssistantJavaScriptTemplates {
|
|
80
91
|
constructor(ha: HomeAssistant, options?: Options);
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("get-promisable-result"),e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,l="ref",f="value",v="toJSON",b=function(t){return"".concat(l,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},g=function(t){return y("Entity",t)},w=function(t){return y("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):g(e)},A=function(t){p.add(t)},N=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return s&&c?"".concat(d," ").concat(c):d}},T=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?N(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),d(e))return m(e),N(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),T(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||w(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),T(t[e])}})}}),state_translated:function(e){if(m(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return m(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return m(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(g(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.AREA_ID);var a=c().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 n,i,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===r}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var s=new Proxy(((n={})[f]=void 0,n[v]=function(){return this[f]},n),{get:function(t,e,n){if(e===f||e===v)return A(i),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(l," property. A ").concat(l,' only exposes a "').concat(f,'" property'))},set:function(e,n,s){if(n===f){var r=e[f];return e[f]=s,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(s)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(l)),!1}});return h.set(e,s),s},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&A(i),i){case r.PANEL_URL:return location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=_(t,i,a),this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.STATE_CHANGE_EVENT})}))},t.prototype._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e,n){var i=this;this._scopped.tracked.forEach((function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,s){if(i.has(t)){var r=i.get(t);r.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=new Map(Object.entries(e(e({},this._variables),i))),r=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=r.includes("return")||!this._autoReturn?r:"return ".concat(r);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","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","clientSide","ref","unref"],Array.from(s.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(s.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t.getPromisableResult((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=h;
|
|
1
|
+
"use strict";var t=require("get-promisable-result"),e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u="refs",d=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},_=function(t){return t.includes(".")},h="ref",p="value",f="toJSON",l=function(t,e){return void 0===e&&(e=!1),e?"".concat(u,".").concat(t):"".concat(h,".").concat(t)};function v(t,e,n){var c=function(){return Object.entries(t.hass.areas)},u=function(){return Object.entries(t.hass.devices)},v=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=new Map,m=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},w=function(t){return m("Entity",t)},E=function(t){return m("Domain",t)},A=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},N=function(e){t.hass.states[e]?b.add(e):w(e)},T=function(t){b.add(t)},S=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return s&&c?"".concat(d," ").concat(c):d}},R=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?S(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),_(e))return N(e),S(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(_(n))return N(n),R(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||E(n),new Proxy(d(i),{get:function(t,e){return N("".concat(n,".").concat(e)),R(t[e])}})}}),state_translated:function(e){if(N(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return N(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return N(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(w(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(_(e))return N(e),t.hass.entities[e];var n=v().filter((function(t){return t[0].startsWith(e)}));return n.length||E(e),new Proxy(d(n),{get:function(t,n){return N("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return N(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(_(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(_(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i,s,r;if(_(e)){N(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(_(e))return N(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=u().find((function(t){return t[1].name===e}));return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(_(e)){N(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.AREA_ID);var a=c().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 n,i,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===r}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?v().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?u().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return b},cleanTracked:function(){b.clear()},ref:function(t,e){var n,i=l(e);if(y.has(e))return y.get(e);var s=new Proxy(((n={})[p]=void 0,n[f]=function(){return this[p]},n),{get:function(t,e,n){if(e===p||e===f)return T(i),Reflect.get(t,e,n);A("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(p,'" property'))},set:function(e,n,s){if(n===p){var r=e[p];return e[p]=s,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(s)}}}),!0}return A('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,s),s},unref:function(t,e){var n=l(e);y.has(e)?(y.delete(e),t(n)):A("".concat(e," is not a ref or it has been unrefed already"))},get refsVariables(){return g},buildRefsVariables:function(t,e){return Object.entries(e).forEach((function(t){var e=t[0],n=t[1];g.has(e)||g.set(e,n)})),new Proxy(e,{get:function(t,e){var n=l(e);return T(n),g.get(e)},set:function(e,n,i){var s=l(n),r=g.get(n);return g.set(n,i),t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}})},cleanRefsVariables:function(){g.clear()},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&T(i),i){case r.PANEL_URL:return location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var b=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,d=e.refs,_=void 0===d?{}:d,h=e.refsVariableName,p=void 0===h?u:h,f=e.autoReturn,l=void 0===f||f;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refs=_,this._refsVariableName=p,this._autoReturn=l,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=v(t,i,a),this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.STATE_CHANGE_EVENT})}))},t.prototype._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e,n){var i=this;this._scopped.tracked.forEach((function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,s){if(i.has(t)){var r=i.get(t);r.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=e(e({},this._refs),o),d=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),_=d.includes("return")||!this._autoReturn?d:"return ".concat(d);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(_)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this)),this._scopped.buildRefsVariables.bind(this._scopped,this._entityWatchCallback.bind(this))(u)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._refs},set:function(t){var e=this,n=this._scopped.refsVariables;Array.from(n.keys()).forEach((function(t){e.cleanTracked(l(t,!0))})),this._scopped.cleanRefsVariables(),this._refs=t},enumerable:!1,configurable:!0}),t}(),y=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t.getPromisableResult((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new b(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=y;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-beta.0",
|
|
4
4
|
"description": "A JavaScript utility to render Home Assistant JavaScript templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"home-assistant",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"license": "Apache-2.0",
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
34
|
-
"url": "git+https://github.com/elchininet/home-assistant-javascript-templates"
|
|
34
|
+
"url": "git+https://github.com/elchininet/home-assistant-javascript-templates.git"
|
|
35
35
|
},
|
|
36
36
|
"private": false,
|
|
37
37
|
"scripts": {
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
49
|
"@types/jest": "^30.0.0",
|
|
50
|
-
"@types/node": "^24.
|
|
50
|
+
"@types/node": "^24.10.1",
|
|
51
51
|
"get-promisable-result": "^1.0.1",
|
|
52
52
|
"jest": "^30.2.0",
|
|
53
53
|
"jest-environment-jsdom": "^30.2.0",
|
|
54
54
|
"jest-location-mock": "^2.0.0",
|
|
55
|
-
"rollup": "^4.
|
|
55
|
+
"rollup": "^4.53.3",
|
|
56
56
|
"rollup-plugin-ts": "^3.4.5",
|
|
57
57
|
"ts-jest": "^29.4.5",
|
|
58
58
|
"tslib": "^2.8.1",
|
|
@@ -60,5 +60,11 @@
|
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"get-promisable-result": "^1.0.1"
|
|
63
|
+
},
|
|
64
|
+
"pnpm": {
|
|
65
|
+
"overrides": {
|
|
66
|
+
"glob@>=10.3.7 <=11.0.3": ">=11.1.0",
|
|
67
|
+
"js-yaml@<4.1.1": ">=4.1.1"
|
|
68
|
+
}
|
|
63
69
|
}
|
|
64
70
|
}
|