Skip to content

Commit

Permalink
fix(#12): Handle HUI Elements (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosabot authored Sep 15, 2024
1 parent 14fd529 commit 4b14d3b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/streamline-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { version } from "../package.json";
// If the card is errored, try to recreate it
if (this._card.nodeName === "HUI-ERROR-CARD") {
this._shadow.removeChild(this._card);
this._card = HELPERS.createCardElement(this._config);
this.createCard();
this._shadow.appendChild(this._card);
} else {
this._card.setConfig?.(this._config);
Expand Down Expand Up @@ -104,22 +104,22 @@ import { version } from "../package.json";
);
}

const templateConfig =
this._templateConfig =
lovelace.config.streamline_templates[this._originalConfig.template];
if (!templateConfig) {
if (!this._templateConfig) {
throw new Error(
`The template "${this._originalConfig.template}" doesn't exist in streamline_templates`,
);
} else if (!(templateConfig.card || templateConfig.element)) {
} else if (!(this._templateConfig.card || this._templateConfig.element)) {
throw new Error(
"You should define either a card or an element in the template",
);
} else if (templateConfig.card && templateConfig.element) {
} else if (this._templateConfig.card && this._templateConfig.element) {
throw new Error("You can define a card and an element in the template");
}

this._config = evaluateConfig(
templateConfig,
this._templateConfig,
this._originalConfig.variables,
this._hass,
);
Expand All @@ -143,7 +143,7 @@ import { version } from "../package.json";
if (typeof this._config.type === "undefined") {
throw new Error("[Streamline Card] You need to define a type");
}
this._card = HELPERS.createCardElement(this._config);
this.createCard();
this._shadow.appendChild(this._card);
}

Expand All @@ -158,6 +158,19 @@ import { version } from "../package.json";
return this._card?.getLayoutOptions?.();
}

createCard() {
if (this._templateConfig.card) {
this._card = HELPERS.createCardElement(this._config);
} else if (this._templateConfig.element) {
this._card = HELPERS.createHuiElement(this._config);
if (this._config.style) {
Object.keys(this._config.style).forEach((prop) => {
this.style.setProperty(prop, this._config.style[prop]);
});
}
}
}

static getConfigElement() {
return document.createElement("streamline-card-editor");
}
Expand Down

0 comments on commit 4b14d3b

Please sign in to comment.