home-assistant-javascript-templates
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,1 +0,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}; | ||
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}; |
@@ -1,1 +0,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; | ||
"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; |
{ | ||
"name": "home-assistant-javascript-templates", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A JavaScript utility to render Home Assistant JavaScript templates", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -46,3 +46,3 @@ # home-assistant-javascript-templates | ||
```javascript | ||
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates'; | ||
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates'; | ||
@@ -85,3 +85,3 @@ const renderer = new HomeAssistantJavaScriptTemplates( | ||
`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. | ||
`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. | ||
@@ -96,5 +96,8 @@ >Note: If you try to use `states` as a function sending a domain it will throw an error. | ||
states['device_tracker.paulus'].state // returns the state of the entity id 'device_tracker.paulus' | ||
states['device_tracker'] // returns an array with all the states of the 'device_tracker' domain | ||
states.device_tracker.paulus.state // returns the state of the entity id 'device_tracker.paulus' | ||
states.device_tracker // returns an object constaining all the entities of the 'device_tracker' domain | ||
``` | ||
>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`. | ||
#### is_state | ||
@@ -260,6 +263,9 @@ | ||
renderer.renderTemplate(` | ||
const udatesEntities = states['update']; | ||
const updatesEntitiesOn = udatesEntities?.filter((entity) => entity.state === 'on'); | ||
return updatesEntitiesOn?.length || 0; | ||
const udatesEntities = states.update; | ||
const updateEntitiesValues = Object.values(udatesEntities); | ||
const updatesEntitiesOn = updateEntitiesValues.filter((entity) => entity.state === 'on'); | ||
return updatesEntitiesOn.length; | ||
`); | ||
``` | ||
``` | ||
[Optional chaining operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30690
267
110