home-assistant-javascript-templates 6.0.0 → 6.0.1

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/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 s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,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"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",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(".")},h="ref",_="value",p="toJSON",f=function(t){return"".concat(h,".").concat(t)};function v(t,e,n){var c=function(){return Object.entries(t.hass.areas)},v=function(){return Object.entries(t.hass.devices)},l=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},m=function(t){return g("Entity",t)},w=function(t){return g("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},A=function(e){t.hass.states[e]?b.add(e):m(e)},N=function(t){b.add(t)},T=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;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 s&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?T(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 A(e),T(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 A(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 A("".concat(n,".").concat(e)),S(t[e])}})}}),state_translated:function(e){if(A(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return A(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return A(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[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)):(m(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return A(e),t.hass.entities[e];var n=l().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return A("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return A(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,s,r;if(d(e)){A(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(d(e))return A(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=v().find((function(t){return t[1].name===e}));return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(d(e)){A(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},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,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.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,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===r}));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?l().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?v().filter((function(t){return t[1].area_id===e[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 b},cleanTracked:function(){b.clear()},ref:function(t,e,n){var i;void 0===n&&(n=void 0);var s=f(e);if(y.has(e))return y.get(e);var r=new Proxy(((i={})[_]=n,i[p]=function(){return this[_]},i),{get:function(t,e,n){if(e===_||e===p)return N(s),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(_,'" property'))},set:function(e,n,i){if(n===_){var r=e[_];return e[_]=i,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,r),r},unref:function(t,e){var n=f(e);y.has(e)?(y.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},refs:function(t,e,n){void 0===n&&(n={});var i=this.ref,s=this.unref,r=new Proxy(n,{get:function(e,n){return i(t,n).value},set:function(e,n,s){return i(t,n).value=s,!0}});return Object.entries(n).forEach((function(n){var r=n[0],a=n[1];y.has(r)&&s(e,r),i(t,r,a)})),r},cleanRefs:function(t){var e=this;Array.from(y.keys()).forEach((function(n){e.unref(t,n)}))},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&N(i),i){case r.PANEL_URL:return location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var l=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,u=e.refs,d=void 0===u?{}:u,h=e.refsVariableName,_=void 0===h?"refs":h,p=e.autoReturn,f=void 0===p||p;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refsVariableName=_,this._autoReturn=f,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=v(t,i,a),this.refs=d,this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.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._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)}))},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,n){var i=this;this._scopped.tracked.forEach((function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))}))},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.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),d=u.includes("return")||!this._autoReturn?u:"return ".concat(u);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(d)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),o)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._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}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this))},set:function(t){this._scopped.cleanRefs(this.cleanTracked.bind(this)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),t)},enumerable:!1,configurable:!0}),t}(),b=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 l(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();export{b 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 s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,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"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",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(".")},h="ref",_="value",p="toJSON",f=function(t){return"".concat(h,".").concat(t)};function v(t,e,n){var c=function(){return Object.entries(t.hass.areas)},v=function(){return Object.entries(t.hass.devices)},l=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},m=function(t){return g("Entity",t)},w=function(t){return g("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},A=function(e){t.hass.states[e]?b.add(e):m(e)},N=function(t){b.add(t)},T=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;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 s&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?T(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 A(e),T(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 A(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 A("".concat(n,".").concat(e)),S(t[e])}})}}),state_translated:function(e){if(A(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return A(e),Array.isArray(n)?n.some(function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n}):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return A(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[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)):(m(t),!1)},entities:new Proxy(function(e){if(void 0===e)return t.hass.entities;if(d(e))return A(e),t.hass.entities[e];var n=l().filter(function(t){return t[0].startsWith(e)});return n.length||w(e),new Proxy(u(n),{get:function(t,n){return A("".concat(e,".").concat(n)),t[n]}})},{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return A(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,s,r;if(d(e)){A(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(d(e))return A(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=v().find(function(t){return t[1].name===e});return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(d(e)){A(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},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,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.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,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find(function(t){var n=t[1];return n.area_id===e||n.area_id===r});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?l().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?v().filter(function(t){return t[1].area_id===e[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 b},cleanTracked:function(){b.clear()},ref:function(t,e,n){var i;void 0===n&&(n=void 0);var s=f(e);if(y.has(e))return y.get(e);var r=new Proxy(((i={})[_]=n,i[p]=function(){return this[_]},i),{get:function(t,e,n){if(e===_||e===p)return N(s),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(_,'" property'))},set:function(e,n,i){if(n===_){var r=e[_];return e[_]=i,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,r),r},unref:function(t,e){var n=f(e);y.has(e)?(y.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},refs:function(t,e,n){void 0===n&&(n={});var i=this.ref,s=this.unref,r=new Proxy(n,{get:function(e,n){return i(t,n).value},set:function(e,n,s){return i(t,n).value=s,!0}});return Object.entries(n).forEach(function(n){var r=n[0],a=n[1];y.has(r)&&s(e,r),i(t,r,a)}),r},cleanRefs:function(t){var e=this;Array.from(y.keys()).forEach(function(n){e.unref(t,n)})},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&N(i),i){case r.PANEL_URL:return window.location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var l=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,u=e.refs,d=void 0===u?{}:u,h=e.refsVariableName,_=void 0===h?"refs":h,p=e.autoReturn,f=void 0===p||p;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refsVariableName=_,this._autoReturn=f,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=v(t,i,a),this.refs=d,this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach(function(t,n){t.forEach(function(t,i){e.trackTemplate(n,i,t)})})},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,function(){t._panelUrlWatchCallback()}),window.addEventListener(a.POPSTATE,function(){t._panelUrlWatchCallback()})},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.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._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)})},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,n){var i=this;this._scopped.tracked.forEach(function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))})},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.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}})},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),d=u.includes("return")||!this._autoReturn?u:"return ".concat(u);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(d)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),o)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._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}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this))},set:function(t){this._scopped.cleanRefs(this.cleanTracked.bind(this)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),t)},enumerable:!1,configurable:!0}),t}(),b=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 l(e,n)})}return e.prototype.getRenderer=function(){return this._renderer},e}();export{b as default};
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 s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,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"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",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(".")},h="ref",_="value",p="toJSON",f=function(t){return"".concat(h,".").concat(t)};function v(t,e,n){var c=function(){return Object.entries(t.hass.areas)},v=function(){return Object.entries(t.hass.devices)},l=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},m=function(t){return g("Entity",t)},w=function(t){return g("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},A=function(e){t.hass.states[e]?b.add(e):m(e)},N=function(t){b.add(t)},T=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;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 s&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?T(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 A(e),T(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 A(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 A("".concat(n,".").concat(e)),S(t[e])}})}}),state_translated:function(e){if(A(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return A(e),Array.isArray(n)?n.some((function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n})):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return A(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[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)):(m(t),!1)},entities:new Proxy((function(e){if(void 0===e)return t.hass.entities;if(d(e))return A(e),t.hass.entities[e];var n=l().filter((function(t){return t[0].startsWith(e)}));return n.length||w(e),new Proxy(u(n),{get:function(t,n){return A("".concat(e,".").concat(n)),t[n]}})}),{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return A(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,s,r;if(d(e)){A(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(d(e))return A(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=v().find((function(t){return t[1].name===e}));return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(d(e)){A(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},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,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.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,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find((function(t){var n=t[1];return n.area_id===e||n.area_id===r}));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?l().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?v().filter((function(t){return t[1].area_id===e[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 b},cleanTracked:function(){b.clear()},ref:function(t,e,n){var i;void 0===n&&(n=void 0);var s=f(e);if(y.has(e))return y.get(e);var r=new Proxy(((i={})[_]=n,i[p]=function(){return this[_]},i),{get:function(t,e,n){if(e===_||e===p)return N(s),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(_,'" property'))},set:function(e,n,i){if(n===_){var r=e[_];return e[_]=i,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,r),r},unref:function(t,e){var n=f(e);y.has(e)?(y.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},refs:function(t,e,n){void 0===n&&(n={});var i=this.ref,s=this.unref,r=new Proxy(n,{get:function(e,n){return i(t,n).value},set:function(e,n,s){return i(t,n).value=s,!0}});return Object.entries(n).forEach((function(n){var r=n[0],a=n[1];y.has(r)&&s(e,r),i(t,r,a)})),r},cleanRefs:function(t){var e=this;Array.from(y.keys()).forEach((function(n){e.unref(t,n)}))},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&N(i),i){case r.PANEL_URL:return location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var l=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,u=e.refs,d=void 0===u?{}:u,h=e.refsVariableName,_=void 0===h?"refs":h,p=e.autoReturn,f=void 0===p||p;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refsVariableName=_,this._autoReturn=f,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=v(t,i,a),this.refs=d,this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach((function(t,n){t.forEach((function(t,i){e.trackTemplate(n,i,t)}))}))},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,(function(){t._panelUrlWatchCallback()})),window.addEventListener(a.POPSTATE,(function(){t._panelUrlWatchCallback()}))},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.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._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,(function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)}))},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,n){var i=this;this._scopped.tracked.forEach((function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))}))},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.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}}))},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),d=u.includes("return")||!this._autoReturn?u:"return ".concat(u);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(d)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),o)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._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}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this))},set:function(t){this._scopped.cleanRefs(this.cleanTracked.bind(this)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),t)},enumerable:!1,configurable:!0}),t}(),b=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 l(e,n)}))}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=b;
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 s in e=arguments[n])Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var i,s=0,r=e.length;s<r;s++)!i&&s in e||(i||(i=Array.prototype.slice.call(e,0,s)),i[s]=e[s]);return t.concat(i||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i,s,r,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"}(s||(s={})),function(t){t.PANEL_URL="panel_url",t.LANG="lang"}(r||(r={})),function(t){t.LOCATION_CHANGED="location-changed",t.TRANSLATIONS_UPDATED="translations-updated",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(".")},h="ref",_="value",p="toJSON",f=function(t){return"".concat(h,".").concat(t)};function v(t,e,n){var c=function(){return Object.entries(t.hass.areas)},v=function(){return Object.entries(t.hass.devices)},l=function(){return Object.entries(t.hass.entities)},b=new Set,y=new Map,g=function(t,e){n&&console.warn("".concat(t," ").concat(e," used in a JavaScript template doesn't exist"))},m=function(t){return g("Entity",t)},w=function(t){return g("Domain",t)},E=function(t){var i=new SyntaxError(t);if(e)throw i;n&&console.warn(i)},A=function(e){t.hass.states[e]?b.add(e):m(e)},N=function(t){b.add(t)},T=function(e,n){var i=n.with_unit,s=void 0!==i&&i,r=n.rounded,a=void 0!==r&&r;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 s&&c?"".concat(d," ").concat(c):d}},S=function(t){return new Proxy(t,{get:function(t,e){return"state_with_unit"===e?T(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 A(e),T(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 A(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 A("".concat(n,".").concat(e)),S(t[e])}})}}),state_translated:function(e){if(A(e),t.hass.states[e])return t.hass.formatEntityState(t.hass.states[e])},is_state:function(e,n){var i;return A(e),Array.isArray(n)?n.some(function(n){var i;return(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n}):(null===(i=t.hass.states[e])||void 0===i?void 0:i.state)===n},state_attr:function(e,n){var i,s;return A(e),null===(s=null===(i=t.hass.states[e])||void 0===i?void 0:i.attributes)||void 0===s?void 0:s[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)):(m(t),!1)},entities:new Proxy(function(e){if(void 0===e)return t.hass.entities;if(d(e))return A(e),t.hass.entities[e];var n=l().filter(function(t){return t[0].startsWith(e)});return n.length||w(e),new Proxy(u(n),{get:function(t,n){return A("".concat(e,".").concat(n)),t[n]}})},{get:function(t,e){return t(e)}}),entity_prop:function(e,n){var i;return A(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,s,r;if(d(e)){A(e);var a=null===(i=t.hass.entities[e])||void 0===i?void 0:i.device_id;return null===(s=t.hass.devices[a])||void 0===s?void 0:s[n]}return null===(r=t.hass.devices[e])||void 0===r?void 0:r[n]},is_device_attr:function(t,e,n){return this.device_attr(t,e)===n},device_id:function(e){var n;if(d(e))return A(e),null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;var i=v().find(function(t){return t[1].name===e});return null==i?void 0:i[0]},device_name:function(e){var n,i,s;if(d(e)){A(e);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;return null===(i=t.hass.devices[r])||void 0===i?void 0:i.name}return null===(s=t.hass.devices[e])||void 0===s?void 0:s.name},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,s.AREA_ID);var r=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;if(r)return this.device_attr(r,s.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,r;e in t.hass.devices&&(r=this.device_attr(e,s.AREA_ID));var a=null===(n=t.hass.entities[e])||void 0===n?void 0:n.device_id;a&&(r=this.device_attr(a,s.AREA_ID));var o=c().find(function(t){var n=t[1];return n.area_id===e||n.area_id===r});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?l().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?v().filter(function(t){return t[1].area_id===e[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 b},cleanTracked:function(){b.clear()},ref:function(t,e,n){var i;void 0===n&&(n=void 0);var s=f(e);if(y.has(e))return y.get(e);var r=new Proxy(((i={})[_]=n,i[p]=function(){return this[_]},i),{get:function(t,e,n){if(e===_||e===p)return N(s),Reflect.get(t,e,n);E("".concat(e," is not a valid ").concat(h," property. A ").concat(h,' only exposes a "').concat(_,'" property'))},set:function(e,n,i){if(n===_){var r=e[_];return e[_]=i,t({event_type:a.STATE_CHANGE_EVENT,data:{entity_id:s,old_state:{state:JSON.stringify(r)},new_state:{state:JSON.stringify(i)}}}),!0}return E('property "'.concat(n,'" cannot be set in a ').concat(h)),!1}});return y.set(e,r),r},unref:function(t,e){var n=f(e);y.has(e)?(y.delete(e),t(n)):E("".concat(e," is not a ref or it has been unrefed already"))},refs:function(t,e,n){void 0===n&&(n={});var i=this.ref,s=this.unref,r=new Proxy(n,{get:function(e,n){return i(t,n).value},set:function(e,n,s){return i(t,n).value=s,!0}});return Object.entries(n).forEach(function(n){var r=n[0],a=n[1];y.has(r)&&s(e,r),i(t,r,a)}),r},cleanRefs:function(t){var e=this;Array.from(y.keys()).forEach(function(n){e.unref(t,n)})},clientSideProxy:new Proxy({},{get:function(e,i){switch(Object.values(r).includes(i)&&N(i),i){case r.PANEL_URL:return window.location.pathname;case r.LANG:return t.hass.language}n&&console.warn("clientSideProxy should only be used to access these variables: ".concat(Object.values(r).join(", ")))}})}}var l=function(){function t(t,e){var n=e.throwErrors,i=void 0!==n&&n,s=e.throwWarnings,a=void 0===s||s,o=e.variables,c=void 0===o?{}:o,u=e.refs,d=void 0===u?{}:u,h=e.refsVariableName,_=void 0===h?"refs":h,p=e.autoReturn,f=void 0===p||p;this._throwErrors=i,this._throwWarnings=a,this._variables=c,this._refsVariableName=_,this._autoReturn=f,this._subscriptions=new Map,this._clientSideEntitiesRegExp=new RegExp("(^|[ \\?(+:\\{\\[><,])(".concat(Object.values(r).join("|"),")($|[ \\?)+:\\}\\]><.,])"),"gm"),this._scopped=v(t,i,a),this.refs=d,this._watchForPanelUrlChange(),this._watchForEntitiesChange(),this._watchForLanguageChange()}return t.prototype._executeRenderingFunctions=function(t){var e=this;this._subscriptions.get(t).forEach(function(t,n){t.forEach(function(t,i){e.trackTemplate(n,i,t)})})},t.prototype._watchForPanelUrlChange=function(){var t=this;window.addEventListener(a.LOCATION_CHANGED,function(){t._panelUrlWatchCallback()}),window.addEventListener(a.POPSTATE,function(){t._panelUrlWatchCallback()})},t.prototype._panelUrlWatchCallback=function(){this._subscriptions.has(r.PANEL_URL)&&this._executeRenderingFunctions(r.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._watchForLanguageChange=function(){var t=this;window.addEventListener(a.TRANSLATIONS_UPDATED,function(){t._subscriptions.has(r.LANG)&&t._executeRenderingFunctions(r.LANG)})},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,n){var i=this;this._scopped.tracked.forEach(function(s){var r=[e,n];if(i._subscriptions.has(s)){var a=i._subscriptions.get(s);if(a.has(t)){var o=a.get(t);o.has(e)||o.set.apply(o,r)}else a.set(t,new Map([r]))}else i._subscriptions.set(s,new Map([[t,new Map([r])]]))})},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.delete(e),0===r.size&&(i.delete(t),0===i.size&&n._subscriptions.delete(s))}})},t.prototype.renderTemplate=function(t,i){void 0===i&&(i={});try{var s=i.variables,r=void 0===s?{}:s,a=i.refs,o=void 0===a?{}:a,c=new Map(Object.entries(e(e({},this._variables),r))),u=t.trim().replace(this._clientSideEntitiesRegExp,"$1clientSide.$2$3"),d=u.includes("return")||!this._autoReturn?u:"return ".concat(u);return(new(Function.bind.apply(Function,n(n([void 0,"hass","states","state_translated","is_state","state_attr","is_state_attr","has_value","entities","entity_prop","is_entity_prop","devices","device_attr","is_device_attr","device_id","device_name","areas","area_id","area_name","area_entities","area_devices","user_name","user_is_admin","user_is_owner","user_agent","clientSide","ref","unref",this._refsVariableName],Array.from(c.keys()),!1),["".concat('"use strict";'," ").concat(d)],!1)))).apply(void 0,n([this._scopped.hass,this._scopped.states,this._scopped.state_translated.bind(this._scopped),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.device_name.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)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),o)],Array.from(c.values()),!1))}catch(t){if(this._throwErrors)throw t;return void(this._throwWarnings&&console.warn(t))}},t.prototype.trackTemplate=function(t,e,n){var i=this;void 0===n&&(n={}),this._scopped.cleanTracked();var s=this.renderTemplate(t,n);return this._storeTracked(t,e,n),e(s),function(){return i._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}),Object.defineProperty(t.prototype,"refs",{get:function(){return this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this))},set:function(t){this._scopped.cleanRefs(this.cleanTracked.bind(this)),this._scopped.refs(this._entityWatchCallback.bind(this),this.cleanTracked.bind(this),t)},enumerable:!1,configurable:!0}),t}(),b=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 l(e,n)})}return e.prototype.getRenderer=function(){return this._renderer},e}();module.exports=b;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "home-assistant-javascript-templates",
3
- "version": "6.0.0",
3
+ "version": "6.0.1",
4
4
  "description": "A JavaScript utility to render Home Assistant JavaScript templates",
5
5
  "keywords": [
6
6
  "home-assistant",
@@ -45,26 +45,25 @@
45
45
  "postversion": "git push && git push --tags"
46
46
  },
47
47
  "devDependencies": {
48
- "@rollup/plugin-terser": "^0.4.4",
48
+ "@rollup/plugin-terser": "^1.0.0",
49
49
  "@types/jest": "^30.0.0",
50
- "@types/node": "^24.10.1",
51
- "get-promisable-result": "^1.0.1",
50
+ "@types/node": "^25.3.5",
51
+ "get-promisable-result": "^1.0.2",
52
52
  "jest": "^30.2.0",
53
53
  "jest-environment-jsdom": "^30.2.0",
54
- "jest-location-mock": "^2.0.0",
55
- "rollup": "^4.53.3",
54
+ "jest-location-mock": "^3.0.0",
55
+ "rollup": "^4.59.0",
56
56
  "rollup-plugin-ts": "^3.4.5",
57
57
  "ts-jest": "^29.4.6",
58
58
  "tslib": "^2.8.1",
59
59
  "typescript": "^5.9.3"
60
60
  },
61
61
  "dependencies": {
62
- "get-promisable-result": "^1.0.1"
62
+ "get-promisable-result": "^1.0.2"
63
63
  },
64
64
  "pnpm": {
65
65
  "overrides": {
66
- "glob@>=10.3.7 <=11.0.3": ">=11.1.0",
67
- "js-yaml@<4.1.1": ">=4.1.1"
66
+ "@isaacs/brace-expansion@<=5.0.0": ">=5.0.1"
68
67
  }
69
68
  }
70
69
  }