Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

home-assistant-javascript-templates

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

home-assistant-javascript-templates - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

10

dist/esm/index.d.ts

@@ -34,2 +34,6 @@ interface Area {

}
interface Tracked {
entities: string[];
domains: string[];
}
declare class HomeAssistantJavaScriptTemplates {

@@ -39,4 +43,8 @@ constructor(ha: HomeAssistant, throwErrors?: boolean);

private _errors;
renderTemplate(template: string): string;
renderTemplate(template: string): any;
get tracked(): Tracked;
cleanTrackedEntities(): void;
cleanTrackedDomains(): void;
cleanTracked(): void;
}
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };

2

dist/esm/index.js

@@ -1,1 +0,1 @@

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};
var e,t,i=/^([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 s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.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){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.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=n.hass.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=n.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 i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().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,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.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 i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.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 n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(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)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();export{r as default};

@@ -34,2 +34,6 @@ interface Area {

}
interface Tracked {
entities: string[];
domains: string[];
}
declare class HomeAssistantJavaScriptTemplates {

@@ -39,4 +43,8 @@ constructor(ha: HomeAssistant, throwErrors?: boolean);

private _errors;
renderTemplate(template: string): string;
renderTemplate(template: string): any;
get tracked(): Tracked;
cleanTrackedEntities(): void;
cleanTrackedDomains(): void;
cleanTracked(): void;
}
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };

@@ -1,1 +0,1 @@

"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;
"use strict";var e,t,i=/^([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 s=function(e){return e.includes(".")};function n(n){var r=function(){return Object.entries(n.hass.areas)},a=new Set,c=new Set,d=function(e){n.hass.states[e]&&a.add(e)};return{get hass(){return n.hass},states:new Proxy((function(e){var t;if(s(e))return d(e),null===(t=n.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){if(s(t))return d(t),n.hass.states[t];var r,a=Object.entries(n.hass.states).filter((function(e){return e[0].startsWith(t)}));return a.length&&(r=t,c.add(r)),new Proxy(a.reduce((function(e,t){var s=t[0],n=t[1];return e[s.replace(i,"$2")]=n,e}),{}),{get:function(e,i){return d("".concat(t,".").concat(i)),e[i]}})}}),is_state:function(e,t){var i;return d(e),(null===(i=n.hass.states[e])||void 0===i?void 0:i.state)===t},state_attr:function(e,t){var i,s;return d(e),null===(s=null===(i=n.hass.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=n.hass.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=n.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 i;if(e in n.hass.devices)return this.device_attr(e,t.AREA_ID);var s=this.device_id(e);if(s)return this.device_attr(s,t.AREA_ID);var a=r().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,s;e in n.hass.devices&&(s=this.device_attr(e,t.AREA_ID));var a=this.device_id(e);a&&(s=this.device_attr(a,t.AREA_ID));var c=r().find((function(t){var i=t[1];return i.area_id===e||i.area_id===s}));return null===(i=null==c?void 0:c[1])||void 0===i?void 0:i.name},area_entities:function(e){var t=r().find((function(t){var i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.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 i=t[1];return i.area_id===e||i.name===e}));return t?Object.entries(n.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 n.hass.user.name},get user_is_admin(){return n.hass.user.is_admin},get user_is_owner(){return n.hass.user.is_owner},tracked:{get entities(){return Array.from(a)},get domains(){return Array.from(c)}},cleanTrackedEntities:function(){a.clear()},cleanTrackedDomains:function(){c.clear()}}}var r=function(){function e(e,t){void 0===t&&(t=!1),this._scopped=n(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)}},Object.defineProperty(e.prototype,"tracked",{get:function(){return this._scopped.tracked},enumerable:!1,configurable:!0}),e.prototype.cleanTrackedEntities=function(){this._scopped.cleanTrackedEntities()},e.prototype.cleanTrackedDomains=function(){this._scopped.cleanTrackedDomains()},e.prototype.cleanTracked=function(){this._scopped.cleanTrackedEntities(),this._scopped.cleanTrackedDomains()},e}();module.exports=r;
{
"name": "home-assistant-javascript-templates",
"version": "3.0.1",
"version": "3.1.0",
"description": "A JavaScript utility to render Home Assistant JavaScript templates",

@@ -50,5 +50,5 @@ "keywords": [

"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@types/node": "^20.11.17",
"jest": "^29.7.0",
"rollup": "^4.9.6",
"rollup": "^4.10.0",
"rollup-plugin-ts": "^3.4.5",

@@ -55,0 +55,0 @@ "ts-jest": "^29.1.2",

@@ -72,6 +72,53 @@ # home-assistant-javascript-templates

### renderTemplate method
### Properties
This is the main method to render `JavaScript` templates, it needs a string as a parameter. Inside this string you can use several objects and methods.
#### tracked
```typescript
interface Tracked {
entities: string[];
domains: string[];
}
get tracked(): Tracked
```
This property will return an object with two properties (`entities` and `domains`). Each of these properties will be an array containing the entities or ids that have been tracked when the templates have been rendered. If some domain or entity was not reached because it was inside a condition that never met, then it will not be included in the `tracked` property. Only those entities or domains that were called during the rendering by the code using [states](#states), [is_state](#is_state), [state_attr](#state_attr), [is_state_attr](#is_state_attr) or [has_value](#has_value) will be included.
>Note: take into account that the domains will be only tracked if the `states` object is used accesing a domain. For example `states('device_tracker.paulus')` or `states['device_tracker.paulus']` will track the entity `device_tracker.paulus` but not the domain `device_tracker` but `states.device_tracker.paulus` will track both, the domain `device_tracker` and the entity `device_tracker.paulus`. The rest of the methods will track only entities.
### Methods
#### renderTemplate
```typescript
renderTemplate(template: string): any
```
This is the main method to render `JavaScript` templates, it needs a string as a parameter. Inside this string you can use [several objects and methods](#objects-and-methods-available-in-the-templates). It returns whatever the `JavaScript` code returns, because of that it is typed as `any`.
#### cleanTrackedEntities
```typescript
cleanTrackedEntities(): void
```
This method will clean all the tracked entities until the moment, so after being called, the `tracked` property will return an empty array as `entities`.
#### cleanTrackedDomains
```typescript
cleanTrackedDomains(): void
```
This method will clean all the tracked domains until the moment, so after being called, the `tracked` property will return an empty array as `domains`.
#### cleanTracked
```typescript
cleanTracked(): void
```
This method will clean all the tracked entities and domains until the moment. It is the same as calling `cleanTrackedEntities` and `cleanTrackedDomains` consecutively.
### Objects and methods available in the templates

@@ -78,0 +125,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc