Set lovelace when restoring view from cache, optimise picture element and thermostat (#5880)

This commit is contained in:
Bram Kragten 2020-05-14 18:44:38 +02:00 committed by GitHub
parent 349355584a
commit dcbbaf08f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 34 deletions

View File

@ -21,6 +21,8 @@ import { PictureElementsCardConfig } from "./types";
class HuiPictureElementsCard extends LitElement implements LovelaceCard { class HuiPictureElementsCard extends LitElement implements LovelaceCard {
@property() public hass?: HomeAssistant; @property() public hass?: HomeAssistant;
@property() private _elements?: LovelaceElement[];
public static getStubConfig( public static getStubConfig(
hass: HomeAssistant, hass: HomeAssistant,
entities: string[], entities: string[],
@ -70,6 +72,14 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
} }
this._config = config; this._config = config;
this._elements = this._config.elements.map(
(elementConfig: LovelaceElementConfig) => {
const element = createStyledHuiElement(elementConfig);
element.hass = this.hass;
return element as LovelaceElement;
}
);
} }
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
@ -78,11 +88,8 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
return; return;
} }
if (changedProps.has("hass")) { if (this._elements && changedProps.has("hass")) {
for (const el of Array.from( for (const element of this._elements) {
this.shadowRoot!.querySelectorAll("#root > *")
)) {
const element = el as LovelaceElement;
element.hass = this.hass; element.hass = this.hass;
} }
} }
@ -120,14 +127,7 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
.entity=${this._config.entity} .entity=${this._config.entity}
.aspectRatio=${this._config.aspect_ratio} .aspectRatio=${this._config.aspect_ratio}
></hui-image> ></hui-image>
${this._config.elements.map( ${this._elements}
(elementConfig: LovelaceElementConfig) => {
const element = createStyledHuiElement(elementConfig);
element.hass = this.hass;
return element;
}
)}
</div> </div>
</ha-card> </ha-card>
`; `;

View File

@ -88,15 +88,6 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
this._config = config; this._config = config;
} }
public connectedCallback(): void {
super.connectedCallback();
this.rescale_svg();
}
protected firstUpdated(): void {
this.rescale_svg();
}
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.hass || !this._config) { if (!this.hass || !this._config) {
return html``; return html``;
@ -165,7 +156,9 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
<g> <g>
<text text-anchor="middle" class="set-value"> <text text-anchor="middle" class="set-value">
${ ${
this._setTemp === undefined || this._setTemp === null stateObj.state === UNAVAILABLE
? this.hass.localize("state.default.unavailable")
: this._setTemp === undefined || this._setTemp === null
? "" ? ""
: Array.isArray(this._setTemp) : Array.isArray(this._setTemp)
? this._stepSize === 1 ? this._stepSize === 1
@ -255,12 +248,19 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
} }
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
if (changedProps.has("_setTemp")) {
return true;
}
return hasConfigOrEntityChanged(this, changedProps); return hasConfigOrEntityChanged(this, changedProps);
} }
protected updated(changedProps: PropertyValues): void { protected updated(changedProps: PropertyValues): void {
super.updated(changedProps); super.updated(changedProps);
if (changedProps.has("_setTemp")) {
this.rescale_svg();
}
if ( if (
!this._config || !this._config ||
!this.hass || !this.hass ||
@ -287,8 +287,16 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
if (!stateObj) { if (!stateObj) {
return; return;
} }
this._setTemp = this._getSetTemp(stateObj); const newTemp = this._getSetTemp(stateObj);
this.rescale_svg(); if (
Array.isArray(this._setTemp) &&
Array.isArray(newTemp) &&
(this._setTemp[0] !== newTemp[0] || this._setTemp[1] !== newTemp[1])
) {
this._setTemp = newTemp;
} else if (this._setTemp !== newTemp) {
this._setTemp = newTemp;
}
} }
private rescale_svg() { private rescale_svg() {
@ -322,9 +330,11 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
return this.hass!.config.unit_system.temperature === UNIT_F ? 1 : 0.5; return this.hass!.config.unit_system.temperature === UNIT_F ? 1 : 0.5;
} }
private _getSetTemp(stateObj: HassEntity) { private _getSetTemp(
stateObj: HassEntity
): undefined | number | [number, number] {
if (stateObj.state === UNAVAILABLE) { if (stateObj.state === UNAVAILABLE) {
return this.hass!.localize("state.default.unavailable"); return undefined;
} }
if ( if (

View File

@ -633,17 +633,16 @@ class HUIRoot extends LitElement {
if (viewConfig.panel && viewConfig.cards && viewConfig.cards.length > 0) { if (viewConfig.panel && viewConfig.cards && viewConfig.cards.length > 0) {
view = document.createElement("hui-panel-view"); view = document.createElement("hui-panel-view");
view.config = viewConfig; view.config = viewConfig;
view.lovelace = this.lovelace;
view.index = viewIndex; view.index = viewIndex;
} else { } else {
view = document.createElement("hui-view"); view = document.createElement("hui-view");
view.lovelace = this.lovelace;
view.columns = this.columns; view.columns = this.columns;
view.index = viewIndex; view.index = viewIndex;
} }
this._viewCache![viewIndex] = view; this._viewCache![viewIndex] = view;
} }
view.lovelace = this.lovelace;
view.hass = this.hass; view.hass = this.hass;
const configBackground = viewConfig.background || this.config.background; const configBackground = viewConfig.background || this.config.background;

View File

@ -157,7 +157,7 @@ export class HUIView extends LitElement {
if (configChanged) { if (configChanged) {
this._createCards(lovelace.config.views[this.index!]); this._createCards(lovelace.config.views[this.index!]);
} else if (editModeChanged || changedProperties.has("columns")) { } else if (editModeChanged || changedProperties.has("columns")) {
this._recreateColumns(); this._createColumns();
} }
if (hassChanged && !configChanged) { if (hassChanged && !configChanged) {
@ -217,10 +217,6 @@ export class HUIView extends LitElement {
root.style.display = elements.length > 0 ? "block" : "none"; root.style.display = elements.length > 0 ? "block" : "none";
} }
private async _recreateColumns() {
this._createColumns();
}
private _createColumns() { private _createColumns() {
this._createColumnsIteration++; this._createColumnsIteration++;
const iteration = this._createColumnsIteration; const iteration = this._createColumnsIteration;