home-assistant-javascript-templates 5.1.0 → 5.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 CHANGED
@@ -338,6 +338,14 @@ Property to return the user agent of the browser in which Home Assistant is runn
338
338
  user_agent
339
339
  ```
340
340
 
341
+ #### panel_url
342
+
343
+ Property to return the current Home Assistant panel URL (`window.location.pathname`).
344
+
345
+ ```javascript
346
+ panel_url
347
+ ```
348
+
341
349
  ## Examples
342
350
 
343
351
  #### Get a device attribute and return a formatted text with it
@@ -379,7 +387,7 @@ const haJsTemplates = new HomeAssistantJavaScriptTemplates(
379
387
  haJsTemplates.getRenderer()
380
388
  .then((renderer) => {
381
389
  const element = document.querySelector('#my-element');
382
- renderer.trackTemplate(
390
+ const untrack = renderer.trackTemplate(
383
391
  `
384
392
  const udatesEntities = states.update;
385
393
  const updateEntitiesValues = Object.values(udatesEntities);
@@ -390,6 +398,10 @@ haJsTemplates.getRenderer()
390
398
  element.innerHTML = result;
391
399
  }
392
400
  );
401
+
402
+ // Later untrack the template
403
+ untrack();
404
+
393
405
  });
394
406
 
