mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
History Graph Editor to ha form (#11797)
This commit is contained in:
parent
5f69a4c165
commit
2a98ace0b3
@ -87,8 +87,8 @@ export interface TargetSelector {
|
|||||||
|
|
||||||
export interface NumberSelector {
|
export interface NumberSelector {
|
||||||
number: {
|
number: {
|
||||||
min: number;
|
min?: number;
|
||||||
max: number;
|
max?: number;
|
||||||
step?: number;
|
step?: number;
|
||||||
mode?: "box" | "slider";
|
mode?: "box" | "slider";
|
||||||
unit_of_measurement?: string;
|
unit_of_measurement?: string;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import "@polymer/paper-input/paper-input";
|
import "../../../../components/ha-form/ha-form";
|
||||||
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import {
|
import {
|
||||||
array,
|
array,
|
||||||
@ -11,16 +11,15 @@ import {
|
|||||||
assign,
|
assign,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import { HistoryGraphCardConfig } from "../../cards/types";
|
import type { HistoryGraphCardConfig } from "../../cards/types";
|
||||||
import "../../components/hui-entity-editor";
|
import "../../components/hui-entity-editor";
|
||||||
import { EntityConfig } from "../../entity-rows/types";
|
import type { EntityConfig } from "../../entity-rows/types";
|
||||||
import { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { processEditorEntities } from "../process-editor-entities";
|
import { processEditorEntities } from "../process-editor-entities";
|
||||||
import { entitiesConfigStruct } from "../structs/entities-struct";
|
import { entitiesConfigStruct } from "../structs/entities-struct";
|
||||||
import { EditorTarget, EntitiesEditorEvent } from "../types";
|
|
||||||
import { configElementStyle } from "./config-elements-style";
|
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
|
import type { HaFormSchema } from "../../../../components/ha-form/types";
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@ -32,6 +31,21 @@ const cardConfigStruct = assign(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const SCHEMA: HaFormSchema[] = [
|
||||||
|
{ name: "title", selector: { text: {} } },
|
||||||
|
{
|
||||||
|
name: "",
|
||||||
|
type: "grid",
|
||||||
|
schema: [
|
||||||
|
{ name: "hours_to_show", selector: { number: { min: 1, mode: "box" } } },
|
||||||
|
{
|
||||||
|
name: "refresh_interval",
|
||||||
|
selector: { number: { min: 1, mode: "box" } },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
@customElement("hui-history-graph-card-editor")
|
@customElement("hui-history-graph-card-editor")
|
||||||
export class HuiHistoryGraphCardEditor
|
export class HuiHistoryGraphCardEditor
|
||||||
extends LitElement
|
extends LitElement
|
||||||
@ -49,104 +63,48 @@ export class HuiHistoryGraphCardEditor
|
|||||||
this._configEntities = processEditorEntities(config.entities);
|
this._configEntities = processEditorEntities(config.entities);
|
||||||
}
|
}
|
||||||
|
|
||||||
get _title(): string {
|
|
||||||
return this._config!.title || "";
|
|
||||||
}
|
|
||||||
|
|
||||||
get _hours_to_show(): number {
|
|
||||||
return this._config!.hours_to_show || 24;
|
|
||||||
}
|
|
||||||
|
|
||||||
get _refresh_interval(): number {
|
|
||||||
return this._config!.refresh_interval || 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="card-config">
|
<ha-form
|
||||||
<paper-input
|
.hass=${this.hass}
|
||||||
.label="${this.hass.localize(
|
.data=${this._config}
|
||||||
"ui.panel.lovelace.editor.card.generic.title"
|
.schema=${SCHEMA}
|
||||||
)} (${this.hass.localize(
|
.computeLabel=${this._computeLabelCallback}
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
@value-changed=${this._valueChanged}
|
||||||
)})"
|
></ha-form>
|
||||||
.value=${this._title}
|
<hui-entity-editor
|
||||||
.configValue=${"title"}
|
.hass=${this.hass}
|
||||||
@value-changed=${this._valueChanged}
|
.entities=${this._configEntities}
|
||||||
></paper-input>
|
@entities-changed=${this._entitiesChanged}
|
||||||
<div class="side-by-side">
|
></hui-entity-editor>
|
||||||
<paper-input
|
|
||||||
type="number"
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.hours_to_show"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._hours_to_show}
|
|
||||||
min="1"
|
|
||||||
.configValue=${"hours_to_show"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></paper-input>
|
|
||||||
<paper-input
|
|
||||||
type="number"
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.refresh_interval"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._refresh_interval}
|
|
||||||
.configValue=${"refresh_interval"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></paper-input>
|
|
||||||
</div>
|
|
||||||
<hui-entity-editor
|
|
||||||
.hass=${this.hass}
|
|
||||||
.entities=${this._configEntities}
|
|
||||||
@entities-changed=${this._valueChanged}
|
|
||||||
></hui-entity-editor>
|
|
||||||
</div>
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: EntitiesEditorEvent): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
if (!this._config || !this.hass) {
|
fireEvent(this, "config-changed", { config: ev.detail.value });
|
||||||
return;
|
|
||||||
}
|
|
||||||
const target = ev.target! as EditorTarget;
|
|
||||||
|
|
||||||
if (!ev.detail && this[`_${target.configValue}`] === target.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ev.detail && ev.detail.entities) {
|
|
||||||
this._config = { ...this._config, entities: ev.detail.entities };
|
|
||||||
this._configEntities = processEditorEntities(this._config.entities);
|
|
||||||
} else if (target.configValue) {
|
|
||||||
if (target.value === "") {
|
|
||||||
this._config = { ...this._config };
|
|
||||||
delete this._config[target.configValue!];
|
|
||||||
} else {
|
|
||||||
let value: any = target.value;
|
|
||||||
if (target.type === "number") {
|
|
||||||
value = Number(value);
|
|
||||||
}
|
|
||||||
this._config = {
|
|
||||||
...this._config,
|
|
||||||
[target.configValue!]: value,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
private _entitiesChanged(ev: CustomEvent): void {
|
||||||
return configElementStyle;
|
let config = this._config!;
|
||||||
|
|
||||||
|
config = { ...config, entities: ev.detail.entities };
|
||||||
|
this._configEntities = processEditorEntities(config.entities);
|
||||||
|
|
||||||
|
fireEvent(this, "config-changed", { config });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _computeLabelCallback = (schema: HaFormSchema) =>
|
||||||
|
this.hass!.localize(`ui.panel.lovelace.editor.card.generic.${schema.name}`);
|
||||||
|
|
||||||
|
static styles: CSSResultGroup = css`
|
||||||
|
ha-form {
|
||||||
|
margin-bottom: 24px;
|
||||||
|
}
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user