UNPKG

ha-template

Version:

A tiny (<2kB) lit component for rendering templates in Home Assistant custom cards

2 lines (1 loc) 1.5 kB
import{LitElement as e,nothing as t}from"lit";import{property as s,state as r}from"lit/decorators.js";function o(e,t,s,r){var o,i=arguments.length,n=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,s):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,s,r);else for(var c=e.length-1;c>=0;c--)(o=e[c])&&(n=(i<3?o(n):i>3?o(t,s,n):o(t,s))||n);return i>3&&n&&Object.defineProperty(t,s,n),n}"function"==typeof SuppressedError&&SuppressedError;class i extends e{constructor(){super(...arguments),this.template="",this.variables={},this.value=null,this.unsubscribePromise=null}connectedCallback(){super.connectedCallback(),this.hass?this.unsubscribePromise||this.template&&(this.unsubscribePromise=this.hass.connection.subscribeMessage((e=>{this.value=e.result}),{type:"render_template",template:this.template,variables:this.variables})):console.warn("hass object is not provided")}async disconnectedCallback(){if(super.disconnectedCallback(),this.unsubscribePromise)try{const e=await this.unsubscribePromise;return this.unsubscribePromise=null,e()}catch(e){if("not_found"!==e.code)throw e}}render(){var e;return null!==(e=this.value)&&void 0!==e?e:t}}function n(e="ha-template"){customElements.get(e)||customElements.define(e,i)}o([s()],i.prototype,"hass",void 0),o([s()],i.prototype,"template",void 0),o([s()],i.prototype,"variables",void 0),o([s()],i.prototype,"value",void 0),o([r()],i.prototype,"unsubscribePromise",void 0);export{i as HATemplate,n as default};