home-assistant-javascript-templates 1.1.0 → 2.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 +20 -17
- 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 ...');
|
|
@@ -46,7 +46,7 @@ renderer.renderTemplate('... template string ...');
|
|
|
46
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,13 +61,13 @@ 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
|
-
| `
|
|
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. |
|
|
69
|
+
| `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
|
|
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
|
|
73
73
|
|
|
@@ -77,18 +77,21 @@ 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
|
-
|
|
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. When using it as an object, you can use also an entity id but in those cases it will return the entire state object, so you need to access its `state` property to get the state value. When using it as an object with a domain, it will return an array with all the states of that domain.
|
|
85
|
+
|
|
86
|
+
>Note: If you try to use `states` as a function sending a domain it will throw an error.
|
|
85
87
|
|
|
86
88
|
```javascript
|
|
87
89
|
// Using states as a function
|
|
88
|
-
states('device_tracker.paulus')
|
|
90
|
+
states('device_tracker.paulus') // returns the state of the entity id 'device_tracker.paulus'
|
|
89
91
|
|
|
90
92
|
// Using states as an object
|
|
91
|
-
states['device_tracker.paulus'].state
|
|
93
|
+
states['device_tracker.paulus'].state // returns the state of the entity id 'device_tracker.paulus'
|
|
94
|
+
states['device_tracker'] // returns an array with all the states of the 'device_tracker' domain
|
|
92
95
|
```
|
|
93
96
|
|
|
94
97
|
#### is_state
|
|
@@ -101,7 +104,7 @@ is_state('device_tracker.paulus', 'not_home')
|
|
|
101
104
|
|
|
102
105
|
#### state_attr
|
|
103
106
|
|
|
104
|
-
Method to return the value of the state attribute or `
|
|
107
|
+
Method to return the value of the state attribute or `undefined` if it doesn’t exist.
|
|
105
108
|
|
|
106
109
|
```javascript
|
|
107
110
|
state_attr('device_tracker.paulus', 'battery')
|
|
@@ -125,10 +128,10 @@ has_value('sensor.my_sensor')
|
|
|
125
128
|
|
|
126
129
|
#### device_attr
|
|
127
130
|
|
|
128
|
-
Method that returns the value of an attribute for the given device id or `
|
|
131
|
+
Method that returns the value of an attribute for the given device id or `undefined` if it doesn’t exist.
|
|
129
132
|
|
|
130
133
|
```javascript
|
|
131
|
-
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
|
|
134
|
+
device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer')
|
|
132
135
|
```
|
|
133
136
|
|
|
134
137
|
#### is_device_attr
|
|
@@ -141,7 +144,7 @@ is_device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer', 'Synology')
|
|
|
141
144
|
|
|
142
145
|
#### device_id
|
|
143
146
|
|
|
144
|
-
Method to return the device id for a given entity id or `
|
|
147
|
+
Method to return the device id for a given entity id or `undefined` if the entity doesn‘t exist.
|
|
145
148
|
|
|
146
149
|
```javascript
|
|
147
150
|
device_id('sensor.my_sensor')
|
|
@@ -157,7 +160,7 @@ areas()
|
|
|
157
160
|
|
|
158
161
|
#### area_id
|
|
159
162
|
|
|
160
|
-
Method to return the area id for a given device id, entity id, or area name. It returns `
|
|
163
|
+
Method to return the area id for a given device id, entity id, or area name. It returns `undefined` if the area doesn‘t exist.
|
|
161
164
|
|
|
162
165
|
```javascript
|
|
163
166
|
area_id('b8c1c9dd23cb82bbfa09b5657f41d04f')
|
|
@@ -167,7 +170,7 @@ area_id('Woonkamer')
|
|
|
167
170
|
|
|
168
171
|
#### area_name
|
|
169
172
|
|
|
170
|
-
Method to return the area name for a given device id, entity id, or area id. It returns `
|
|
173
|
+
Method to return the area name for a given device id, entity id, or area id. It returns `undefined` if the area doesn‘t exist.
|
|
171
174
|
|
|
172
175
|
```javascript
|
|
173
176
|
area_name('b8c1c9dd23cb82bbfa09b5657f41d04f')
|
|
@@ -225,7 +228,7 @@ user_is_owner
|
|
|
225
228
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
226
229
|
|
|
227
230
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
228
|
-
document.querySelector('home-assistant')
|
|
231
|
+
document.querySelector('home-assistant')
|
|
229
232
|
);
|
|
230
233
|
|
|
231
234
|
/**
|
|
@@ -247,7 +250,7 @@ renderer.renderTemplate(`
|
|
|
247
250
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
248
251
|
|
|
249
252
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
250
|
-
document.querySelector('home-assistant')
|
|
253
|
+
document.querySelector('home-assistant')
|
|
251
254
|
);
|
|
252
255
|
|
|
253
256
|
renderer.renderTemplate(`
|
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;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));
|
|
1
|
+
var e,t;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));function s(e,t,s){if(s||2===arguments.length)for(var i,r=0,a=t.length;r<a;r++)!i&&r in t||(i||(i=Array.prototype.slice.call(t,0,r)),i[r]=t[r]);return e.concat(i||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;function i(i){var r=function(){return Object.entries(i.hass.areas)};return{get hass(){return i.hass},states:new Proxy((function(e){var t;if(e.includes("."))return null===(t=i.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("[home-assistant-javascript-templates]: states method cannot be used with a domain, use it as an object instead.")}),{get:function(e,t){return t.includes(".")?i.hass.states[t]:Object.entries(i.hass.states).filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var i=t[1];return s(s([],e,!0),[i],!1)}),[])}}),is_state:function(e,t){var s;return(null===(s=i.hass.states[e])||void 0===s?void 0:s.state)===t},state_attr:function(e,t){var s,r;return null===(r=null===(s=i.hass.states[e])||void 0===s?void 0:s.attributes)||void 0===r?void 0:r[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=i.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=i.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 s;if(e in i.hass.devices)return this.device_attr(e,t.AREA_ID);var a=this.device_id(e);if(a)return this.device_attr(a,t.AREA_ID);var n=r().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,a;e in i.hass.devices&&(a=this.device_attr(e,t.AREA_ID));var n=this.device_id(e);n&&(a=this.device_attr(n,t.AREA_ID));var d=r().find((function(t){var s=t[1];return s.area_id===e||s.area_id===a}));return null===(s=null==d?void 0:d[1])||void 0===s?void 0:s.name},area_entities:function(e){var t=r().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(i.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 s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(i.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 i.hass.user.name},get user_is_admin(){return i.hass.user.is_admin},get user_is_owner(){return i.hass.user.is_owner}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=i(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{r 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;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));
|
|
1
|
+
"use strict";var e,t;!function(e){e.UNKNOWN="unknown",e.UNAVAILABLE="unavailable"}(e||(e={})),function(e){e.AREA_ID="area_id",e.NAME="name"}(t||(t={}));function s(e,t,s){if(s||2===arguments.length)for(var i,r=0,n=t.length;r<n;r++)!i&&r in t||(i||(i=Array.prototype.slice.call(t,0,r)),i[r]=t[r]);return e.concat(i||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;function i(i){var r=function(){return Object.entries(i.hass.areas)};return{get hass(){return i.hass},states:new Proxy((function(e){var t;if(e.includes("."))return null===(t=i.hass.states[e])||void 0===t?void 0:t.state;throw SyntaxError("[home-assistant-javascript-templates]: states method cannot be used with a domain, use it as an object instead.")}),{get:function(e,t){return t.includes(".")?i.hass.states[t]:Object.entries(i.hass.states).filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var i=t[1];return s(s([],e,!0),[i],!1)}),[])}}),is_state:function(e,t){var s;return(null===(s=i.hass.states[e])||void 0===s?void 0:s.state)===t},state_attr:function(e,t){var s,r;return null===(r=null===(s=i.hass.states[e])||void 0===s?void 0:s.attributes)||void 0===r?void 0:r[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=i.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=i.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 s;if(e in i.hass.devices)return this.device_attr(e,t.AREA_ID);var n=this.device_id(e);if(n)return this.device_attr(n,t.AREA_ID);var a=r().find((function(t){return t[1].name===e}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(e){var s,n;e in i.hass.devices&&(n=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(n=this.device_attr(a,t.AREA_ID));var d=r().find((function(t){var s=t[1];return s.area_id===e||s.area_id===n}));return null===(s=null==d?void 0:d[1])||void 0===s?void 0:s.name},area_entities:function(e){var t=r().find((function(t){var s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(i.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 s=t[1];return s.area_id===e||s.name===e}));return t?Object.entries(i.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 i.hass.user.name},get user_is_admin(){return i.hass.user.is_admin},get user_is_owner(){return i.hass.user.is_owner}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=i(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=r;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.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",
|