home-assistant-javascript-templates 5.7.0 → 5.8.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
@@ -156,23 +156,29 @@ The `hass` object
156
156
 
157
157
  #### states
158
158
 
159
- `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 state object.
159
+ `states` could be used in two ways, as a function or as an object. When using it as function it allows an entity id (containing the domain) and an optional options object as parameters and it will return the state of that entity. As an object it allows you to access a domain or the full entity state object.
160
160
 
161
161
  >[!IMPORTANT]
162
162
  >If you try to use `states` as a function sending a domain it will throw an error.
163
163
 
164
164
  ```javascript
165
- // Using states as a function
166
- states('device_tracker.paulus') // returns the state of the entity id 'device_tracker.paulus'
165
+ // Using states as a function (for a state with value 17.456 and unit ºC)
166
+ states('sensor.slaapkamer_temperatuur') // 17.456
167
+ states('sensor.slaapkamer_temperatuur', { with_unit: true }) // 17.456 ºC
168
+ states('sensor.slaapkamer_temperatuur', { with_unit: true, rounded: true }) // 17.5 ºC
169
+ states('sensor.slaapkamer_temperatuur', { rounded: true }) // 17.5
170
+ states('sensor.slaapkamer_temperatuur', { rounded: 2 }) // 17.46
167
171
 
