home-assistant-javascript-templates 1.2.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -15
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ pnpm add home-assistant-javascript-templates
|
|
|
34
34
|
const HomeAssistantJavaScriptTemplates = require('home-assistant-javascript-templates');
|
|
35
35
|
|
|
36
36
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
37
|
-
document.querySelector('home-assistant')
|
|
37
|
+
document.querySelector('home-assistant')
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
renderer.renderTemplate('... template string ...');
|
|
@@ -43,10 +43,10 @@ renderer.renderTemplate('... template string ...');
|
|
|
43
43
|
#### Usage with ES6 modules
|
|
44
44
|
|
|
45
45
|
```javascript
|
|
46
|
-
import HomeAssistantJavaScriptTemplates
|
|
46
|
+
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
47
47
|
|
|
48
48
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
49
|
-
document.querySelector('home-assistant')
|
|
49
|
+
document.querySelector('home-assistant')
|
|
50
50
|
);
|
|
51
51
|
|
|
52
52
|
renderer.renderTemplate('... template string ...');
|
|
@@ -61,12 +61,12 @@ The package exposes a class that needs to be instantiated and is this isntance t
|
|
|
61
61
|
Main class of the library, it is the `default` export in the package.
|
|
62
62
|
|
|
63
63
|
```typescript
|
|
64
|
-
new HomeAssistantJavaScriptTemplates(
|
|
64
|
+
new HomeAssistantJavaScriptTemplates(ha, throwErrors = false);
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
| Parameter | Optional | Description |
|
|
68
68
|
| ------------- | ------------- | -------------------------------------------------- |
|
|
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
|
### renderTemplate method
|
|
@@ -77,11 +77,11 @@ This is the main method to render `JavaScript` templates, it needs a string as a
|
|
|
77
77
|
|
|
78
78
|
#### hass
|
|
79
79
|
|
|
80
|
-
The
|
|
80
|
+
The `hass` object
|
|
81
81
|
|
|
82
82
|
#### states
|
|
83
83
|
|
|
84
|
-
`states` could be used in two ways, as a function or as an object. When using it as function it only allows an entity id as a parameter and it will return the state of that entity.
|
|
84
|
+
`states` could be used in two ways, as a function or as an object. When using it as function it only allows an entity id (containing the domain) as a parameter and it will return the state of that entity. As an object it allows you to access a domain or the full entity id.
|
|
85
85
|
|
|
86
86
|
>Note: If you try to use `states` as a function sending a domain it will throw an error.
|
|
87
87
|
|
|
@@ -91,9 +91,12 @@ states('device_tracker.paulus') // returns the state of the entity id 'device_tr
|
|
|
91
91
|
|
|
92
92
|
// Using states as an object
|
|
93
93
|
states['device_tracker.paulus'].state // returns the state of the entity id 'device_tracker.paulus'
|
|
94
|
-
states
|
|
94
|
+
states.device_tracker.paulus.state // returns the state of the entity id 'device_tracker.paulus'
|
|
95
|
+
states.device_tracker // returns an object constaining all the entities of the 'device_tracker' domain
|
|
95
96
|
```
|
|
96
97
|
|
|
98
|
+
>Note: Avoid using `states['device_tracker.paulus'].state` or `states.device_tracker.paulus.state`, instead use `states('device_tracker.paulus')` which will return `undefined` if the device id doesn‘t exist or the entity isn’t ready yet (the former will throw an error). If you still want to use them it is advisable to use the [Optional chaining operator], e.g. `states['device_tracker.paulus']?.state` or `states.device_tracker?.paulus?.state`.
|
|
99
|
+
|
|
97
100
|
#### is_state
|
|
98
101
|
|
|
99
102
|
Method to check if the state of an entity is equal to a certain value. It returns a `boolean`. If the entity id doesn‘t exist it returns `false`.
|
|
@@ -131,7 +134,7 @@ has_value('sensor.my_sensor')
|
|
|
131
134
|
Method that returns the value of an attribute for the given device id or `undefined` if it doesn’t exist.
|
|
132
135
|
|
|
133
136
|
```javascript
|
|
134
|
-
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
|
|
137
|
+
device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer')
|
|
135
138
|
```
|
|
136
139
|
|
|
137
140
|
#### is_device_attr
|
|
@@ -228,7 +231,7 @@ user_is_owner
|
|
|
228
231
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
229
232
|
|
|
230
233
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
231
|
-
document.querySelector('home-assistant')
|
|
234
|
+
document.querySelector('home-assistant')
|
|
232
235
|
);
|
|
233
236
|
|
|
234
237
|
/**
|
|
@@ -250,12 +253,15 @@ renderer.renderTemplate(`
|
|
|
250
253
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
251
254
|
|
|
252
255
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
253
|
-
document.querySelector('home-assistant')
|
|
256
|
+
document.querySelector('home-assistant')
|
|
254
257
|
);
|
|
255
258
|
|
|
256
259
|
renderer.renderTemplate(`
|
|
257
|
-
const udatesEntities = states
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
+
const udatesEntities = states.update;
|
|
261
|
+
const updateEntitiesValues = Object.values(udatesEntities);
|
|
262
|
+
const updatesEntitiesOn = updateEntitiesValues.filter((entity) => entity.state === 'on');
|
|
263
|
+
return updatesEntitiesOn.length;
|
|
260
264
|
`);
|
|
261
|
-
```
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
[Optional chaining operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -28,10 +28,13 @@ interface Hass {
|
|
|
28
28
|
states: Record<string, State>;
|
|
29
29
|
user: User;
|
|
30
30
|
}
|
|
31
|
+
interface HomeAssistant extends HTMLElement {
|
|
32
|
+
hass: Hass;
|
|
33
|
+
}
|
|
31
34
|
declare class HomeAssistantJavaScriptTemplates {
|
|
32
|
-
constructor(
|
|
35
|
+
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
33
36
|
private _scopped;
|
|
34
37
|
private _errors;
|
|
35
38
|
renderTemplate(template: string): string;
|
|
36
39
|
}
|
|
37
|
-
export { HomeAssistantJavaScriptTemplates as default, Hass };
|
|
40
|
+
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t
|
|
1
|
+
var e,t,s=/^([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 i=function(e){return e.includes(".")};function r(r){var a=function(){return Object.entries(r.hass.areas)};return{get hass(){return r.hass},states:new Proxy((function(e){var t;if(i(e))return null===(t=r.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){return i(t)?r.hass.states[t]:Object.entries(r.hass.states).filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var i=t[0],r=t[1];return e[i.replace(s,"$2")]=r,e}),{})}}),is_state:function(e,t){var s;return(null===(s=r.hass.states[e])||void 0===s?void 0:s.state)===t},state_attr:function(e,t){var s,i;return null===(i=null===(s=r.hass.states[e])||void 0===s?void 0:s.attributes)||void 0===i?void 0:i[t]},is_state_attr:function(e,t,s){return this.state_attr(e,t)===s},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 s;return null===(s=r.hass.devices[e])||void 0===s?void 0:s[t]},is_device_attr:function(e,t,s){return this.device_attr(e,t)===s},device_id:function(e){var t;return null===(t=r.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return a().map((function(e){return e[1].area_id}))},area_id:function(e){var s;if(e in r.hass.devices)return this.device_attr(e,t.AREA_ID);var i=this.device_id(e);if(i)return this.device_attr(i,t.AREA_ID);var n=a().find((function(t){return t[1].name===e}));return null===(s=null==n?void 0:n[1])||void 0===s?void 0:s.area_id},area_name:function(e){var s,i;e in r.hass.devices&&(i=this.device_attr(e,t.AREA_ID));var n=this.device_id(e);n&&(i=this.device_attr(n,t.AREA_ID));var d=a().find((function(t){var s=t[1];return s.area_id===e||s.area_id===i}));return null===(s=null==d?void 0:d[1])||void 0===s?void 0:s.name},area_entities:function(e){var t=a().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(r.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=a().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(r.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 r.hass.user.name},get user_is_admin(){return r.hass.user.is_admin},get user_is_owner(){return r.hass.user.is_owner}}}var a=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=r(e),this._errors=t}return e.prototype.renderTemplate=function(e){var t=e.includes("return")?e:"return ".concat(e),s=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 s(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)}},e}();export{a as default};
|
package/dist/index.d.ts
CHANGED
|
@@ -28,10 +28,13 @@ interface Hass {
|
|
|
28
28
|
states: Record<string, State>;
|
|
29
29
|
user: User;
|
|
30
30
|
}
|
|
31
|
+
interface HomeAssistant extends HTMLElement {
|
|
32
|
+
hass: Hass;
|
|
33
|
+
}
|
|
31
34
|
declare class HomeAssistantJavaScriptTemplates {
|
|
32
|
-
constructor(
|
|
35
|
+
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
33
36
|
private _scopped;
|
|
34
37
|
private _errors;
|
|
35
38
|
renderTemplate(template: string): string;
|
|
36
39
|
}
|
|
37
|
-
export { HomeAssistantJavaScriptTemplates as default, Hass };
|
|
40
|
+
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,s=/^([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 i=function(e){return e.includes(".")};function r(r){var a=function(){return Object.entries(r.hass.areas)};return{get hass(){return r.hass},states:new Proxy((function(e){var t;if(i(e))return null===(t=r.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){return i(t)?r.hass.states[t]:Object.entries(r.hass.states).filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var i=t[0],r=t[1];return e[i.replace(s,"$2")]=r,e}),{})}}),is_state:function(e,t){var s;return(null===(s=r.hass.states[e])||void 0===s?void 0:s.state)===t},state_attr:function(e,t){var s,i;return null===(i=null===(s=r.hass.states[e])||void 0===s?void 0:s.attributes)||void 0===i?void 0:i[t]},is_state_attr:function(e,t,s){return this.state_attr(e,t)===s},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 s;return null===(s=r.hass.devices[e])||void 0===s?void 0:s[t]},is_device_attr:function(e,t,s){return this.device_attr(e,t)===s},device_id:function(e){var t;return null===(t=r.hass.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return a().map((function(e){return e[1].area_id}))},area_id:function(e){var s;if(e in r.hass.devices)return this.device_attr(e,t.AREA_ID);var i=this.device_id(e);if(i)return this.device_attr(i,t.AREA_ID);var n=a().find((function(t){return t[1].name===e}));return null===(s=null==n?void 0:n[1])||void 0===s?void 0:s.area_id},area_name:function(e){var s,i;e in r.hass.devices&&(i=this.device_attr(e,t.AREA_ID));var n=this.device_id(e);n&&(i=this.device_attr(n,t.AREA_ID));var d=a().find((function(t){var s=t[1];return s.area_id===e||s.area_id===i}));return null===(s=null==d?void 0:d[1])||void 0===s?void 0:s.name},area_entities:function(e){var t=a().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(r.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=a().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(r.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 r.hass.user.name},get user_is_admin(){return r.hass.user.is_admin},get user_is_owner(){return r.hass.user.is_owner}}}var a=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=r(e),this._errors=t}return e.prototype.renderTemplate=function(e){var t=e.includes("return")?e:"return ".concat(e),s=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 s(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)}},e}();module.exports=a;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A JavaScript utility to render Home Assistant JavaScript templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"home-assistant",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
49
|
"@types/jest": "^29.5.11",
|
|
50
|
-
"@types/node": "^20.11.
|
|
50
|
+
"@types/node": "^20.11.9",
|
|
51
51
|
"jest": "^29.7.0",
|
|
52
52
|
"rollup": "^4.9.6",
|
|
53
53
|
"rollup-plugin-ts": "^3.4.5",
|