home-assistant-javascript-templates 1.0.1 → 1.2.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 +58 -11
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
A JavaScript utility to render Home Assistant JavaScript templates.
|
|
4
4
|
|
|
5
|
-
[](https://github.com/elchininet/home-assistant-javascript-templates/actions/workflows/deploy.yaml)
|
|
5
|
+
[](https://github.com/elchininet/home-assistant-javascript-templates/actions/workflows/deploy.yaml)
|
|
6
|
+
[](https://coveralls.io/github/elchininet/home-assistant-javascript-templates?branch=master)
|
|
7
|
+
[](https://badge.fury.io/js/home-assistant-javascript-templates)
|
|
6
8
|
|
|
7
9
|
## Install
|
|
8
10
|
|
|
@@ -65,7 +67,7 @@ new HomeAssistantJavaScriptTemplates(hass, throwErrors = false);
|
|
|
65
67
|
| Parameter | Optional | Description |
|
|
66
68
|
| ------------- | ------------- | -------------------------------------------------- |
|
|
67
69
|
| `hass` | no | A valid `hass` object |
|
|
68
|
-
| `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. |
|
|
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. |
|
|
69
71
|
|
|
70
72
|
### renderTemplate method
|
|
71
73
|
|
|
@@ -79,14 +81,17 @@ The same `hass` object that was sent to the class
|
|
|
79
81
|
|
|
80
82
|
#### states
|
|
81
83
|
|
|
82
|
-
|
|
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.
|
|
83
87
|
|
|
84
88
|
```javascript
|
|
85
89
|
// Using states as a function
|
|
86
|
-
states('device_tracker.paulus')
|
|
90
|
+
states('device_tracker.paulus') // returns the state of the entity id 'device_tracker.paulus'
|
|
87
91
|
|
|
88
92
|
// Using states as an object
|
|
89
|
-
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
|
|
90
95
|
```
|
|
91
96
|
|
|
92
97
|
#### is_state
|
|
@@ -99,7 +104,7 @@ is_state('device_tracker.paulus', 'not_home')
|
|
|
99
104
|
|
|
100
105
|
#### state_attr
|
|
101
106
|
|
|
102
|
-
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.
|
|
103
108
|
|
|
104
109
|
```javascript
|
|
105
110
|
state_attr('device_tracker.paulus', 'battery')
|
|
@@ -123,7 +128,7 @@ has_value('sensor.my_sensor')
|
|
|
123
128
|
|
|
124
129
|
#### device_attr
|
|
125
130
|
|
|
126
|
-
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.
|
|
127
132
|
|
|
128
133
|
```javascript
|
|
129
134
|
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
|
|
@@ -139,7 +144,7 @@ is_device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer', 'Synology')
|
|
|
139
144
|
|
|
140
145
|
#### device_id
|
|
141
146
|
|
|
142
|
-
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.
|
|
143
148
|
|
|
144
149
|
```javascript
|
|
145
150
|
device_id('sensor.my_sensor')
|
|
@@ -155,7 +160,7 @@ areas()
|
|
|
155
160
|
|
|
156
161
|
#### area_id
|
|
157
162
|
|
|
158
|
-
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.
|
|
159
164
|
|
|
160
165
|
```javascript
|
|
161
166
|
area_id('b8c1c9dd23cb82bbfa09b5657f41d04f')
|
|
@@ -165,7 +170,7 @@ area_id('Woonkamer')
|
|
|
165
170
|
|
|
166
171
|
#### area_name
|
|
167
172
|
|
|
168
|
-
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.
|
|
169
174
|
|
|
170
175
|
```javascript
|
|
171
176
|
area_name('b8c1c9dd23cb82bbfa09b5657f41d04f')
|
|
@@ -191,7 +196,33 @@ area_devices('woonkamer')
|
|
|
191
196
|
area_devices('Woonkamer')
|
|
192
197
|
```
|
|
193
198
|
|
|
194
|
-
|
|
199
|
+
#### user_name
|
|
200
|
+
|
|
201
|
+
Property to return the name of the user logged in in Home Assistant. It returns a `string`.
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
user_name
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### user_is_admin
|
|
208
|
+
|
|
209
|
+
Property to return if the user logged in in Home Assistant is admin or not. It returns a `boolean`.
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
user_is_admin
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### user_is_owner
|
|
216
|
+
|
|
217
|
+
Property to return if the user logged in in Home Assistant is the owner. It returns a `boolean`.
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
user_is_owner
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Examples
|
|
224
|
+
|
|
225
|
+
#### Get a device attribute and return a formatted text with it
|
|
195
226
|
|
|
196
227
|
```javascript
|
|
197
228
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
@@ -211,4 +242,20 @@ renderer.renderTemplate(`
|
|
|
211
242
|
const serialNumber = device_attr(deviceId, "serial_number");
|
|
212
243
|
return "sn:" + serialNumber;
|
|
213
244
|
`);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
#### Get all the available updates
|
|
248
|
+
|
|
249
|
+
```javascript
|
|
250
|
+
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
251
|
+
|
|
252
|
+
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
253
|
+
document.querySelector('home-assistant').hass
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
renderer.renderTemplate(`
|
|
257
|
+
const udatesEntities = states['update'];
|
|
258
|
+
const updatesEntitiesOn = udatesEntities?.filter((entity) => entity.state === 'on');
|
|
259
|
+
return updatesEntitiesOn?.length || 0;
|
|
260
|
+
`);
|
|
214
261
|
```
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -16,11 +16,17 @@ interface Entity {
|
|
|
16
16
|
area_id: string | null;
|
|
17
17
|
device_id: string;
|
|
18
18
|
}
|
|
19
|
+
interface User {
|
|
20
|
+
name: string;
|
|
21
|
+
is_admin: boolean;
|
|
22
|
+
is_owner: boolean;
|
|
23
|
+
}
|
|
19
24
|
interface Hass {
|
|
20
25
|
areas: Record<string, Area>;
|
|
21
26
|
devices: Record<string, Device>;
|
|
22
27
|
entities: Record<string, Entity>;
|
|
23
28
|
states: Record<string, State>;
|
|
29
|
+
user: User;
|
|
24
30
|
}
|
|
25
31
|
declare class HomeAssistantJavaScriptTemplates {
|
|
26
32
|
constructor(hass: Hass, throwErrors?: boolean);
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +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={}));function i(e,t,i){if(i||2===arguments.length)for(var r,s=0,n=t.length;s<n;s++)!r&&s in t||(r||(r=Array.prototype.slice.call(t,0,s)),r[s]=t[s]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;function r(r){var s=Object.entries(r.areas),n=Object.entries(r.states),a=Object.entries(r.devices),d=Object.entries(r.entities);return{hass:r,states:new Proxy((function(e){var t;if(e.includes("."))return null===(t=r.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(".")?r.states[t]:n.filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var r=t[1];return i(i([],e,!0),[r],!1)}),[])}}),is_state:function(e,t){var i;return(null===(i=r.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return null===(s=null===(i=r.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=r.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=r.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return s.map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in r.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=s.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,n;e in r.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=s.find((function(t){var i=t[1];return i.area_id===e||i.area_id===n}));return null===(i=null==d?void 0:d[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=s.find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?d.filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=s.find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?a.filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},user_name:r.user.name,user_is_admin:r.user.is_admin,user_is_owner:r.user.is_owner}}var s=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),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)}},e}();export{s as default};
|
package/dist/index.d.ts
CHANGED
|
@@ -16,11 +16,17 @@ interface Entity {
|
|
|
16
16
|
area_id: string | null;
|
|
17
17
|
device_id: string;
|
|
18
18
|
}
|
|
19
|
+
interface User {
|
|
20
|
+
name: string;
|
|
21
|
+
is_admin: boolean;
|
|
22
|
+
is_owner: boolean;
|
|
23
|
+
}
|
|
19
24
|
interface Hass {
|
|
20
25
|
areas: Record<string, Area>;
|
|
21
26
|
devices: Record<string, Device>;
|
|
22
27
|
entities: Record<string, Entity>;
|
|
23
28
|
states: Record<string, State>;
|
|
29
|
+
user: User;
|
|
24
30
|
}
|
|
25
31
|
declare class HomeAssistantJavaScriptTemplates {
|
|
26
32
|
constructor(hass: Hass, throwErrors?: boolean);
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
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 i(e,t,i){if(i||2===arguments.length)for(var r,s=0,n=t.length;s<n;s++)!r&&s in t||(r||(r=Array.prototype.slice.call(t,0,s)),r[s]=t[s]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;function r(r){var s=Object.entries(r.areas),n=Object.entries(r.states),a=Object.entries(r.devices),d=Object.entries(r.entities);return{hass:r,states:new Proxy((function(e){var t;if(e.includes("."))return null===(t=r.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(".")?r.states[t]:n.filter((function(e){return e[0].startsWith(t)})).reduce((function(e,t){var r=t[1];return i(i([],e,!0),[r],!1)}),[])}}),is_state:function(e,t){var i;return(null===(i=r.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return null===(s=null===(i=r.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=r.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=r.entities[e])||void 0===t?void 0:t.device_id},areas:function(){return s.map((function(e){return e[1].area_id}))},area_id:function(e){var i;if(e in r.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=s.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,n;e in r.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=s.find((function(t){var i=t[1];return i.area_id===e||i.area_id===n}));return null===(i=null==d?void 0:d[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=s.find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?d.filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[0]})):[]},area_devices:function(e){var t=s.find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?a.filter((function(e){return e[1].area_id===t[1].area_id})).map((function(e){return e[1].id})):[]},user_name:r.user.name,user_is_admin:r.user.is_admin,user_is_owner:r.user.is_owner}}var s=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),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)}},e}();module.exports=s;
|