mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 17:26:42 +00:00
Lovelace Entity Card Editor to Ha Form - Adds Theme Selector and HaFormColumn (#11731)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
794bc161c8
commit
6cac7eeff0
73
src/components/ha-form/ha-form-grid.ts
Normal file
73
src/components/ha-form/ha-form-grid.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import "./ha-form";
|
||||||
|
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
||||||
|
import { customElement, property } from "lit/decorators";
|
||||||
|
import type {
|
||||||
|
HaFormGridSchema,
|
||||||
|
HaFormDataContainer,
|
||||||
|
HaFormElement,
|
||||||
|
HaFormSchema,
|
||||||
|
} from "./types";
|
||||||
|
import type { HomeAssistant } from "../../types";
|
||||||
|
|
||||||
|
@customElement("ha-form-grid")
|
||||||
|
export class HaFormGrid extends LitElement implements HaFormElement {
|
||||||
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
|
@property({ attribute: false }) public data!: HaFormDataContainer;
|
||||||
|
|
||||||
|
@property({ attribute: false }) public schema!: HaFormGridSchema;
|
||||||
|
|
||||||
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
|
@property() public computeLabel?: (
|
||||||
|
schema: HaFormSchema,
|
||||||
|
data?: HaFormDataContainer
|
||||||
|
) => string;
|
||||||
|
|
||||||
|
@property() public computeHelper?: (schema: HaFormSchema) => string;
|
||||||
|
|
||||||
|
protected firstUpdated() {
|
||||||
|
this.setAttribute("own-margin", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected render(): TemplateResult {
|
||||||
|
return html`
|
||||||
|
${this.schema.schema.map(
|
||||||
|
(item) =>
|
||||||
|
html`
|
||||||
|
<ha-form
|
||||||
|
.hass=${this.hass}
|
||||||
|
.data=${this.data}
|
||||||
|
.schema=${[item]}
|
||||||
|
.disabled=${this.disabled}
|
||||||
|
.computeLabel=${this.computeLabel}
|
||||||
|
.computeHelper=${this.computeHelper}
|
||||||
|
></ha-form>
|
||||||
|
`
|
||||||
|
)}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
static get styles(): CSSResultGroup {
|
||||||
|
return css`
|
||||||
|
:host {
|
||||||
|
display: grid !important;
|
||||||
|
grid-template-columns: repeat(
|
||||||
|
var(--form-grid-column-count, auto-fit),
|
||||||
|
minmax(var(--form-grid-min-width, 200px), 1fr)
|
||||||
|
);
|
||||||
|
grid-gap: 8px;
|
||||||
|
}
|
||||||
|
:host > ha-form {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-form-grid": HaFormGrid;
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,18 @@
|
|||||||
import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
|
import {
|
||||||
|
css,
|
||||||
|
CSSResultGroup,
|
||||||
|
html,
|
||||||
|
LitElement,
|
||||||
|
PropertyValues,
|
||||||
|
TemplateResult,
|
||||||
|
} from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
|
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import "../ha-alert";
|
import "../ha-alert";
|
||||||
import "./ha-form-boolean";
|
import "./ha-form-boolean";
|
||||||
import "./ha-form-constant";
|
import "./ha-form-constant";
|
||||||
|
import "./ha-form-grid";
|
||||||
import "./ha-form-float";
|
import "./ha-form-float";
|
||||||
import "./ha-form-integer";
|
import "./ha-form-integer";
|
||||||
import "./ha-form-multi_select";
|
import "./ha-form-multi_select";
|
||||||
@ -14,17 +22,18 @@ import "./ha-form-string";
|
|||||||
import { HaFormElement, HaFormDataContainer, HaFormSchema } from "./types";
|
import { HaFormElement, HaFormDataContainer, HaFormSchema } from "./types";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
|
|
||||||
const getValue = (obj, item) => (obj ? obj[item.name] : null);
|
const getValue = (obj, item) =>
|
||||||
|
obj ? (!item.name ? obj : obj[item.name]) : null;
|
||||||
|
|
||||||
let selectorImported = false;
|
let selectorImported = false;
|
||||||
|
|
||||||
@customElement("ha-form")
|
@customElement("ha-form")
|
||||||
export class HaForm extends LitElement implements HaFormElement {
|
export class HaForm extends LitElement implements HaFormElement {
|
||||||
@property() public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public data!: HaFormDataContainer;
|
@property({ attribute: false }) public data!: HaFormDataContainer;
|
||||||
|
|
||||||
@property() public schema!: HaFormSchema[];
|
@property({ attribute: false }) public schema!: HaFormSchema[];
|
||||||
|
|
||||||
@property() public error?: Record<string, string>;
|
@property() public error?: Record<string, string>;
|
||||||
|
|
||||||
@ -64,7 +73,7 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<div class="root">
|
<div class="root">
|
||||||
${this.error && this.error.base
|
${this.error && this.error.base
|
||||||
@ -101,6 +110,9 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
data: getValue(this.data, item),
|
data: getValue(this.data, item),
|
||||||
label: this._computeLabel(item, this.data),
|
label: this._computeLabel(item, this.data),
|
||||||
disabled: this.disabled,
|
disabled: this.disabled,
|
||||||
|
hass: this.hass,
|
||||||
|
computeLabel: this.computeLabel,
|
||||||
|
computeHelper: this.computeHelper,
|
||||||
})}
|
})}
|
||||||
`;
|
`;
|
||||||
})}
|
})}
|
||||||
@ -115,8 +127,12 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
const schema = (ev.target as HaFormElement).schema as HaFormSchema;
|
const schema = (ev.target as HaFormElement).schema as HaFormSchema;
|
||||||
|
|
||||||
|
const newValue = !schema.name
|
||||||
|
? ev.detail.value
|
||||||
|
: { [schema.name]: ev.detail.value };
|
||||||
|
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value: { ...this.data, [schema.name]: ev.detail.value },
|
value: { ...this.data, ...newValue },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return root;
|
return root;
|
||||||
|
@ -11,7 +11,8 @@ export type HaFormSchema =
|
|||||||
| HaFormSelectSchema
|
| HaFormSelectSchema
|
||||||
| HaFormMultiSelectSchema
|
| HaFormMultiSelectSchema
|
||||||
| HaFormTimeSchema
|
| HaFormTimeSchema
|
||||||
| HaFormSelector;
|
| HaFormSelector
|
||||||
|
| HaFormGridSchema;
|
||||||
|
|
||||||
export interface HaFormBaseSchema {
|
export interface HaFormBaseSchema {
|
||||||
name: string;
|
name: string;
|
||||||
@ -25,6 +26,12 @@ export interface HaFormBaseSchema {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface HaFormGridSchema extends HaFormBaseSchema {
|
||||||
|
type: "grid";
|
||||||
|
name: "";
|
||||||
|
schema: HaFormSchema[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface HaFormSelector extends HaFormBaseSchema {
|
export interface HaFormSelector extends HaFormBaseSchema {
|
||||||
type?: never;
|
type?: never;
|
||||||
selector: Selector;
|
selector: Selector;
|
||||||
|
@ -35,9 +35,12 @@ export class HaBooleanSelector extends LitElement {
|
|||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return css`
|
return css`
|
||||||
|
:host {
|
||||||
|
height: 56px;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
ha-formfield {
|
ha-formfield {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin: 16px 0;
|
|
||||||
--mdc-typography-body2-font-size: 1em;
|
--mdc-typography-body2-font-size: 1em;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -22,6 +22,8 @@ export class HaIconSelector extends LitElement {
|
|||||||
<ha-icon-picker
|
<ha-icon-picker
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.value=${this.value}
|
.value=${this.value}
|
||||||
|
.fallbackPath=${this.selector.icon.fallbackPath}
|
||||||
|
.placeholder=${this.selector.icon.placeholder}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._valueChanged}
|
||||||
></ha-icon-picker>
|
></ha-icon-picker>
|
||||||
`;
|
`;
|
||||||
|
34
src/components/ha-selector/ha-selector-theme.ts
Normal file
34
src/components/ha-selector/ha-selector-theme.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import "../../panels/lovelace/components/hui-theme-select-editor";
|
||||||
|
import { html, LitElement } from "lit";
|
||||||
|
import { customElement, property } from "lit/decorators";
|
||||||
|
import type { HomeAssistant } from "../../types";
|
||||||
|
import type { ThemeSelector } from "../../data/selector";
|
||||||
|
|
||||||
|
@customElement("ha-selector-theme")
|
||||||
|
export class HaThemeSelector extends LitElement {
|
||||||
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
|
@property({ attribute: false }) public selector!: ThemeSelector;
|
||||||
|
|
||||||
|
@property() public value?: string;
|
||||||
|
|
||||||
|
@property() public label?: string;
|
||||||
|
|
||||||
|
@property({ type: Boolean, reflect: true }) public disabled = false;
|
||||||
|
|
||||||
|
protected render() {
|
||||||
|
return html`
|
||||||
|
<hui-theme-select-editor
|
||||||
|
.hass=${this.hass}
|
||||||
|
.value=${this.value}
|
||||||
|
.label=${this.label}
|
||||||
|
></hui-theme-select-editor>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-selector-theme": HaThemeSelector;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import { html, LitElement } from "lit";
|
import { html, LitElement } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
|
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
|
||||||
import { Selector } from "../../data/selector";
|
import type { Selector } from "../../data/selector";
|
||||||
import { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
import "./ha-selector-action";
|
import "./ha-selector-action";
|
||||||
import "./ha-selector-addon";
|
import "./ha-selector-addon";
|
||||||
import "./ha-selector-area";
|
import "./ha-selector-area";
|
||||||
@ -19,6 +19,7 @@ import "./ha-selector-text";
|
|||||||
import "./ha-selector-time";
|
import "./ha-selector-time";
|
||||||
import "./ha-selector-icon";
|
import "./ha-selector-icon";
|
||||||
import "./ha-selector-media";
|
import "./ha-selector-media";
|
||||||
|
import "./ha-selector-theme";
|
||||||
|
|
||||||
@customElement("ha-selector")
|
@customElement("ha-selector")
|
||||||
export class HaSelector extends LitElement {
|
export class HaSelector extends LitElement {
|
||||||
|
@ -14,7 +14,8 @@ export type Selector =
|
|||||||
| ObjectSelector
|
| ObjectSelector
|
||||||
| SelectSelector
|
| SelectSelector
|
||||||
| IconSelector
|
| IconSelector
|
||||||
| MediaSelector;
|
| MediaSelector
|
||||||
|
| ThemeSelector;
|
||||||
|
|
||||||
export interface EntitySelector {
|
export interface EntitySelector {
|
||||||
entity: {
|
entity: {
|
||||||
@ -147,8 +148,15 @@ export interface SelectSelector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface IconSelector {
|
export interface IconSelector {
|
||||||
|
icon: {
|
||||||
|
placeholder?: string;
|
||||||
|
fallbackPath?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ThemeSelector {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
icon: {};
|
theme: {};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MediaSelector {
|
export interface MediaSelector {
|
||||||
|
@ -39,7 +39,7 @@ export class HuiThemeSelectEditor extends LitElement {
|
|||||||
.sort()
|
.sort()
|
||||||
.map(
|
.map(
|
||||||
(theme) =>
|
(theme) =>
|
||||||
html` <mwc-list-item .value=${theme}>${theme}</mwc-list-item> `
|
html`<mwc-list-item .value=${theme}>${theme}</mwc-list-item>`
|
||||||
)}
|
)}
|
||||||
</mwc-select>
|
</mwc-select>
|
||||||
`;
|
`;
|
||||||
@ -57,7 +57,7 @@ export class HuiThemeSelectEditor extends LitElement {
|
|||||||
if (!this.hass || ev.target.value === "") {
|
if (!this.hass || ev.target.value === "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.value = ev.target.value === "remove" ? "" : ev.target.selected;
|
this.value = ev.target.value === "remove" ? "" : ev.target.value;
|
||||||
fireEvent(this, "value-changed", { value: this.value });
|
fireEvent(this, "value-changed", { value: this.value });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,18 @@
|
|||||||
import "@polymer/paper-input/paper-input";
|
import "../../../../components/ha-form/ha-form";
|
||||||
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
import { html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { assert, assign, boolean, object, optional, string } from "superstruct";
|
import { assert, assign, boolean, object, optional, string } from "superstruct";
|
||||||
|
import type { HassEntity } from "home-assistant-js-websocket/dist/types";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { computeDomain } from "../../../../common/entity/compute_domain";
|
import { computeDomain } from "../../../../common/entity/compute_domain";
|
||||||
import { domainIcon } from "../../../../common/entity/domain_icon";
|
import { domainIcon } from "../../../../common/entity/domain_icon";
|
||||||
import "../../../../components/entity/ha-entity-attribute-picker";
|
import type { HaFormSchema } from "../../../../components/ha-form/types";
|
||||||
import "../../../../components/ha-icon-picker";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import { HomeAssistant } from "../../../../types";
|
import type { EntityCardConfig } from "../../cards/types";
|
||||||
import { EntityCardConfig } from "../../cards/types";
|
|
||||||
import "../../components/hui-action-editor";
|
|
||||||
import "../../components/hui-entity-editor";
|
|
||||||
import "../../components/hui-theme-select-editor";
|
|
||||||
import { headerFooterConfigStructs } from "../../header-footer/structs";
|
import { headerFooterConfigStructs } from "../../header-footer/structs";
|
||||||
import { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { EditorTarget, EntitiesEditorEvent } from "../types";
|
|
||||||
import { configElementStyle } from "./config-elements-style";
|
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@ -46,174 +42,82 @@ export class HuiEntityCardEditor
|
|||||||
this._config = config;
|
this._config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
get _entity(): string {
|
private _schema = memoizeOne(
|
||||||
return this._config!.entity || "";
|
(entity: string, icon: string, entityState: HassEntity): HaFormSchema[] => [
|
||||||
}
|
{ name: "entity", required: true, selector: { entity: {} } },
|
||||||
|
{
|
||||||
|
type: "grid",
|
||||||
|
name: "",
|
||||||
|
schema: [
|
||||||
|
{ name: "name", selector: { text: {} } },
|
||||||
|
{
|
||||||
|
name: "icon",
|
||||||
|
selector: {
|
||||||
|
icon: {
|
||||||
|
placeholder: icon || entityState?.attributes.icon,
|
||||||
|
fallbackPath:
|
||||||
|
!icon && !entityState?.attributes.icon && entityState
|
||||||
|
? domainIcon(computeDomain(entity), entityState)
|
||||||
|
: undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
get _name(): string {
|
{
|
||||||
return this._config!.name || "";
|
name: "attribute",
|
||||||
}
|
selector: { attribute: { entity_id: entity } },
|
||||||
|
},
|
||||||
get _icon(): string {
|
{ name: "unit", selector: { text: {} } },
|
||||||
return this._config!.icon || "";
|
{ name: "theme", selector: { theme: {} } },
|
||||||
}
|
{ name: "state_color", selector: { boolean: {} } },
|
||||||
|
],
|
||||||
get _attribute(): string {
|
},
|
||||||
return this._config!.attribute || "";
|
]
|
||||||
}
|
);
|
||||||
|
|
||||||
get _unit(): string {
|
|
||||||
return this._config!.unit || "";
|
|
||||||
}
|
|
||||||
|
|
||||||
get _state_color(): boolean {
|
|
||||||
return this._config!.state_color ?? false;
|
|
||||||
}
|
|
||||||
|
|
||||||
get _theme(): string {
|
|
||||||
return this._config!.theme || "";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
const entityState = this.hass.states[this._entity];
|
|
||||||
|
const entityState = this.hass.states[this._config.entity];
|
||||||
|
|
||||||
|
const schema = this._schema(
|
||||||
|
this._config.entity,
|
||||||
|
this._config.icon,
|
||||||
|
entityState
|
||||||
|
);
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="card-config">
|
<ha-form
|
||||||
<ha-entity-picker
|
.hass=${this.hass}
|
||||||
.label="${this.hass.localize(
|
.data=${this._config}
|
||||||
"ui.panel.lovelace.editor.card.generic.entity"
|
.schema=${schema}
|
||||||
)} (${this.hass.localize(
|
.computeLabel=${this._computeLabelCallback}
|
||||||
"ui.panel.lovelace.editor.card.config.required"
|
@value-changed=${this._valueChanged}
|
||||||
)})"
|
></ha-form>
|
||||||
.hass=${this.hass}
|
|
||||||
.value=${this._entity}
|
|
||||||
.configValue=${"entity"}
|
|
||||||
@change=${this._valueChanged}
|
|
||||||
allow-custom-entity
|
|
||||||
></ha-entity-picker>
|
|
||||||
<div class="side-by-side">
|
|
||||||
<paper-input
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.name"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._name}
|
|
||||||
.configValue=${"name"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></paper-input>
|
|
||||||
<ha-icon-picker
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.icon"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._icon}
|
|
||||||
.placeholder=${this._icon || entityState?.attributes.icon}
|
|
||||||
.fallbackPath=${!this._icon &&
|
|
||||||
!entityState?.attributes.icon &&
|
|
||||||
entityState
|
|
||||||
? domainIcon(computeDomain(entityState.entity_id), entityState)
|
|
||||||
: undefined}
|
|
||||||
.configValue=${"icon"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></ha-icon-picker>
|
|
||||||
</div>
|
|
||||||
<div class="side-by-side">
|
|
||||||
<ha-entity-attribute-picker
|
|
||||||
.hass=${this.hass}
|
|
||||||
.entityId=${this._entity}
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.attribute"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._attribute}
|
|
||||||
.configValue=${"attribute"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></ha-entity-attribute-picker>
|
|
||||||
<paper-input
|
|
||||||
.label="${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.unit"
|
|
||||||
)} (${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
|
||||||
)})"
|
|
||||||
.value=${this._unit}
|
|
||||||
.configValue=${"unit"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></paper-input>
|
|
||||||
</div>
|
|
||||||
<div class="side-by-side">
|
|
||||||
<hui-theme-select-editor
|
|
||||||
.hass=${this.hass}
|
|
||||||
.value=${this._theme}
|
|
||||||
.configValue=${"theme"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
>
|
|
||||||
</hui-theme-select-editor>
|
|
||||||
<ha-formfield
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.generic.state_color"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<ha-switch
|
|
||||||
.checked=${this._config!.state_color}
|
|
||||||
.configValue=${"state_color"}
|
|
||||||
@change=${this._valueChanged}
|
|
||||||
>
|
|
||||||
</ha-switch>
|
|
||||||
</ha-formfield>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: EntitiesEditorEvent): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
if (!this._config || !this.hass) {
|
const config = ev.detail.value;
|
||||||
return;
|
Object.keys(config).forEach((k) => config[k] === "" && delete config[k]);
|
||||||
}
|
fireEvent(this, "config-changed", { config });
|
||||||
const target = ev.currentTarget! as EditorTarget;
|
|
||||||
|
|
||||||
if (
|
|
||||||
this[`_${target.configValue}`] === target.value ||
|
|
||||||
this[`_${target.configValue}`] === target.config
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (target.configValue) {
|
|
||||||
if (target.value === "") {
|
|
||||||
this._config = { ...this._config };
|
|
||||||
delete this._config[target.configValue!];
|
|
||||||
} else {
|
|
||||||
let newValue: string | undefined;
|
|
||||||
if (
|
|
||||||
target.configValue === "icon_height" &&
|
|
||||||
!isNaN(Number(target.value))
|
|
||||||
) {
|
|
||||||
newValue = `${String(target.value)}px`;
|
|
||||||
}
|
|
||||||
this._config = {
|
|
||||||
...this._config,
|
|
||||||
[target.configValue!]:
|
|
||||||
target.checked !== undefined
|
|
||||||
? target.checked
|
|
||||||
: newValue !== undefined
|
|
||||||
? newValue
|
|
||||||
: target.value
|
|
||||||
? target.value
|
|
||||||
: target.config,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
private _computeLabelCallback = (schema: HaFormSchema) => {
|
||||||
return configElementStyle;
|
if (schema.name === "entity") {
|
||||||
}
|
return `${this.hass!.localize(
|
||||||
|
"ui.panel.lovelace.editor.card.generic.entity"
|
||||||
|
)} (${this.hass!.localize(
|
||||||
|
"ui.panel.lovelace.editor.card.config.required"
|
||||||
|
)})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.hass!.localize(
|
||||||
|
`ui.panel.lovelace.editor.card.generic.${schema.name}`
|
||||||
|
);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user