Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entities card: propagate hass correctly #1918

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/panels/lovelace/cards/hui-entities-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import {
html,
LitElement,
PropertyDeclarations,
PropertyValues,
} from "@polymer/lit-element";
import { TemplateResult } from "lit-html";

import "../../../components/ha-card.js";
import "../components/hui-entities-toggle.js";
Expand Down Expand Up @@ -36,13 +42,19 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
protected _config?: Config;
protected _configEntities?: ConfigEntity[];

set hass(hass) {
set hass(hass: HomeAssistant) {
this._hass = hass;
this.shadowRoot!.querySelectorAll("#states > *").forEach(
this.shadowRoot!.querySelectorAll("#states > div > *").forEach(
(element: unknown) => {
(element as EntityRow).hass = hass;
}
);
const entitiesToggle = this.shadowRoot!.querySelector(
"hui-entities-toggle"
);
if (entitiesToggle) {
(entitiesToggle as any).hass = hass;
}
}

static get properties(): PropertyDeclarations {
Expand All @@ -51,15 +63,15 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
};
}

public getCardSize() {
public getCardSize(): number {
if (!this._config) {
return 0;
}
// +1 for the header
return (this._config.title ? 1 : 0) + this._config.entities.length;
}

public setConfig(config: Config) {
public setConfig(config: Config): void {
const entities = processConfigEntities(config.entities);
for (const entity of entities) {
if (
Expand All @@ -78,14 +90,18 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
this._configEntities = entities;
}

protected render() {
protected updated(_changedProperties: PropertyValues): void {
if (this._hass && this._config) {
applyThemesOnElement(this, this._hass.themes, this._config.theme);
}
}

protected render(): TemplateResult {
if (!this._config || !this._hass) {
return html``;
}
const { show_header_toggle, title } = this._config;

applyThemesOnElement(this, this._hass.themes, this._config.theme);

return html`
${this.renderStyle()}
<ha-card>
Expand All @@ -104,8 +120,7 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
.entities="${this._configEntities!.map(
(conf) => conf.entity
)}"
>
</hui-entities-toggle>`
></hui-entities-toggle>`
}
</div>`
}
Expand All @@ -118,7 +133,7 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
`;
}

private renderStyle() {
private renderStyle(): TemplateResult {
return html`
<style>
ha-card {
Expand Down Expand Up @@ -153,7 +168,7 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
`;
}

private renderEntity(entityConf) {
private renderEntity(entityConf: ConfigEntity): TemplateResult {
const element = createRowElement(entityConf);
if (this._hass) {
element.hass = this._hass;
Expand All @@ -169,9 +184,8 @@ class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement)
return html`<div>${element}</div>`;
}

private _handleClick(entityConf: ConfigEntity) {
private _handleClick(entityConf: ConfigEntity): void {
const entityId = entityConf.entity;

fireEvent(this, "hass-more-info", { entityId });
}
}
Expand Down