home-assistant-javascript-templates 1.2.0 → 2.0.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 +8 -8
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ pnpm add home-assistant-javascript-templates
|
|
|
34
34
|
const HomeAssistantJavaScriptTemplates = require('home-assistant-javascript-templates');
|
|
35
35
|
|
|
36
36
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
37
|
-
document.querySelector('home-assistant')
|
|
37
|
+
document.querySelector('home-assistant')
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
renderer.renderTemplate('... template string ...');
|
|
@@ -46,7 +46,7 @@ renderer.renderTemplate('... template string ...');
|
|
|
46
46
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
47
47
|
|
|
48
48
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
49
|
-
document.querySelector('home-assistant')
|
|
49
|
+
document.querySelector('home-assistant')
|
|
50
50
|
);
|
|
51
51
|
|
|
52
52
|
renderer.renderTemplate('... template string ...');
|
|
@@ -61,12 +61,12 @@ The package exposes a class that needs to be instantiated and is this isntance t
|
|
|
61
61
|
Main class of the library, it is the `default` export in the package.
|
|
62
62
|
|
|
63
63
|
```typescript
|
|
64
|
-
new HomeAssistantJavaScriptTemplates(
|
|
64
|
+
new HomeAssistantJavaScriptTemplates(ha, throwErrors = false);
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
| Parameter | Optional | Description |
|
|
68
68
|
| ------------- | ------------- | -------------------------------------------------- |
|
|
69
|
-
| `
|
|
69
|
+
| `ha` | no | An HTML element that has the `hass` object as a property (e.g. the `home-assistant` custom element). |
|
|
70
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
|
|
@@ -77,7 +77,7 @@ This is the main method to render `JavaScript` templates, it needs a string as a
|
|
|
77
77
|
|
|
78
78
|
#### hass
|
|
79
79
|
|
|
80
|
-
The
|
|
80
|
+
The `hass` object
|
|
81
81
|
|
|
82
82
|
#### states
|
|
83
83
|
|
|
@@ -131,7 +131,7 @@ has_value('sensor.my_sensor')
|
|
|
131
131
|
Method that returns the value of an attribute for the given device id or `undefined` if it doesn’t exist.
|
|
132
132
|
|
|
133
133
|
```javascript
|
|
134
|
-
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
|
|
134
|
+
device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer')
|
|
135
135
|
```
|
|
136
136
|
|
|
137
137
|
#### is_device_attr
|
|
@@ -228,7 +228,7 @@ user_is_owner
|
|
|
228
228
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
229
229
|
|
|
230
230
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
231
|
-
document.querySelector('home-assistant')
|
|
231
|
+
document.querySelector('home-assistant')
|
|
232
232
|
);
|
|
233
233
|
|
|
234
234
|
/**
|
|
@@ -250,7 +250,7 @@ renderer.renderTemplate(`
|
|
|
250
250
|
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
|
|
251
251
|
|
|
252
252
|
const renderer = new HomeAssistantJavaScriptTemplates(
|
|
253
|
-
document.querySelector('home-assistant')
|
|
253
|
+
document.querySelector('home-assistant')
|
|
254
254
|
);
|
|
255
255
|
|
|
256
256
|
renderer.renderTemplate(`
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -28,10 +28,13 @@ interface Hass {
|
|
|
28
28
|
states: Record<string, State>;
|
|
29
29
|
user: User;
|
|
30
30
|
}
|
|
31
|
+
interface HomeAssistant extends HTMLElement {
|
|
32
|
+
hass: Hass;
|
|
33
|
+
}
|
|
31
34
|
declare class HomeAssistantJavaScriptTemplates {
|
|
32
|
-
constructor(
|
|
35
|
+
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
33
36
|
private _scopped;
|
|
34
37
|
private _errors;
|
|
35
38
|
renderTemplate(template: string): string;
|
|
36
39
|
}
|
|
37
|
-
export { HomeAssistantJavaScriptTemplates as default, Hass };
|
|
40
|
+
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };
|
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={}));function
|
|
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};
|
package/dist/index.d.ts
CHANGED
|
@@ -28,10 +28,13 @@ interface Hass {
|
|
|
28
28
|
states: Record<string, State>;
|
|
29
29
|
user: User;
|
|
30
30
|
}
|
|
31
|
+
interface HomeAssistant extends HTMLElement {
|
|
32
|
+
hass: Hass;
|
|
33
|
+
}
|
|
31
34
|
declare class HomeAssistantJavaScriptTemplates {
|
|
32
|
-
constructor(
|
|
35
|
+
constructor(ha: HomeAssistant, throwErrors?: boolean);
|
|
33
36
|
private _scopped;
|
|
34
37
|
private _errors;
|
|
35
38
|
renderTemplate(template: string): string;
|
|
36
39
|
}
|
|
37
|
-
export { HomeAssistantJavaScriptTemplates as default, Hass };
|
|
40
|
+
export { HomeAssistantJavaScriptTemplates as default, HomeAssistant, Hass };
|
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={}));function
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "home-assistant-javascript-templates",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A JavaScript utility to render Home Assistant JavaScript templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"home-assistant",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
49
|
"@types/jest": "^29.5.11",
|
|
50
|
-
"@types/node": "^20.11.
|
|
50
|
+
"@types/node": "^20.11.9",
|
|
51
51
|
"jest": "^29.7.0",
|
|
52
52
|
"rollup": "^4.9.6",
|
|
53
53
|
"rollup-plugin-ts": "^3.4.5",
|