Fix sensor card from not working with detail (#7060)

* Fix sensor card from not working with detail

* comments
This commit is contained in:
Zack Barett 2020-09-22 12:06:42 -05:00 committed by GitHub
parent 6d5c6e2fbc
commit 0304c0eca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 34 deletions

View File

@ -5,22 +5,24 @@ import "@polymer/paper-listbox/paper-listbox";
import { import {
customElement, customElement,
html, html,
internalProperty,
LitElement, LitElement,
property, property,
internalProperty,
TemplateResult, TemplateResult,
} from "lit-element"; } from "lit-element";
import { assert, number, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { stateIcon } from "../../../../common/entity/state_icon"; import { stateIcon } from "../../../../common/entity/state_icon";
import "../../../../components/entity/ha-entity-picker"; import "../../../../components/entity/ha-entity-picker";
import "../../../../components/ha-formfield";
import "../../../../components/ha-icon-input"; import "../../../../components/ha-icon-input";
import "../../../../components/ha-switch";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
import { SensorCardConfig } from "../../cards/types"; import { SensorCardConfig } from "../../cards/types";
import "../../components/hui-theme-select-editor"; import "../../components/hui-theme-select-editor";
import { LovelaceCardEditor } from "../../types"; import { LovelaceCardEditor } from "../../types";
import { EditorTarget, EntitiesEditorEvent } from "../types"; import { EditorTarget, EntitiesEditorEvent } from "../types";
import { configElementStyle } from "./config-elements-style"; import { configElementStyle } from "./config-elements-style";
import { string, assert, object, optional, number } from "superstruct";
const cardConfigStruct = object({ const cardConfigStruct = object({
type: string(), type: string(),
@ -68,8 +70,8 @@ export class HuiSensorCardEditor extends LitElement
return this._config!.unit || ""; return this._config!.unit || "";
} }
get _detail(): number | string { get _detail(): number {
return this._config!.number || "1"; return this._config!.detail ?? 1;
} }
get _theme(): string { get _theme(): string {
@ -97,10 +99,10 @@ export class HuiSensorCardEditor extends LitElement
"ui.panel.lovelace.editor.card.config.required" "ui.panel.lovelace.editor.card.config.required"
)})" )})"
.hass=${this.hass} .hass=${this.hass}
.value="${this._entity}" .value=${this._entity}
.configValue=${"entity"} .configValue=${"entity"}
.includeDomains=${includeDomains} .includeDomains=${includeDomains}
@change="${this._valueChanged}" @change=${this._valueChanged}
allow-custom-entity allow-custom-entity
></ha-entity-picker> ></ha-entity-picker>
<paper-input <paper-input
@ -109,9 +111,9 @@ export class HuiSensorCardEditor extends LitElement
)} (${this.hass.localize( )} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional" "ui.panel.lovelace.editor.card.config.optional"
)})" )})"
.value="${this._name}" .value=${this._name}
.configValue="${"name"}" .configValue=${"name"}
@value-changed="${this._valueChanged}" @value-changed=${this._valueChanged}
></paper-input> ></paper-input>
<div class="side-by-side"> <div class="side-by-side">
<ha-icon-input <ha-icon-input
@ -132,15 +134,15 @@ export class HuiSensorCardEditor extends LitElement
)} (${this.hass.localize( )} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional" "ui.panel.lovelace.editor.card.config.optional"
)})" )})"
.configValue="${"graph"}" .configValue=${"graph"}
@value-changed="${this._valueChanged}" @value-changed=${this._valueChanged}
> >
<paper-listbox <paper-listbox
slot="dropdown-content" slot="dropdown-content"
.selected="${graphs.indexOf(this._graph)}" .selected=${graphs.indexOf(this._graph)}
> >
${graphs.map((graph) => { ${graphs.map((graph) => {
return html` <paper-item>${graph}</paper-item> `; return html`<paper-item>${graph}</paper-item>`;
})} })}
</paper-listbox> </paper-listbox>
</paper-dropdown-menu> </paper-dropdown-menu>
@ -152,28 +154,28 @@ export class HuiSensorCardEditor extends LitElement
)} (${this.hass.localize( )} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional" "ui.panel.lovelace.editor.card.config.optional"
)})" )})"
.value="${this._unit}" .value=${this._unit}
.configValue="${"unit"}" .configValue=${"unit"}
@value-changed="${this._valueChanged}" @value-changed=${this._valueChanged}
></paper-input>
<paper-input
.label="${this.hass.localize(
"ui.panel.lovelace.editor.card.sensor.graph_detail"
)} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})"
type="number"
.value="${this._detail}"
.configValue="${"detail"}"
@value-changed="${this._valueChanged}"
></paper-input> ></paper-input>
<ha-formfield
label=${this.hass.localize(
"ui.panel.lovelace.editor.card.sensor.show_more_detail"
)}
>
<ha-switch
.checked=${this._detail === 2}
.configValue=${"detail"}
@change=${this._change}
></ha-switch>
</ha-formfield>
</div> </div>
<div class="side-by-side"> <div class="side-by-side">
<hui-theme-select-editor <hui-theme-select-editor
.hass=${this.hass} .hass=${this.hass}
.value="${this._theme}" .value=${this._theme}
.configValue="${"theme"}" .configValue=${"theme"}
@value-changed="${this._valueChanged}" @value-changed=${this._valueChanged}
></hui-theme-select-editor> ></hui-theme-select-editor>
<paper-input <paper-input
.label="${this.hass.localize( .label="${this.hass.localize(
@ -182,15 +184,34 @@ export class HuiSensorCardEditor extends LitElement
"ui.panel.lovelace.editor.card.config.optional" "ui.panel.lovelace.editor.card.config.optional"
)})" )})"
type="number" type="number"
.value="${this._hours_to_show}" .value=${this._hours_to_show}
.configValue="${"hours_to_show"}" .configValue=${"hours_to_show"}
@value-changed="${this._valueChanged}" @value-changed=${this._valueChanged}
></paper-input> ></paper-input>
</div> </div>
</div> </div>
`; `;
} }
private _change(ev: Event) {
if (!this._config || !this.hass) {
return;
}
const value = (ev.target! as EditorTarget).checked ? 2 : 1;
if (this._detail === value) {
return;
}
this._config = {
...this._config,
detail: value,
};
fireEvent(this, "config-changed", { config: this._config });
}
private _valueChanged(ev: EntitiesEditorEvent): void { private _valueChanged(ev: EntitiesEditorEvent): void {
if (!this._config || !this.hass) { if (!this._config || !this.hass) {
return; return;

View File

@ -2386,7 +2386,7 @@
}, },
"sensor": { "sensor": {
"name": "Sensor", "name": "Sensor",
"graph_detail": "Graph Detail", "show_more_detail": "Show more detail",
"graph_type": "Graph Type", "graph_type": "Graph Type",
"description": "The Sensor card gives you a quick overview of your sensors state with an optional graph to visualize change over time." "description": "The Sensor card gives you a quick overview of your sensors state with an optional graph to visualize change over time."
}, },