Skip to content

Commit

Permalink
Use packaged create functions to allow creation of lazy elements
Browse files Browse the repository at this point in the history
  • Loading branch information
iantrich committed Feb 20, 2020
1 parent 83be6ab commit 3f124bb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restriction-card",
"version": "1.1.7",
"version": "1.1.8",
"description": "Lovelace restriction-card",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '1.1.7';
export const CARD_VERSION = '1.1.8';
17 changes: 16 additions & 1 deletion src/restriction-card.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TemplateResult, customElement, LitElement, property, html, CSSResult, css, PropertyValues } from 'lit-element';
import { classMap } from 'lit-html/directives/class-map';

Expand All @@ -24,6 +25,7 @@ console.info(
class RestrictionCard extends LitElement implements LovelaceCard {
@property() protected _config?: RestrictionCardConfig;
@property() protected _hass?: HomeAssistant;
@property() private _helpers?: any;

set hass(hass: HomeAssistant) {
this._hass = hass;
Expand Down Expand Up @@ -57,6 +59,8 @@ class RestrictionCard extends LitElement implements LovelaceCard {
}

this._config = { duration: 5, action: 'tap', ...config };

this.loadCardHelpers();
}

protected shouldUpdate(changedProps: PropertyValues): boolean {
Expand Down Expand Up @@ -117,7 +121,14 @@ class RestrictionCard extends LitElement implements LovelaceCard {

private renderCard(config: LovelaceCardConfig): TemplateResult {
if (this._hass && this._config) {
const element = createThing(config, this._config.row);
let element;

if (this._config.row) {
element = this._helpers ? this._helpers.createEntityRow(config) : createThing(config, this._config.row);
} else {
element = this._helpers ? this._helpers.createCardElement(config) : createThing(config, this._config.row);
}

element.hass = this._hass;

return html`
Expand All @@ -140,6 +151,10 @@ class RestrictionCard extends LitElement implements LovelaceCard {
);
}

private async loadCardHelpers(): Promise<void> {
this._helpers = await (window as any).loadCardHelpers();
}

private _handleAction(ev): void {
if (this._config && this._config.action === ev.detail.action) {
this._handleRestriction();
Expand Down

0 comments on commit 3f124bb

Please sign in to comment.