home-assistant-javascript-templates 1.1.0 → 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 +12 -9
- package/dist/esm/index.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ new HomeAssistantJavaScriptTemplates(hass, throwErrors = false);
|
|
|
67
67
|
| Parameter | Optional | Description |
|
|
68
68
|
| ------------- | ------------- | -------------------------------------------------- |
|
|
69
69
|
| `hass` | no | A valid `hass` object |
|
|
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. |
|
|
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
|
|
|
@@ -81,14 +81,17 @@ The same `hass` object that was sent to the class
|
|
|
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,7 +128,7 @@ 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
134
|
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
|
|
@@ -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')
|
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 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.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 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;
|