home-assistant-javascript-templates 4.0.0 → 5.1.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 +70 -49
- package/dist/esm/index.d.ts +32 -11
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +32 -11
- package/dist/index.js +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -34,11 +34,17 @@ pnpm add home-assistant-javascript-templates
|
|
|
34
34
|
```javascript
|
|
35
35
|
const HomeAssistantJavaScriptTemplates = require('home-assistant-javascript-templates');
|
|
36
36
|
|
|
37
|
-
const
|
|
37
|
+
const haJsTemplates = new HomeAssistantJavaScriptTemplates(
|
|
38
38
|
document.querySelector('home-assistant')
|
|
39
39
|
);
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
haJsTemplates.getRenderer()
|
|
42
|
+
then((renderer) => {
|
|
43
|
+
renderer.renderTemplate('... template string ...');
|
|
44
|
+
renderer.trackTemplate('... template string ...', () => {
|
|
45
|
+
// execute this function every time that en entity used in the template changes
|
|
46
|
+
});
|
|
47
|
+
});
|
|
42
48
|
```
|
|
43
49
|
|
|
44
50
|
#### Usage with ES6 modules
|
|
@@ -46,16 +52,22 @@ renderer.renderTemplate('... template string ...');
|
|
|
46
52
|
```javascript
|
|
47
53
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
48
54
|
|
|
49
|
-
const
|
|
55
|
+
const haJsTemplates = new HomeAssistantJavaScriptTemplates(
|
|
50
56
|
document.querySelector('home-assistant')
|
|
51
57
|
);
|
|
52
58
|
|
|
53
|
-
|
|
59
|
+
haJsTemplates.getRenderer()
|
|
60
|
+
then((renderer) => {
|
|
61
|
+
renderer.renderTemplate('... template string ...');
|
|
62
|
+
renderer.trackTemplate('... template string ...', () => {
|
|
63
|
+
// execute this function every time that en entity used in the template changes
|
|
64
|
+
});
|
|
65
|
+
});
|
|
54
66
|
```
|
|
55
67
|
|
|
56
68
|
## API
|
|
57
69
|
|
|
58
|
-
The package exposes a class that needs to be instantiated and
|
|
70
|
+
The package exposes a class that needs to be instantiated and the resolved promise that returns the `getRenderer` method of this instance is what you need to use in your code to render `JavaScript` templates.
|
|
59
71
|
|
|
60
72
|
### HomeAssistantJavaScriptTemplates class
|
|
61
73
|
|
|
@@ -64,36 +76,31 @@ Main class of the library, it is the `default` export in the package.
|
|
|
64
76
|
```typescript
|
|
65
77
|
new HomeAssistantJavaScriptTemplates(
|
|
66
78
|
ha,
|
|
67
|
-
|
|
68
|
-
trackNonExistent = false
|
|
79
|
+
options
|
|
69
80
|
);
|
|
70
81
|
```
|
|
71
82
|
|
|
72
83
|
| Parameter | Optional | Description |
|
|
73
84
|
| ------------------ | ------------- | -------------------------------------------------- |
|
|
74
85
|
| `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
|
|
75
|
-
| `
|
|
76
|
-
| `trackNonExistent` | yes | Indicates if the library should track those domains and entities that doesn't exist. |
|
|
86
|
+
| `options` | yes | An object containing the configuration options. |
|
|
77
87
|
|
|
78
|
-
|
|
88
|
+
#### Configuration options
|
|
79
89
|
|
|
80
|
-
|
|
90
|
+
| Parameter | Optional | Default | Description |
|
|
91
|
+
| ------------------ | ------------- | ------- | -------------------------------------------------- |
|
|
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
|
+
| `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. |
|
|
81
94
|
|
|
82
|
-
|
|
83
|
-
interface Tracked {
|
|
84
|
-
entities: string[];
|
|
85
|
-
domains: string[];
|
|
86
|
-
}
|
|
95
|
+
### Methods
|
|
87
96
|
|
|
88
|
-
|
|
89
|
-
```
|
|
97
|
+
#### getRenderer
|
|
90
98
|
|
|
91
|
-
|
|
99
|
+
Returns a `Promise` than once it resolved returns an instance of the [HomeAssistantJavaScriptTemplatesRenderer](#homeassistantjavascripttemplatesrenderer-class) class.
|
|
92
100
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
> 3. The rest of the methods will track only entities.
|
|
101
|
+
### HomeAssistantJavaScriptTemplatesRenderer class
|
|
102
|
+
|
|
103
|
+
This class is only exported as a type in the package, you cannot import it directly. An instance of this class will be returned by the promise that is returned by the [getRenderer method](#getrenderer) of the [HomeAssistantJavaScriptTemplates class](#homeassistantjavascripttemplates-class).
|
|
97
104
|
|
|
98
105
|
### Methods
|
|
99
106
|
|
|
@@ -103,31 +110,30 @@ This property will return an object with two properties (`entities` and `domains
|
|
|
103
110
|
renderTemplate(template: string): any
|
|
104
111
|
```
|
|
105
112
|
|
|
106
|
-
This
|
|
113
|
+
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`.
|
|
107
114
|
|
|
108
|
-
####
|
|
115
|
+
#### trackTemplate
|
|
109
116
|
|
|
110
117
|
```typescript
|
|
111
|
-
|
|
118
|
+
trackTemplate(
|
|
119
|
+
template: string,
|
|
120
|
+
renderingFunction: (result?: any) => void
|
|
121
|
+
): () => void
|
|
112
122
|
```
|
|
113
123
|
|
|
114
|
-
This method
|
|
124
|
+
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.
|
|
115
125
|
|
|
116
|
-
|
|
126
|
+
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.
|
|
117
127
|
|
|
118
|
-
|
|
119
|
-
cleanTrackedDomains(): void
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
This method will clean all the tracked domains until the moment, so after being called, the `tracked` property will return an empty array as `domains`.
|
|
128
|
+
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`.
|
|
123
129
|
|
|
124
130
|
#### cleanTracked
|
|
125
131
|
|
|
126
132
|
```typescript
|
|
127
|
-
cleanTracked(): void
|
|
133
|
+
cleanTracked(entityId?: string): void
|
|
128
134
|
```
|
|
129
135
|
|
|
130
|
-
This method will clean
|
|
136
|
+
This method will clean the template tracking for a specific entity or will clean all the template trackings if no entity id is specified.
|
|
131
137
|
|
|
132
138
|
### Objects and methods available in the templates
|
|
133
139
|
|
|
@@ -339,7 +345,7 @@ user_agent
|
|
|
339
345
|
```javascript
|
|
340
346
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
341
347
|
|
|
342
|
-
const
|
|
348
|
+
const haJsTemplates = new HomeAssistantJavaScriptTemplates(
|
|
343
349
|
document.querySelector('home-assistant')
|
|
344
350
|
);
|
|
345
351
|
|
|
@@ -349,28 +355,43 @@ const renderer = new HomeAssistantJavaScriptTemplates(
|
|
|
349
355
|
* Return the value of the attribute prefixed with "sn: "
|
|
350
356
|
* It will return something like "sn: 123456"
|
|
351
357
|
*/
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
358
|
+
haJsTemplates.getRenderer()
|
|
359
|
+
.then((renderer) => {
|
|
360
|
+
const result = renderer.renderTemplate(`
|
|
361
|
+
const deviceId = device_id("binary_sensor.koffiezetapparaat_aan");
|
|
362
|
+
const serialNumber = device_attr(deviceId, "serial_number");
|
|
363
|
+
return "sn:" + serialNumber;
|
|
364
|
+
`);
|
|
365
|
+
console.log(result);
|
|
366
|
+
});
|
|
367
|
+
|
|
357
368
|
```
|
|
358
369
|
|
|
359
|
-
#### Get all the available updates
|
|
370
|
+
#### Get all the available updates and update an HTML element with the result with entity changes
|
|
360
371
|
|
|
361
372
|
```javascript
|
|
362
373
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
363
374
|
|
|
364
|
-
const
|
|
375
|
+
const haJsTemplates = new HomeAssistantJavaScriptTemplates(
|
|
365
376
|
document.querySelector('home-assistant')
|
|
366
377
|
);
|
|
367
378
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
379
|
+
haJsTemplates.getRenderer()
|
|
380
|
+
.then((renderer) => {
|
|
381
|
+
const element = document.querySelector('#my-element');
|
|
382
|
+
renderer.trackTemplate(
|
|
383
|
+
`
|
|
384
|
+
const udatesEntities = states.update;
|
|
385
|
+
const updateEntitiesValues = Object.values(udatesEntities);
|
|
386
|
+
const updatesEntitiesOn = updateEntitiesValues.filter((entity) => entity.state === 'on');
|
|
387
|
+
return updatesEntitiesOn.length;
|
|
388
|
+
`,
|
|
389
|
+
(result) => {
|
|
390
|
+
element.innerHTML = result;
|
|
391
|
+
}
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
|
|
374
395
|
```
|
|
375
396
|
|
|
376
397
|
[Optional chaining operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
interface Options {
|
|
2
|
+
throwErrors?: boolean;
|
|
3
|
+
throwWarnings?: boolean;
|
|
4
|
+
}
|
|
5
|
+
type RenderingFunction = (result?: any) => void;
|
|
1
6
|
interface Area {
|
|
2
7
|
area_id: string;
|
|
3
8
|
name: string;
|
|
@@ -32,18 +37,34 @@ interface Hass {
|
|
|
32
37
|
interface HomeAssistant extends HTMLElement {
|
|
33
38
|
hass: Hass;
|
|
34
39
|
}
|
|
35
|
-
interface
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
interface HassConnection {
|
|
41
|
+
conn: {
|
|
42
|
+
subscribeMessage: <T>(callback: (response: T) => void, options: Record<string, unknown>) => void;
|
|
43
|
+
};
|
|
38
44
|
}
|
|
39
|
-
declare
|
|
40
|
-
|
|
45
|
+
declare global {
|
|
46
|
+
interface Window {
|
|
47
|
+
hassConnection: Promise<HassConnection>;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
51
|
+
constructor(ha: HomeAssistant, options: Options);
|
|
52
|
+
private _throwErrors;
|
|
53
|
+
private _throwWarnings;
|
|
54
|
+
private _subscriptions;
|
|
41
55
|
private _scopped;
|
|
42
|
-
private
|
|
56
|
+
private _watchForEntitiesChange;
|
|
57
|
+
private _entityWatchCallback;
|
|
58
|
+
private _storeTracked;
|
|
59
|
+
private _untrackTemplate;
|
|
43
60
|
renderTemplate(template: string): any;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
61
|
+
trackTemplate(template: string, renderingFunction: RenderingFunction): () => void;
|
|
62
|
+
cleanTracked(entityId?: string): void;
|
|
63
|
+
}
|
|
64
|
+
declare class HomeAssistantJavaScriptTemplates {
|
|
65
|
+
constructor(ha: HomeAssistant, options?: Options);
|
|
66
|
+
private _renderer;
|
|
67
|
+
getRenderer(): Promise<HomeAssistantJavaScriptTemplatesRenderer>;
|
|
48
68
|
}
|
|
49
|
-
export { HomeAssistantJavaScriptTemplates as default
|
|
69
|
+
export { HomeAssistantJavaScriptTemplates as default };
|
|
70
|
+
export type { HomeAssistant, HomeAssistantJavaScriptTemplatesRenderer, HassConnection, Hass };
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,
|
|
1
|
+
var t,e,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={}));var s=function(t){return t.reduce((function(t,e){var i=e[0],s=e[1];return t[i.replace(n,"$2")]=s,t}),{})},r=function(t){return t.includes(".")};function a(n,a){var o=function(){return Object.entries(n.hass.areas)},c=function(){return Object.entries(n.hass.entities)},u=new Set,d=function(t,e){a&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},_=function(t){return d("Entity",t)},h=function(t){return d("Domain",t)},p=function(t){n.hass.states[t]?u.add(t):_(t)};return{get hass(){return n.hass},states:new Proxy((function(t){var e;if(r(t))return p(t),null===(e=n.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(r(e))return p(e),n.hass.states[e];var i=Object.entries(n.hass.states).filter((function(t){return t[0].startsWith(e)}));return i.length||h(e),new Proxy(s(i),{get:function(t,i){return p("".concat(e,".").concat(i)),t[i]}})}}),is_state:function(t,e){var i;return p(t),(null===(i=n.hass.states[t])||void 0===i?void 0:i.state)===e},state_attr:function(t,e){var i,s;return p(t),null===(s=null===(i=n.hass.states[t])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,i){return this.state_attr(t,e)===i},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(_(e),!1)},entities:new Proxy((function(t){if(void 0===t)return n.hass.entities;if(r(t))return p(t),n.hass.entities[t];var e=c().filter((function(e){return e[0].startsWith(t)}));return e.length||h(t),new Proxy(s(e),{get:function(e,i){return p("".concat(t,".").concat(i)),e[i]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var i;return p(t),null===(i=n.hass.entities[t])||void 0===i?void 0:i[e]},is_entity_prop:function(t,e,i){return this.entity_prop(t,e)===i},devices:new Proxy((function(t){if(void 0===t)return n.hass.devices;if(r(t))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[t]}),{get:function(t,e){if(r(e))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[e]}}),device_attr:function(t,e){var i;return null===(i=n.hass.devices[t])||void 0===i?void 0:i[e]},is_device_attr:function(t,e,i){return this.device_attr(t,e)===i},device_id:function(t){var e;return p(t),null===(e=n.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return o().map((function(t){return t[1].area_id}))},area_id:function(t){var i,s;if(t in n.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=o().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var i,s,r;t in n.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var c=o().find((function(e){var i=e[1];return i.area_id===t||i.area_id===r}));return null===(s=null==c?void 0:c[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?c().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?Object.entries(n.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get tracked(){return u},cleanTracked:function(){u.clear()}}}var o=function(){function t(t,e){var i=e.throwErrors,n=void 0!==i&&i,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=n,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=a(t,r),this._watchForEntitiesChange()}return t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:"subscribe_events",event_type:"state_changed"})}))},t.prototype._entityWatchCallback=function(t){var e=this;if(this._subscriptions.size){var i=t.data.entity_id;this._subscriptions.has(i)&&this._subscriptions.get(i).forEach((function(t,i){t.forEach((function(t){e.trackTemplate(i,t)}))}))}},t.prototype._storeTracked=function(t,e){var i=this;this._scopped.tracked.forEach((function(n){if(i._subscriptions.has(n)){var s=i._subscriptions.get(n);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else i._subscriptions.set(n,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var i=this;this._subscriptions.forEach((function(n,s){if(n.has(t)){var r=n.get(t);r.has(e)&&r.delete(e),0===r.size&&(n.delete(t),0===n.size&&i._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);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(e))(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(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var i=this;this._scopped.cleanTracked();var n=this.renderTemplate(t);return this._storeTracked(t,e),e(n),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),c=function(){function t(t,e){var i,n;void 0===e&&(e={}),this._renderer=(i=function(){return t.hass},n=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=i();n(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new o(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();export{c as default};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
interface Options {
|
|
2
|
+
throwErrors?: boolean;
|
|
3
|
+
throwWarnings?: boolean;
|
|
4
|
+
}
|
|
5
|
+
type RenderingFunction = (result?: any) => void;
|
|
1
6
|
interface Area {
|
|
2
7
|
area_id: string;
|
|
3
8
|
name: string;
|
|
@@ -32,18 +37,34 @@ interface Hass {
|
|
|
32
37
|
interface HomeAssistant extends HTMLElement {
|
|
33
38
|
hass: Hass;
|
|
34
39
|
}
|
|
35
|
-
interface
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
interface HassConnection {
|
|
41
|
+
conn: {
|
|
42
|
+
subscribeMessage: <T>(callback: (response: T) => void, options: Record<string, unknown>) => void;
|
|
43
|
+
};
|
|
38
44
|
}
|
|
39
|
-
declare
|
|
40
|
-
|
|
45
|
+
declare global {
|
|
46
|
+
interface Window {
|
|
47
|
+
hassConnection: Promise<HassConnection>;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
declare class HomeAssistantJavaScriptTemplatesRenderer {
|
|
51
|
+
constructor(ha: HomeAssistant, options: Options);
|
|
52
|
+
private _throwErrors;
|
|
53
|
+
private _throwWarnings;
|
|
54
|
+
private _subscriptions;
|
|
41
55
|
private _scopped;
|
|
42
|
-
private
|
|
56
|
+
private _watchForEntitiesChange;
|
|
57
|
+
private _entityWatchCallback;
|
|
58
|
+
private _storeTracked;
|
|
59
|
+
private _untrackTemplate;
|
|
43
60
|
renderTemplate(template: string): any;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
61
|
+
trackTemplate(template: string, renderingFunction: RenderingFunction): () => void;
|
|
62
|
+
cleanTracked(entityId?: string): void;
|
|
63
|
+
}
|
|
64
|
+
declare class HomeAssistantJavaScriptTemplates {
|
|
65
|
+
constructor(ha: HomeAssistant, options?: Options);
|
|
66
|
+
private _renderer;
|
|
67
|
+
getRenderer(): Promise<HomeAssistantJavaScriptTemplatesRenderer>;
|
|
48
68
|
}
|
|
49
|
-
export { HomeAssistantJavaScriptTemplates as default
|
|
69
|
+
export { HomeAssistantJavaScriptTemplates as default };
|
|
70
|
+
export type { HomeAssistant, HomeAssistantJavaScriptTemplatesRenderer, HassConnection, Hass };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,
|
|
1
|
+
"use strict";var t,e,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={}));var s=function(t){return t.reduce((function(t,e){var i=e[0],s=e[1];return t[i.replace(n,"$2")]=s,t}),{})},r=function(t){return t.includes(".")};function a(n,a){var o=function(){return Object.entries(n.hass.areas)},c=function(){return Object.entries(n.hass.entities)},u=new Set,d=function(t,e){a&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},_=function(t){return d("Entity",t)},h=function(t){return d("Domain",t)},p=function(t){n.hass.states[t]?u.add(t):_(t)};return{get hass(){return n.hass},states:new Proxy((function(t){var e;if(r(t))return p(t),null===(e=n.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(r(e))return p(e),n.hass.states[e];var i=Object.entries(n.hass.states).filter((function(t){return t[0].startsWith(e)}));return i.length||h(e),new Proxy(s(i),{get:function(t,i){return p("".concat(e,".").concat(i)),t[i]}})}}),is_state:function(t,e){var i;return p(t),(null===(i=n.hass.states[t])||void 0===i?void 0:i.state)===e},state_attr:function(t,e){var i,s;return p(t),null===(s=null===(i=n.hass.states[t])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,i){return this.state_attr(t,e)===i},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(_(e),!1)},entities:new Proxy((function(t){if(void 0===t)return n.hass.entities;if(r(t))return p(t),n.hass.entities[t];var e=c().filter((function(e){return e[0].startsWith(t)}));return e.length||h(t),new Proxy(s(e),{get:function(e,i){return p("".concat(t,".").concat(i)),e[i]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var i;return p(t),null===(i=n.hass.entities[t])||void 0===i?void 0:i[e]},is_entity_prop:function(t,e,i){return this.entity_prop(t,e)===i},devices:new Proxy((function(t){if(void 0===t)return n.hass.devices;if(r(t))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[t]}),{get:function(t,e){if(r(e))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[e]}}),device_attr:function(t,e){var i;return null===(i=n.hass.devices[t])||void 0===i?void 0:i[e]},is_device_attr:function(t,e,i){return this.device_attr(t,e)===i},device_id:function(t){var e;return p(t),null===(e=n.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return o().map((function(t){return t[1].area_id}))},area_id:function(t){var i,s;if(t in n.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=o().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var i,s,r;t in n.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var c=o().find((function(e){var i=e[1];return i.area_id===t||i.area_id===r}));return null===(s=null==c?void 0:c[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?c().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?Object.entries(n.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get tracked(){return u},cleanTracked:function(){u.clear()}}}var o=function(){function t(t,e){var i=e.throwErrors,n=void 0!==i&&i,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=n,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=a(t,r),this._watchForEntitiesChange()}return t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:"subscribe_events",event_type:"state_changed"})}))},t.prototype._entityWatchCallback=function(t){var e=this;if(this._subscriptions.size){var i=t.data.entity_id;this._subscriptions.has(i)&&this._subscriptions.get(i).forEach((function(t,i){t.forEach((function(t){e.trackTemplate(i,t)}))}))}},t.prototype._storeTracked=function(t,e){var i=this;this._scopped.tracked.forEach((function(n){if(i._subscriptions.has(n)){var s=i._subscriptions.get(n);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else i._subscriptions.set(n,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var i=this;this._subscriptions.forEach((function(n,s){if(n.has(t)){var r=n.get(t);r.has(e)&&r.delete(e),0===r.size&&(n.delete(t),0===n.size&&i._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);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(e))(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(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var i=this;this._scopped.cleanTracked();var n=this.renderTemplate(t);return this._storeTracked(t,e),e(n),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),c=function(){function t(t,e){var i,n;void 0===e&&(e={}),this._renderer=(i=function(){return t.hass},n=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=i();n(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new o(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();module.exports=c;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "A JavaScript utility to render Home Assistant JavaScript templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"home-assistant",
|
|
@@ -46,14 +46,14 @@
|
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
|
-
"@types/jest": "^29.5.
|
|
50
|
-
"@types/node": "^22.
|
|
49
|
+
"@types/jest": "^29.5.13",
|
|
50
|
+
"@types/node": "^22.7.7",
|
|
51
51
|
"jest": "^29.7.0",
|
|
52
52
|
"jest-environment-jsdom": "^29.7.0",
|
|
53
|
-
"rollup": "^4.
|
|
53
|
+
"rollup": "^4.24.0",
|
|
54
54
|
"rollup-plugin-ts": "^3.4.5",
|
|
55
55
|
"ts-jest": "^29.2.5",
|
|
56
|
-
"tslib": "^2.
|
|
57
|
-
"typescript": "^5.
|
|
56
|
+
"tslib": "^2.8.0",
|
|
57
|
+
"typescript": "^5.6.3"
|
|
58
58
|
}
|
|
59
59
|
}
|