168
- // Using states as an object
169
- states['device_tracker.paulus'].state // returns the state of the entity id 'device_tracker.paulus'
170
- states.device_tracker.paulus.state // returns the state of the entity id 'device_tracker.paulus'
171
- states.device_tracker // returns an object containing all the entities states of the 'device_tracker' domain
172
+ // Using states as an object (for a state with value 17.456 and unit ºC)
173
+ states['sensor.slaapkamer_temperatuur'].state // 17.456
174
+ states['sensor.slaapkamer_temperatuur'].state_with_unit // 17.5 ºC
175
+ states.sensor.slaapkamer_temperatuur.state // 17.456
176
+ states.sensor.slaapkamer_temperatuur.state_with_unit // 17.5 ºC
177
+ states.sensor // returns an object containing all the entities states of the 'sensor' domain
172
178
  ```
173
179
 
174
180
  >[!TIP]
175
- >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`.
181
+ >Avoid using `states['sensor.slaapkamer_temperatuur'].state` or `states.sensor.slaapkamer_temperatuur.state`. Use `states('sensor.slaapkamer_temperatuur')` instead, 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['sensor.slaapkamer_temperatuur']?.state` or `states.sensor?.slaapkamer_temperatuur?.state`.
176
182
 
177
183
  #### is_state
178
184
 
@@ -36,13 +36,15 @@ interface Hass {
36
36
  entities: Record<string, Entity>;
37
37
  states: Record<string, State>;
38
38
  user: User;
39
+ language: string;
39
40
  }
40
41
  interface HomeAssistant extends HTMLElement {
41
42
  hass: Hass;
42
43
  }
44
+ type CancelSubscription = () => Promise<void>;
43
45
  interface HassConnection {
44
46
  conn: {
45
- subscribeMessage: <T>(callback: (response: T) => void, options: Vars) => void;
47
+ subscribeMessage: <T>(callback: (response: T) => void, options: Vars) => Promise<CancelSubscription>;
46
48
  };
47
49
  }
48
50
  declare global {
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{getPromisableResult as t}from"get-promisable-result";var e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,r=0,s=e.length;r<s;r++)!i&&r in e||(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,r,s,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(r||(r={})),function(t){t.PANEL_URL="panel_url"}(s||(s={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,f="ref",v="value",l="toJSON",b=function(t){return"".concat(f,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},E=function(t){return y("Entity",t)},g=function(t){return y("Domain",t)},w=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):E(e)},A=function(t){p.add(t)};return{get hass(){return t.hass},states:new Proxy((function(e){var n;if(d(e))return m(e),null===(n=t.hass.states[e])||void 0===n?void 0:n.state;throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),t.hass.states[n];var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||g(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),t[e]}})}}),is_state:function(e,n){var i;return m(e),(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,r;return m(e),null===(r=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===r?void 0:r[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(E(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||g(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,r.AREA_ID);var s=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(s)return this.device_attr(s,r.AREA_ID);var a=c().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 n,i,s;e in t.hass.devices&&(s=this.device_attr(e,r.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(s=this.device_attr(a,r.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===s}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.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?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var r=new Proxy(((n={})[v]=void 0,n[l]=function(){return this[v]},n),{get:function(t,e,n){if(e===v||e===l)return A(i),Reflect.get(t,e,n);w("".concat(e," is not a valid ").concat(f," property. A ").concat(f,' only exposes a "').concat(v,'" property'))},set:function(e,n,r){if(n===v){var s=e[v];return e[v]=r,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(s)},new_state:{state:JSON.stringify(r)}}}),!0}return w('property "'.concat(n,'" cannot be set in a ').concat(f)),!1}});return h.set(e,r),r},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):w("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(t,e){if(e===s.PANEL_URL)return A(s.PANEL_URL),location.pathname;n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(s).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,r=e.throwWarnings,a=void 0===r||r,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><])(".concat(Object.values(s).join("|"),")($|[ \\?)+:\\}\\]><.])"),"gm"),this._scopped=_(t,i,a),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(a.LOCATION_CHANGED,(function(e){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(s.PANEL_URL)&&this._executeRenderingFunctions(s.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.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 r=n._subscriptions.get(i);if(r.has(t)){var s=r.get(t);s.has(e)||s.add(e)}else r.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,r){if(i.has(t)){var s=i.get(t);s.has(e)&&s.delete(e),0===s.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(r))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var r=new Map(Object.entries(e(e({},this._variables),i))),s=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=s.includes("return")||!this._autoReturn?s:"return ".concat(s);return(new(Function.bind.apply(Function,n(n([void 0,"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","clientSide","ref","unref"],Array.from(r.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(r.values()),!1))}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()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();export{h as default};
1
+ import{getPromisableResult as t}from"get-promisable-result";var e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,r=0,s=e.length;r<s;r++)!i&&r in e||(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,r,s,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(r||(r={})),function(t){t.PANEL_URL="panel_url"}(s||(s={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,f="ref",v="value",l="toJSON",b=function(t){return"".concat(f,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},g=function(t){return y("Entity",t)},w=function(t){return y("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):g(e)},A=function(t){p.add(t)},N=function(e,n){var i=n.with_unit,r=void 0!==i&&i,s=n.rounded,a=void 0!==s&&s;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return r&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?N(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),d(e))return m(e),N(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),S(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||w(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),S(t[e])}})}}),is_state:function(e,n){var i;return m(e),(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,r;return m(e),null===(r=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===r?void 0:r[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(g(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,r.AREA_ID);var s=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(s)return this.device_attr(s,r.AREA_ID);var a=c().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 n,i,s;e in t.hass.devices&&(s=this.device_attr(e,r.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(s=this.device_attr(a,r.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===s}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.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?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var r=new Proxy(((n={})[v]=void 0,n[l]=function(){return this[v]},n),{get:function(t,e,n){if(e===v||e===l)return A(i),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(f," property. A ").concat(f,' only exposes a "').concat(v,'" property'))},set:function(e,n,r){if(n===v){var s=e[v];return e[v]=r,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(s)},new_state:{state:JSON.stringify(r)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(f)),!1}});return h.set(e,r),r},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(t,e){if(e===s.PANEL_URL)return A(s.PANEL_URL),location.pathname;n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(s).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,r=e.throwWarnings,a=void 0===r||r,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><])(".concat(Object.values(s).join("|"),")($|[ \\?)+:\\}\\]><.])"),"gm"),this._scopped=_(t,i,a),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(a.LOCATION_CHANGED,(function(e){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(s.PANEL_URL)&&this._executeRenderingFunctions(s.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.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 r=n._subscriptions.get(i);if(r.has(t)){var s=r.get(t);s.has(e)||s.add(e)}else r.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,r){if(i.has(t)){var s=i.get(t);s.delete(e),0===s.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(r))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var r=new Map(Object.entries(e(e({},this._variables),i))),s=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=s.includes("return")||!this._autoReturn?s:"return ".concat(s);return(new(Function.bind.apply(Function,n(n([void 0,"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","clientSide","ref","unref"],Array.from(r.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(r.values()),!1))}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()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();export{h as default};
package/dist/index.d.ts CHANGED
@@ -36,13 +36,15 @@ interface Hass {
36
36
  entities: Record<string, Entity>;
37
37
  states: Record<string, State>;
38
38
  user: User;
39
+ language: string;
39
40
  }
40
41
  interface HomeAssistant extends HTMLElement {
41
42
  hass: Hass;
42
43
  }
44
+ type CancelSubscription = () => Promise<void>;
43
45
  interface HassConnection {
44
46
  conn: {
45
- subscribeMessage: <T>(callback: (response: T) => void, options: Vars) => void;
47
+ subscribeMessage: <T>(callback: (response: T) => void, options: Vars) => Promise<CancelSubscription>;
46
48
  };
47
49
  }
48
50
  declare global {
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("get-promisable-result"),e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,r=0,s=e.length;r<s;r++)!i&&r in e||(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,r,s,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(r||(r={})),function(t){t.PANEL_URL="panel_url"}(s||(s={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,l="ref",f="value",v="toJSON",b=function(t){return"".concat(l,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},E=function(t){return y("Entity",t)},g=function(t){return y("Domain",t)},w=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):E(e)},A=function(t){p.add(t)};return{get hass(){return t.hass},states:new Proxy((function(e){var n;if(d(e))return m(e),null===(n=t.hass.states[e])||void 0===n?void 0:n.state;throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),t.hass.states[n];var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||g(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),t[e]}})}}),is_state:function(e,n){var i;return m(e),(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,r;return m(e),null===(r=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===r?void 0:r[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(E(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||g(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,r.AREA_ID);var s=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(s)return this.device_attr(s,r.AREA_ID);var a=c().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 n,i,s;e in t.hass.devices&&(s=this.device_attr(e,r.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(s=this.device_attr(a,r.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===s}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.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?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var r=new Proxy(((n={})[f]=void 0,n[v]=function(){return this[f]},n),{get:function(t,e,n){if(e===f||e===v)return A(i),Reflect.get(t,e,n);w("".concat(e," is not a valid ").concat(l," property. A ").concat(l,' only exposes a "').concat(f,'" property'))},set:function(e,n,r){if(n===f){var s=e[f];return e[f]=r,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(s)},new_state:{state:JSON.stringify(r)}}}),!0}return w('property "'.concat(n,'" cannot be set in a ').concat(l)),!1}});return h.set(e,r),r},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):w("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(t,e){if(e===s.PANEL_URL)return A(s.PANEL_URL),location.pathname;n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(s).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,r=e.throwWarnings,a=void 0===r||r,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><])(".concat(Object.values(s).join("|"),")($|[ \\?)+:\\}\\]><.])"),"gm"),this._scopped=_(t,i,a),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(a.LOCATION_CHANGED,(function(e){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(s.PANEL_URL)&&this._executeRenderingFunctions(s.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.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 r=n._subscriptions.get(i);if(r.has(t)){var s=r.get(t);s.has(e)||s.add(e)}else r.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,r){if(i.has(t)){var s=i.get(t);s.has(e)&&s.delete(e),0===s.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(r))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var r=new Map(Object.entries(e(e({},this._variables),i))),s=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=s.includes("return")||!this._autoReturn?s:"return ".concat(s);return(new(Function.bind.apply(Function,n(n([void 0,"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","clientSide","ref","unref"],Array.from(r.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(r.values()),!1))}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()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t.getPromisableResult((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=h;
1
+ "use strict";var t=require("get-promisable-result"),e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,r=0,s=e.length;r<s;r++)!i&&r in e||(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,r,s,a,o="[home-assistant-javascript-templates]",c=/^([a-z_]+)\.(\w+)$/;!function(t){t.UNKNOWN="unknown",t.UNAVAILABLE="unavailable"}(i||(i={})),function(t){t.AREA_ID="area_id",t.NAME="name"}(r||(r={})),function(t){t.PANEL_URL="panel_url"}(s||(s={})),function(t){t.LOCATION_CHANGED="location-changed",t.POPSTATE="popstate",t.SUBSCRIBE_EVENTS="subscribe_events",t.STATE_CHANGE_EVENT="state_changed"}(a||(a={}));var u=function(t){return t.reduce((function(t,e){var n=e[0],i=e[1];return t[n.replace(c,"$2")]=i,t}),{})},d=function(t){return t.includes(".")};function _(t,e,n){var c=function(){return Object.entries(t.hass.areas)},_=function(){return Object.entries(t.hass.entities)},p=new Set,h=new Map,f="ref",v="value",l="toJSON",b=function(t){return"".concat(f,".").concat(t)},y=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},g=function(t){return y("Entity",t)},w=function(t){return y("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},m=function(e){t.hass.states[e]?p.add(e):g(e)},A=function(t){p.add(t)},N=function(e,n){var i=n.with_unit,r=void 0!==i&&i,s=n.rounded,a=void 0!==s&&s;if(e){var o=e.state,c=e.attributes.unit_of_measurement,u=Number(a),d=!1===a||isNaN(Number(o))?o:new Intl.NumberFormat(t.hass.language,{minimumFractionDigits:u,maximumFractionDigits:u}).format(Number(o));return r&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?N(t,{rounded:!0,with_unit:!0}):t[e]}})};return{get hass(){return t.hass},states:new Proxy((function(e,n){if(void 0===n&&(n={}),d(e))return m(e),N(t.hass.states[e],n);throw SyntaxError("".concat(o,": states method cannot be used with a domain, use it as an object instead."))}),{get:function(e,n){if(d(n))return m(n),S(t.hass.states[n]);var i=Object.entries(t.hass.states).filter((function(t){return t[0].startsWith(n)}));return i.length||w(n),new Proxy(u(i),{get:function(t,e){return m("".concat(n,".").concat(e)),S(t[e])}})}}),is_state:function(e,n){var i;return m(e),(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,r;return m(e),null===(r=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===r?void 0:r[n]},is_state_attr:function(t,e,n){return this.state_attr(t,e)===n},has_value:function(t){return this.states(t)?!(this.is_state(t,i.UNKNOWN)||this.is_state(t,i.UNAVAILABLE)):(g(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return m(e),t.hass.entities[e];var n=_().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return m("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return m(e),null===(i=t.hass.entities[e])||void 0===i?void 0:i[n]},is_entity_prop:function(t,e,n){return this.entity_prop(t,e)===n},devices:new Proxy((function(e){if(void 0===e)return t.hass.devices;if(d(e))throw SyntaxError("".concat(o,": devices method cannot be used with an entity id, you should use a device id instead."));return t.hass.devices[e]}),{get:function(e,n){if(d(n))throw SyntaxError("".concat(o,": devices cannot be accesed using an entity id, you should use a device id instead."));return t.hass.devices[n]}}),device_attr:function(e,n){var i;return null===(i=t.hass.devices[e])||void 0===i?void 0:i[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;return m(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id},areas:function(){return c().map((function(t){return t[1].area_id}))},area_id:function(e){var n,i;if(e in t.hass.devices)return this.device_attr(e,r.AREA_ID);var s=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(s)return this.device_attr(s,r.AREA_ID);var a=c().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 n,i,s;e in t.hass.devices&&(s=this.device_attr(e,r.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(s=this.device_attr(a,r.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===s}));return null===(i=null==o?void 0:o[1])||void 0===i?void 0:i.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?_().filter((function(t){return t[1].area_id===e[1].area_id})).map((function(t){return t[0]})):[]},area_devices:function(e){var n=c().find((function(t){var n=t[1];return n.area_id===e||n.name===e}));return n?Object.entries(t.hass.devices).filter((function(t){return t[1].area_id===n[1].area_id})).map((function(t){return t[1].id})):[]},get user_name(){return t.hass.user.name},get user_is_admin(){return t.hass.user.is_admin},get user_is_owner(){return t.hass.user.is_owner},get user_agent(){return window.navigator.userAgent},get tracked(){return p},cleanTracked:function(){p.clear()},ref:function(t,e){var n,i=b(e);if(h.has(e))return h.get(e);var r=new Proxy(((n={})[v]=void 0,n[l]=function(){return this[v]},n),{get:function(t,e,n){if(e===v||e===l)return A(i),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(f," property. A ").concat(f,' only exposes a "').concat(v,'" property'))},set:function(e,n,r){if(n===v){var s=e[v];return e[v]=r,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:i,old_state:{state:JSON.stringify(s)},new_state:{state:JSON.stringify(r)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(f)),!1}});return h.set(e,r),r},unref:function(t,e){var n=b(e);h.has(e)?(h.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},clientSideProxy:new Proxy({},{get:function(t,e){if(e===s.PANEL_URL)return A(s.PANEL_URL),location.pathname;n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(s).join(", ")))}})}}var p=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,r=e.throwWarnings,a=void 0===r||r,o=e.variables,c=void 0===o?{}:o,u=e.autoReturn,d=void 0===u||u;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._autoReturn=d,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><])(".concat(Object.values(s).join("|"),")($|[ \\?)+:\\}\\]><.])"),"gm"),this._scopped=_(t,i,a),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(a.LOCATION_CHANGED,(function(e){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(s.PANEL_URL)&&this._executeRenderingFunctions(s.PANEL_URL)},t.prototype._watchForEntitiesChange=function(){var t=this;window.hassConnection.then((function(e){e.conn.subscribeMessage((function(e){return t._entityWatchCallback(e)}),{type:a.SUBSCRIBE_EVENTS,event_type:a.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 r=n._subscriptions.get(i);if(r.has(t)){var s=r.get(t);s.has(e)||s.add(e)}else r.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,r){if(i.has(t)){var s=i.get(t);s.delete(e),0===s.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(r))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var r=new Map(Object.entries(e(e({},this._variables),i))),s=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),a=s.includes("return")||!this._autoReturn?s:"return ".concat(s);return(new(Function.bind.apply(Function,n(n([void 0,"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","clientSide","ref","unref"],Array.from(r.keys()),!1),["".concat('"use strict";'," ").concat(a)],!1)))).apply(void 0,n([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,this._scopped.clientSideProxy,this._scopped.ref.bind(this._scopped,this._entityWatchCallback.bind(this)),this._scopped.unref.bind(this._scopped,this.cleanTracked.bind(this))],Array.from(r.values()),!1))}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()},Object.defineProperty(t.prototype,"variables",{get:function(){return this._variables},set:function(t){this._variables=t},enumerable:!1,configurable:!0}),t}(),h=function(){function e(e,n){void 0===n&&(n={}),this._renderer=t.getPromisableResult((function(){return e.hass}),(function(t){return!!(t&&t.areas&&t.devices&&t.entities&&t.states&&t.user)}),{retries:100,delay:50,rejectMessage:"The provided element doesn't contain a proper or initialised hass object"}).then((function(){return new p(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=h;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "home-assistant-javascript-templates",
3
- "version": "5.7.0",
3
+ "version": "5.8.0",
4
4
  "description": "A JavaScript utility to render Home Assistant JavaScript templates",
5
5
  "keywords": [
6
6
  "home-assistant",
@@ -46,19 +46,19 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@rollup/plugin-terser": "^0.4.4",
49
- "@types/jest": "^29.5.14",
50
- "@types/node": "^22.13.10",
49
+ "@types/jest": "^30.0.0",
50
+ "@types/node": "^24.0.3",
51
51
  "get-promisable-result": "^1.0.1",
52
- "jest": "^29.7.0",
53
- "jest-environment-jsdom": "^29.7.0",
52
+ "jest": "^30.0.2",
53
+ "jest-environment-jsdom": "^30.0.2",
54
54
  "jest-location-mock": "^2.0.0",
55
- "rollup": "^4.35.0",
55
+ "rollup": "^4.44.0",
56
56
  "rollup-plugin-ts": "^3.4.5",
57
- "ts-jest": "^29.2.6",
57
+ "ts-jest": "^29.4.0",
58
58
  "tslib": "^2.8.1",
59
- "typescript": "^5.8.2"
59
+ "typescript": "^5.8.3"
60
60
  },
61
61
  "dependencies": {
62
- "get-promisable-result": "^1.0.0"
62
+ "get-promisable-result": "^1.0.1"
63
63
  }
64
64
  }