home-assistant-javascript-templates
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -31,4 +31,7 @@ interface Area { | ||
} | ||
interface HomeAssistant extends HTMLElement { | ||
hass: Hass; | ||
} | ||
declare class HomeAssistantJavaScriptTemplates { | ||
constructor(hass: Hass, throwErrors?: boolean); | ||
constructor(ha: HomeAssistant, throwErrors?: boolean); | ||
private _scopped; | ||
@@ -38,2 +41,2 @@ private _errors; | ||
} | ||
export { HomeAssistantJavaScriptTemplates as default, Hass }; | ||
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass }; |
@@ -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 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}; | ||
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}; |
@@ -31,4 +31,7 @@ interface Area { | ||
} | ||
interface HomeAssistant extends HTMLElement { | ||
hass: Hass; | ||
} | ||
declare class HomeAssistantJavaScriptTemplates { | ||
constructor(hass: Hass, throwErrors?: boolean); | ||
constructor(ha: HomeAssistant, throwErrors?: boolean); | ||
private _scopped; | ||
@@ -38,2 +41,2 @@ private _errors; | ||
} | ||
export { HomeAssistantJavaScriptTemplates as default, Hass }; | ||
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass }; |
@@ -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 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; | ||
"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; |
{ | ||
"name": "home-assistant-javascript-templates", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A JavaScript utility to render Home Assistant JavaScript templates", | ||
@@ -50,3 +50,3 @@ "keywords": [ | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.11.5", | ||
"@types/node": "^20.11.9", | ||
"jest": "^29.7.0", | ||
@@ -53,0 +53,0 @@ "rollup": "^4.9.6", |
@@ -37,3 +37,3 @@ # home-assistant-javascript-templates | ||
const renderer = new HomeAssistantJavaScriptTemplates( | ||
document.querySelector('home-assistant').hass | ||
document.querySelector('home-assistant') | ||
); | ||
@@ -50,3 +50,3 @@ | ||
const renderer = new HomeAssistantJavaScriptTemplates( | ||
document.querySelector('home-assistant').hass | ||
document.querySelector('home-assistant') | ||
); | ||
@@ -66,3 +66,3 @@ | ||
```typescript | ||
new HomeAssistantJavaScriptTemplates(hass, throwErrors = false); | ||
new HomeAssistantJavaScriptTemplates(ha, throwErrors = false); | ||
``` | ||
@@ -72,3 +72,3 @@ | ||
| ------------- | ------------- | -------------------------------------------------- | | ||
| `hass` | no | A valid `hass` object | | ||
| `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). | | ||
| `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. | | ||
@@ -84,3 +84,3 @@ | ||
The same `hass` object that was sent to the class | ||
The `hass` object | ||
@@ -139,3 +139,3 @@ #### states | ||
```javascript | ||
device_attr('706ad0ebe27e105d7cd0b73386deefdd') | ||
device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer') | ||
``` | ||
@@ -237,3 +237,3 @@ | ||
const renderer = new HomeAssistantJavaScriptTemplates( | ||
document.querySelector('home-assistant').hass | ||
document.querySelector('home-assistant') | ||
); | ||
@@ -260,3 +260,3 @@ | ||
const renderer = new HomeAssistantJavaScriptTemplates( | ||
document.querySelector('home-assistant').hass | ||
document.querySelector('home-assistant') | ||
); | ||
@@ -263,0 +263,0 @@ |
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
30499
112