395
407
  ```
@@ -53,6 +53,9 @@ declare class HomeAssistantJavaScriptTemplatesRenderer {
53
53
  private _throwWarnings;
54
54
  private _subscriptions;
55
55
  private _scopped;
56
+ private _executeRenderingFunctions;
57
+ private _watchForPanelUrlChange;
58
+ private _panelUrlWatchCallback;
56
59
  private _watchForEntitiesChange;
57
60
  private _entityWatchCallback;
58
61
  private _storeTracked;
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- var t,e,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={}));var s=function(t){return t.reduce((function(t,e){var i=e[0],s=e[1];return t[i.replace(n,"$2")]=s,t}),{})},r=function(t){return t.includes(".")};function a(n,a){var o=function(){return Object.entries(n.hass.areas)},c=function(){return Object.entries(n.hass.entities)},u=new Set,d=function(t,e){a&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},_=function(t){return d("Entity",t)},h=function(t){return d("Domain",t)},p=function(t){n.hass.states[t]?u.add(t):_(t)};return{get hass(){return n.hass},states:new Proxy((function(t){var e;if(r(t))return p(t),null===(e=n.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(r(e))return p(e),n.hass.states[e];var i=Object.entries(n.hass.states).filter((function(t){return t[0].startsWith(e)}));return i.length||h(e),new Proxy(s(i),{get:function(t,i){return p("".concat(e,".").concat(i)),t[i]}})}}),is_state:function(t,e){var i;return p(t),(null===(i=n.hass.states[t])||void 0===i?void 0:i.state)===e},state_attr:function(t,e){var i,s;return p(t),null===(s=null===(i=n.hass.states[t])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,i){return this.state_attr(t,e)===i},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(_(e),!1)},entities:new Proxy((function(t){if(void 0===t)return n.hass.entities;if(r(t))return p(t),n.hass.entities[t];var e=c().filter((function(e){return e[0].startsWith(t)}));return e.length||h(t),new Proxy(s(e),{get:function(e,i){return p("".concat(t,".").concat(i)),e[i]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var i;return p(t),null===(i=n.hass.entities[t])||void 0===i?void 0:i[e]},is_entity_prop:function(t,e,i){return this.entity_prop(t,e)===i},devices:new Proxy((function(t){if(void 0===t)return n.hass.devices;if(r(t))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[t]}),{get:function(t,e){if(r(e))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[e]}}),device_attr:function(t,e){var i;return null===(i=n.hass.devices[t])||void 0===i?void 0:i[e]},is_device_attr:function(t,e,i){return this.device_attr(t,e)===i},device_id:function(t){var e;return p(t),null===(e=n.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return o().map((function(t){return t[1].area_id}))},area_id:function(t){var i,s;if(t in n.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=o().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var i,s,r;t in n.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var c=o().find((function(e){var i=e[1];return i.area_id===t||i.area_id===r}));return null===(s=null==c?void 0:c[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?c().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?Object.entries(n.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get user_agent(){return window.navigator.userAgent},get tracked(){return u},cleanTracked:function(){u.clear()}}}var o=function(){function t(t,e){var i=e.throwErrors,n=void 0!==i&&i,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=n,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=a(t,r),this._watchForEntitiesChange()}return t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:"subscribe_events",event_type:"state_changed"})}))},t.prototype._entityWatchCallback=function(t){var e=this;if(this._subscriptions.size){var i=t.data.entity_id;this._subscriptions.has(i)&&this._subscriptions.get(i).forEach((function(t,i){t.forEach((function(t){e.trackTemplate(i,t)}))}))}},t.prototype._storeTracked=function(t,e){var i=this;this._scopped.tracked.forEach((function(n){if(i._subscriptions.has(n)){var s=i._subscriptions.get(n);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else i._subscriptions.set(n,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var i=this;this._subscriptions.forEach((function(n,s){if(n.has(t)){var r=n.get(t);r.has(e)&&r.delete(e),0===r.size&&(n.delete(t),0===n.size&&i._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","".concat('"use strict";'," ").concat(e))(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.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,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,this._scopped.user_agent)}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var i=this;this._scopped.cleanTracked();var n=this.renderTemplate(t);return this._storeTracked(t,e),e(n),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),c=function(){function t(t,e){var i,n;void 0===e&&(e={}),this._renderer=(i=function(){return t.hass},n=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=i();n(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new o(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();export{c as default};
1
+ var t,e,n,i,s="[home-assistant-javascript-templates]",r=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={})),function(t){t.PANEL_URL="panel_url"}(n||(n={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(i||(i={}));var a=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(r,"$2")]=i,t}),{})},o=function(t){return t.includes(".")};function c(i,r){var c=function(){return Object.entries(i.hass.areas)},u=function(){return Object.entries(i.hass.entities)},d=new Set,_=function(t,e){r&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},h=function(t){return _("Entity",t)},p=function(t){return _("Domain",t)},v=function(t){i.hass.states[t]?d.add(t):h(t)};return{get hass(){return i.hass},states:new Proxy((function(t){var e;if(o(t))return v(t),null===(e=i.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(s,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(o(e))return v(e),i.hass.states[e];var n=Object.entries(i.hass.states).filter((function(t){return t[0].startsWith(e)}));return n.length||p(e),new Proxy(a(n),{get:function(t,n){return v("".concat(e,".").concat(n)),t[n]}})}}),is_state:function(t,e){var n;return v(t),(null===(n=i.hass.states[t])||void 0===n?void 0:n.state)===e},state_attr:function(t,e){var n,s;return v(t),null===(s=null===(n=i.hass.states[t])||void 0===n?void 0:n.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(h(e),!1)},entities:new Proxy((function(t){if(void 0===t)return i.hass.entities;if(o(t))return v(t),i.hass.entities[t];var e=u().filter((function(e){return e[0].startsWith(t)}));return e.length||p(t),new Proxy(a(e),{get:function(e,n){return v("".concat(t,".").concat(n)),e[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var n;return v(t),null===(n=i.hass.entities[t])||void 0===n?void 0:n[e]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(t){if(void 0===t)return i.hass.devices;if(o(t))throw SyntaxError("".concat(s,": devices method cannot be used with an entity id, you should use a device id instead."));return i.hass.devices[t]}),{get:function(t,e){if(o(e))throw SyntaxError("".concat(s,": devices cannot be accesed using an entity id, you should use a device id instead."));return i.hass.devices[e]}}),device_attr:function(t,e){var n;return null===(n=i.hass.devices[t])||void 0===n?void 0:n[e]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(t){var e;return v(t),null===(e=i.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(t){var n,s;if(t in i.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(n=i.hass.entities[t])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=c().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var n,s,r;t in i.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(n=i.hass.entities[t])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var o=c().find((function(e){var n=e[1];return n.area_id===t||n.area_id===r}));return null===(s=null==o?void 0:o[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?u().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?Object.entries(i.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get user_agent(){return window.navigator.userAgent},get panel_url(){var t;return t=n.PANEL_URL,d.add(t),location.pathname},get tracked(){return d},cleanTracked:function(){d.clear()}}}var u=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=i,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=c(t,r),this._watchForPanelUrlChange(),this._watchForEntitiesChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t){e.trackTemplate(n,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(i.LOCATION_CHANGED,(function(e){e.detail.replace&&t._panelUrlWatchCallback()})),window.addEventListener(i.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(n.PANEL_URL)&&this._executeRenderingFunctions(n.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:i.SUBSCRIBE_EVENTS,event_type:i.STATE_CHANGE_EVENT})}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e){var n=this;this._scopped.tracked.forEach((function(i){if(n._subscriptions.has(i)){var s=n._subscriptions.get(i);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else n._subscriptions.set(i,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,s){if(i.has(t)){var r=i.get(t);r.has(e)&&r.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","panel_url","user_agent","".concat('"use strict";'," ").concat(e))(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.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,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,this._scopped.panel_url,this._scopped.user_agent)}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var n=this;this._scopped.cleanTracked();var i=this.renderTemplate(t);return this._storeTracked(t,e),e(i),function(){return n._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),d=function(){function t(t,e){var n,i;void 0===e&&(e={}),this._renderer=(n=function(){return t.hass},i=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=n();i(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new u(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();export{d as default};
package/dist/index.d.ts CHANGED
@@ -53,6 +53,9 @@ declare class HomeAssistantJavaScriptTemplatesRenderer {
53
53
  private _throwWarnings;
54
54
  private _subscriptions;
55
55
  private _scopped;
56
+ private _executeRenderingFunctions;
57
+ private _watchForPanelUrlChange;
58
+ private _panelUrlWatchCallback;
56
59
  private _watchForEntitiesChange;
57
60
  private _entityWatchCallback;
58
61
  private _storeTracked;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t,e,i="[home-assistant-javascript-templates]",n=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={}));var s=function(t){return t.reduce((function(t,e){var i=e[0],s=e[1];return t[i.replace(n,"$2")]=s,t}),{})},r=function(t){return t.includes(".")};function a(n,a){var o=function(){return Object.entries(n.hass.areas)},c=function(){return Object.entries(n.hass.entities)},u=new Set,d=function(t,e){a&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},_=function(t){return d("Entity",t)},h=function(t){return d("Domain",t)},p=function(t){n.hass.states[t]?u.add(t):_(t)};return{get hass(){return n.hass},states:new Proxy((function(t){var e;if(r(t))return p(t),null===(e=n.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(i,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(r(e))return p(e),n.hass.states[e];var i=Object.entries(n.hass.states).filter((function(t){return t[0].startsWith(e)}));return i.length||h(e),new Proxy(s(i),{get:function(t,i){return p("".concat(e,".").concat(i)),t[i]}})}}),is_state:function(t,e){var i;return p(t),(null===(i=n.hass.states[t])||void 0===i?void 0:i.state)===e},state_attr:function(t,e){var i,s;return p(t),null===(s=null===(i=n.hass.states[t])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,i){return this.state_attr(t,e)===i},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(_(e),!1)},entities:new Proxy((function(t){if(void 0===t)return n.hass.entities;if(r(t))return p(t),n.hass.entities[t];var e=c().filter((function(e){return e[0].startsWith(t)}));return e.length||h(t),new Proxy(s(e),{get:function(e,i){return p("".concat(t,".").concat(i)),e[i]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var i;return p(t),null===(i=n.hass.entities[t])||void 0===i?void 0:i[e]},is_entity_prop:function(t,e,i){return this.entity_prop(t,e)===i},devices:new Proxy((function(t){if(void 0===t)return n.hass.devices;if(r(t))throw SyntaxError("".concat(i,": devices method cannot be used with an entity id, you should use a device id instead."));return n.hass.devices[t]}),{get:function(t,e){if(r(e))throw SyntaxError("".concat(i,": devices cannot be accesed using an entity id, you should use a device id instead."));return n.hass.devices[e]}}),device_attr:function(t,e){var i;return null===(i=n.hass.devices[t])||void 0===i?void 0:i[e]},is_device_attr:function(t,e,i){return this.device_attr(t,e)===i},device_id:function(t){var e;return p(t),null===(e=n.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return o().map((function(t){return t[1].area_id}))},area_id:function(t){var i,s;if(t in n.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=o().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var i,s,r;t in n.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(i=n.hass.entities[t])||void 0===i?void 0:i.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var c=o().find((function(e){var i=e[1];return i.area_id===t||i.area_id===r}));return null===(s=null==c?void 0:c[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?c().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=o().find((function(e){var i=e[1];return i.area_id===t||i.name===t}));return e?Object.entries(n.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get user_agent(){return window.navigator.userAgent},get tracked(){return u},cleanTracked:function(){u.clear()}}}var o=function(){function t(t,e){var i=e.throwErrors,n=void 0!==i&&i,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=n,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=a(t,r),this._watchForEntitiesChange()}return t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:"subscribe_events",event_type:"state_changed"})}))},t.prototype._entityWatchCallback=function(t){var e=this;if(this._subscriptions.size){var i=t.data.entity_id;this._subscriptions.has(i)&&this._subscriptions.get(i).forEach((function(t,i){t.forEach((function(t){e.trackTemplate(i,t)}))}))}},t.prototype._storeTracked=function(t,e){var i=this;this._scopped.tracked.forEach((function(n){if(i._subscriptions.has(n)){var s=i._subscriptions.get(n);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else i._subscriptions.set(n,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var i=this;this._subscriptions.forEach((function(n,s){if(n.has(t)){var r=n.get(t);r.has(e)&&r.delete(e),0===r.size&&(n.delete(t),0===n.size&&i._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","".concat('"use strict";'," ").concat(e))(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.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,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,this._scopped.user_agent)}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var i=this;this._scopped.cleanTracked();var n=this.renderTemplate(t);return this._storeTracked(t,e),e(n),function(){return i._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),c=function(){function t(t,e){var i,n;void 0===e&&(e={}),this._renderer=(i=function(){return t.hass},n=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=i();n(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new o(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();module.exports=c;
1
+ "use strict";var t,e,n,i,s="[home-assistant-javascript-templates]",r=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(t||(t={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(e||(e={})),function(t){t.PANEL_URL="panel_url"}(n||(n={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(i||(i={}));var a=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(r,"$2")]=i,t}),{})},o=function(t){return t.includes(".")};function c(i,r){var c=function(){return Object.entries(i.hass.areas)},u=function(){return Object.entries(i.hass.entities)},d=new Set,_=function(t,e){r&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},h=function(t){return _("Entity",t)},p=function(t){return _("Domain",t)},v=function(t){i.hass.states[t]?d.add(t):h(t)};return{get hass(){return i.hass},states:new Proxy((function(t){var e;if(o(t))return v(t),null===(e=i.hass.states[t])||void 0===e?void 0:e.state;throw SyntaxError("".concat(s,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(t,e){if(o(e))return v(e),i.hass.states[e];var n=Object.entries(i.hass.states).filter((function(t){return t[0].startsWith(e)}));return n.length||p(e),new Proxy(a(n),{get:function(t,n){return v("".concat(e,".").concat(n)),t[n]}})}}),is_state:function(t,e){var n;return v(t),(null===(n=i.hass.states[t])||void 0===n?void 0:n.state)===e},state_attr:function(t,e){var n,s;return v(t),null===(s=null===(n=i.hass.states[t])||void 0===n?void 0:n.attributes)||void 0===s?void 0:s[e]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(e){return this.states(e)?!(this.is_state(e,t.UNKNOWN)||this.is_state(e,t.UNAVAILABLE)):(h(e),!1)},entities:new Proxy((function(t){if(void 0===t)return i.hass.entities;if(o(t))return v(t),i.hass.entities[t];var e=u().filter((function(e){return e[0].startsWith(t)}));return e.length||p(t),new Proxy(a(e),{get:function(e,n){return v("".concat(t,".").concat(n)),e[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(t,e){var n;return v(t),null===(n=i.hass.entities[t])||void 0===n?void 0:n[e]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(t){if(void 0===t)return i.hass.devices;if(o(t))throw SyntaxError("".concat(s,": devices method cannot be used with an entity id, you should use a device id instead."));return i.hass.devices[t]}),{get:function(t,e){if(o(e))throw SyntaxError("".concat(s,": devices cannot be accesed using an entity id, you should use a device id instead."));return i.hass.devices[e]}}),device_attr:function(t,e){var n;return null===(n=i.hass.devices[t])||void 0===n?void 0:n[e]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(t){var e;return v(t),null===(e=i.hass.entities[t])||void 0===e?void 0:e.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(t){var n,s;if(t in i.hass.devices)return this.device_attr(t,e.AREA_ID);var r=null===(n=i.hass.entities[t])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,e.AREA_ID);var a=c().find((function(e){return e[1].name===t}));return null===(s=null==a?void 0:a[1])||void 0===s?void 0:s.area_id},area_name:function(t){var n,s,r;t in i.hass.devices&&(r=this.device_attr(t,e.AREA_ID));var a=null===(n=i.hass.entities[t])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,e.AREA_ID));var o=c().find((function(e){var n=e[1];return n.area_id===t||n.area_id===r}));return null===(s=null==o?void 0:o[1])||void 0===s?void 0:s.name},area_entities:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?u().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(t){var e=c().find((function(e){var n=e[1];return n.area_id===t||n.name===t}));return e?Object.entries(i.hass.devices).filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[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},get user_agent(){return window.navigator.userAgent},get panel_url(){var t;return t=n.PANEL_URL,d.add(t),location.pathname},get tracked(){return d},cleanTracked:function(){d.clear()}}}var u=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,r=void 0===s||s;this._throwErrors=i,this._throwWarnings=r,this._subscriptions=new Map,this._scopped=c(t,r),this._watchForPanelUrlChange(),this._watchForEntitiesChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t){e.trackTemplate(n,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(i.LOCATION_CHANGED,(function(e){e.detail.replace&&t._panelUrlWatchCallback()})),window.addEventListener(i.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(n.PANEL_URL)&&this._executeRenderingFunctions(n.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:i.SUBSCRIBE_EVENTS,event_type:i.STATE_CHANGE_EVENT})}))},t.prototype._entityWatchCallback=function(t){if(this._subscriptions.size){var e=t.data.entity_id;this._subscriptions.has(e)&&this._executeRenderingFunctions(e)}},t.prototype._storeTracked=function(t,e){var n=this;this._scopped.tracked.forEach((function(i){if(n._subscriptions.has(i)){var s=n._subscriptions.get(i);if(s.has(t)){var r=s.get(t);r.has(e)||r.add(e)}else s.set(t,new Set([e]))}else n._subscriptions.set(i,new Map([[t,new Set([e])]]))}))},t.prototype._untrackTemplate=function(t,e){var n=this;this._subscriptions.forEach((function(i,s){if(i.has(t)){var r=i.get(t);r.has(e)&&r.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t){try{var e=t.includes("return")?t:"return ".concat(t);return new Function("hass","states","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","panel_url","user_agent","".concat('"use strict";'," ").concat(e))(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.entities,this._scopped.entity_prop,this._scopped.is_entity_prop.bind(this._scopped),this._scopped.devices,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,this._scopped.panel_url,this._scopped.user_agent)}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e){var n=this;this._scopped.cleanTracked();var i=this.renderTemplate(t);return this._storeTracked(t,e),e(i),function(){return n._untrackTemplate(t,e)}},t.prototype.cleanTracked=function(t){t?this._subscriptions.has(t)&&this._subscriptions.delete(t):this._subscriptions.clear()},t}(),d=function(){function t(t,e){var n,i;void 0===e&&(e={}),this._renderer=(n=function(){return t.hass},i=function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)},new Promise((function(t,e){var s=0,r=function(){var a=n();i(a)?t(a):++s<100?setTimeout(r,50):e()};r()}))).then((function(){return new u(t,e)})).catch((function(){throw new Error("The provided element doesn't contain a proper or initialised hass object")}))}return t.prototype.getRenderer=function(){return this._renderer},t}();module.exports=d;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "home-assistant-javascript-templates",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "A JavaScript utility to render Home Assistant JavaScript templates",
5
5
  "keywords": [
6
6
  "home-assistant",
@@ -50,6 +50,7 @@
50
50
  "@types/node": "^22.7.7",
51
51
  "jest": "^29.7.0",
52
52
  "jest-environment-jsdom": "^29.7.0",
53
+ "jest-location-mock": "^2.0.0",
53
54
  "rollup": "^4.24.0",
54
55
  "rollup-plugin-ts": "^3.4.5",
55
56
  "ts-jest": "^29.2.5",