home-assistant-javascript-templates 3.0.0 → 3.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 +50 -3
- package/dist/esm/index.d.ts +9 -1
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ renderer.renderTemplate('... template string ...');
|
|
|
54
54
|
|
|
55
55
|
## API
|
|
56
56
|
|
|
57
|
-
The package exposes a class that needs to be instantiated and is this
|
|
57
|
+
The package exposes a class that needs to be instantiated and is this instance the one that you need to use in your code.
|
|
58
58
|
|
|
59
59
|
### HomeAssistantJavaScriptTemplates class
|
|
60
60
|
|
|
@@ -69,9 +69,56 @@ new HomeAssistantJavaScriptTemplates(ha, throwErrors = false);
|
|
|
69
69
|
| `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
|
|
70
70
|
| `throwErrors` | yes | Indicates if the library should throw if the template contains any error. If not it will log the errors as a warning in the console and return `undefined` instead. |
|
|
71
71
|
|
|
72
|
-
###
|
|
72
|
+
### Properties
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
#### tracked
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
interface Tracked {
|
|
78
|
+
entities: string[];
|
|
79
|
+
domains: string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get tracked(): Tracked
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This property will return an object with two properties (`entities` and `domains`). Each of these properties will be an array containing the entities or ids that have been tracked when the templates have been rendered. If some domain or entity was not reached because it was inside a condition that never met, then it will not be included in the `tracked` property. Only those entities or domains that were called during the rendering by the code using [states](#states), [is_state](#is_state), [state_attr](#state_attr), [is_state_attr](#is_state_attr) or [has_value](#has_value) will be included.
|
|
86
|
+
|
|
87
|
+
>Note: take into account that the domains will be only tracked if the `states` object is used accesing a domain. For example `states('device_tracker.paulus')` or `states['device_tracker.paulus']` will track the entity `device_tracker.paulus` but not the domain `device_tracker` but `states.device_tracker.paulus` will track both, the domain `device_tracker` and the entity `device_tracker.paulus`. The rest of the methods will track only entities.
|
|
88
|
+
|
|
89
|
+
### Methods
|
|
90
|
+
|
|
91
|
+
#### renderTemplate
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
renderTemplate(template: string): any
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
This is the main method to render `JavaScript` templates, 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`.
|
|
98
|
+
|
|
99
|
+
#### cleanTrackedEntities
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
cleanTrackedEntities(): void
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
This method will clean all the tracked entities until the moment, so after being called, the `tracked` property will return an empty array as `entities`.
|
|
106
|
+
|
|
107
|
+
#### cleanTrackedDomains
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
cleanTrackedDomains(): void
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
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`.
|
|
114
|
+
|
|
115
|
+
#### cleanTracked
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
cleanTracked(): void
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
This method will clean all the tracked entities and domains until the moment. It is the same as calling `cleanTrackedEntities` and `cleanTrackedDomains` consecutively.
|
|
75
122
|
|
|
76
123
|
### Objects and methods available in the templates
|
|
77
124
|
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -31,10 +31,18 @@ interface Hass {
|
|
|
31
31
|
interface HomeAssistant extends HTMLElement {
|
|
32
32
|
hass: Hass;
|
|
33
33
|
}
|
|
34
|
+
interface Tracked {
|
|
35
|
+
entities: string[];
|
|
36
|
+
domains: string[];
|
|
37
|
+
}
|
|
34
38
|
declare class HomeAssistantJavaScriptTemplates {
|
|
35
39
|
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
36
40
|
private _scopped;
|
|
37
41
|
private _errors;
|
|
38
|
-
renderTemplate(template: string):
|
|
42
|
+
renderTemplate(template: string): any;
|
|
43
|
+
get tracked(): Tracked;
|
|
44
|
+
cleanTrackedEntities(): void;
|
|
45
|
+
cleanTrackedDomains(): void;
|
|
46
|
+
cleanTracked(): void;
|
|
39
47
|
}
|
|
40
48
|
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t,
|
|
1
|
+
var e,t,i=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat("[home-assistant-javascript-templates]",": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return r().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().find((function(t){return t[1].name===e}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.entities).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(e),this._errors=t}return e.prototype.renderTemplate=function(e){var t=e.includes("return")?e:"return ".concat(e),i=new Function("hass","states","is_state","state_attr","is_state_attr","has_value","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","".concat('"use strict";'," ").concat(t));try{return i(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();export{r as default};
|
package/dist/index.d.ts
CHANGED
|
@@ -31,10 +31,18 @@ interface Hass {
|
|
|
31
31
|
interface HomeAssistant extends HTMLElement {
|
|
32
32
|
hass: Hass;
|
|
33
33
|
}
|
|
34
|
+
interface Tracked {
|
|
35
|
+
entities: string[];
|
|
36
|
+
domains: string[];
|
|
37
|
+
}
|
|
34
38
|
declare class HomeAssistantJavaScriptTemplates {
|
|
35
39
|
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
36
40
|
private _scopped;
|
|
37
41
|
private _errors;
|
|
38
|
-
renderTemplate(template: string):
|
|
42
|
+
renderTemplate(template: string): any;
|
|
43
|
+
get tracked(): Tracked;
|
|
44
|
+
cleanTrackedEntities(): void;
|
|
45
|
+
cleanTrackedDomains(): void;
|
|
46
|
+
cleanTracked(): void;
|
|
39
47
|
}
|
|
40
48
|
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t,
|
|
1
|
+
"use strict";var e,t,i=/^([a-z_]+)\.(\w+)$/;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));var s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("".concat("[home-assistant-javascript-templates]",": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,t){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[t]},is_state_attr:function(e,t,i){return this.state_attr(e,t)===i},has_value:function(t){return!!this.states(t)&&!(this.is_state(t,e.UNKNOWN)||this.is_state(t,e.UNAVAILABLE))},device_attr:function(e,t){var i;return null===(i=n.hass.devices[e])||void 0===i?void 0:i[t]},is_device_attr:function(e,t,i){return this.device_attr(e,t)===i},device_id:function(e){var t;return null===(t=n.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return r().map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().find((function(t){return t[1].name===e}));return null===(i=null==a?void 0:a[1])||void 0===i?void 0:i.area_id},area_name:function(e){var i,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.entities).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.hass.devices).filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},get user_name(){return n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(e),this._errors=t}return e.prototype.renderTemplate=function(e){var t=e.includes("return")?e:"return ".concat(e),i=new Function("hass","states","is_state","state_attr","is_state_attr","has_value","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","".concat('"use strict";'," ").concat(t));try{return i(this._scopped.hass,this._scopped.states,this._scopped.is_state.bind(this._scopped),this._scopped.state_attr.bind(this._scopped),this._scopped.is_state_attr.bind(this._scopped),this._scopped.has_value.bind(this._scopped),this._scopped.device_attr.bind(this._scopped),this._scopped.is_device_attr.bind(this._scopped),this._scopped.device_id.bind(this._scopped),this._scopped.areas.bind(this._scopped),this._scopped.area_id.bind(this._scopped),this._scopped.area_name.bind(this._scopped),this._scopped.area_entities.bind(this._scopped),this._scopped.area_devices.bind(this._scopped),this._scopped.user_name,this._scopped.user_is_admin,this._scopped.user_is_owner)}catch(e){if(this._errors)throw e;return void console.warn(e)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();module.exports=r;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "A JavaScript utility to render Home Assistant JavaScript templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"home-assistant",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
|
-
"@types/jest": "^29.5.
|
|
50
|
-
"@types/node": "^20.11.
|
|
49
|
+
"@types/jest": "^29.5.12",
|
|
50
|
+
"@types/node": "^20.11.17",
|
|
51
51
|
"jest": "^29.7.0",
|
|
52
|
-
"rollup": "^4.
|
|
52
|
+
"rollup": "^4.10.0",
|
|
53
53
|
"rollup-plugin-ts": "^3.4.5",
|
|
54
54
|
"ts-jest": "^29.1.2",
|
|
55
55
|
"typescript": "^5.3.3"
|