Convert shopping list card editor to ha-form (#18376)

* Convert shopping list card editor to ha-form

* hide todo from generated dashboard
This commit is contained in:
Bram Kragten 2023-10-24 18:14:14 +02:00 committed by GitHub
parent 32edbd7b33
commit be1624f66f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 78 deletions

View File

@ -16,7 +16,7 @@ import {
mdiCarCoolantLevel,
mdiCash,
mdiChatSleep,
mdiClipboardCheck,
mdiClipboardList,
mdiClock,
mdiCloudUpload,
mdiCog,
@ -121,7 +121,7 @@ export const FIXED_DOMAIN_ICONS = {
siren: mdiBullhorn,
stt: mdiMicrophoneMessage,
text: mdiFormTextbox,
todo: mdiClipboardCheck,
todo: mdiClipboardList,
time: mdiClock,
timer: mdiTimerOutline,
tts: mdiSpeakerMessage,

View File

@ -39,6 +39,7 @@ const HIDE_DOMAIN = new Set([
"event",
"tts",
"stt",
"todo",
]);
const HIDE_PLATFORM = new Set(["mobile_app"]);

View File

@ -1,16 +1,16 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { isComponentLoaded } from "../../../../common/config/is_component_loaded";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/entity/ha-entity-picker";
import "../../../../components/ha-textfield";
import "../../../../components/ha-theme-picker";
import "../../../../components/ha-alert";
import "../../../../components/ha-form/ha-form";
import { HomeAssistant } from "../../../../types";
import { ShoppingListCardConfig } from "../../cards/types";
import { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EditorTarget, EntitiesEditorEvent } from "../types";
import { SchemaUnion } from "../../../../components/ha-form/types";
import { configElementStyle } from "./config-elements-style";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -21,6 +21,17 @@ const cardConfigStruct = assign(
})
);
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{
name: "entity",
selector: {
entity: { domain: "todo" },
},
},
{ name: "theme", selector: { theme: {} } },
] as const;
@customElement("hui-shopping-list-card-editor")
export class HuiShoppingListEditor
extends LitElement
@ -35,92 +46,56 @@ export class HuiShoppingListEditor
this._config = config;
}
get _title(): string {
return this._config!.title || "";
}
get _theme(): string {
return this._config!.theme || "";
}
protected render() {
if (!this.hass || !this._config) {
return nothing;
}
return html`
<div class="card-config">
${!isComponentLoaded(this.hass, "todo")
? html`
<div class="error">
${this.hass.localize(
"ui.panel.lovelace.editor.card.shopping-list.integration_not_loaded"
)}
</div>
`
: ""}
<ha-textfield
.label="${this.hass.localize(
"ui.panel.lovelace.editor.card.generic.title"
)} (${this.hass.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})"
.value=${this._title}
.configValue=${"title"}
@input=${this._valueChanged}
></ha-textfield>
<ha-entity-picker
.hass=${this.hass!}
.configValue=${"entity"}
.value=${this._config.entity}
.includeDomains=${["todo"]}
@value-changed=${this._valueChanged}
>
</ha-entity-picker>
<ha-theme-picker
${
!isComponentLoaded(this.hass, "todo")
? html`
<ha-alert alert-type="error">
${this.hass.localize(
"ui.panel.lovelace.editor.card.shopping-list.integration_not_loaded"
)}
</ha-alert>
`
: ""
}
<ha-form
.hass=${this.hass}
.value=${this._theme}
.configValue=${"theme"}
.label=${`${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`}
.data=${this._config}
.schema=${SCHEMA}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-theme-picker>
></ha-form>
</div>
`;
}
private _valueChanged(ev: EntitiesEditorEvent): void {
if (!this._config || !this.hass) {
return;
}
const target = ev.target! as EditorTarget;
if (this[`_${target.configValue}`] === target.value) {
return;
}
if (target.configValue) {
if (target.value === "") {
this._config = { ...this._config };
delete this._config[target.configValue!];
} else {
this._config = {
...this._config,
[target.configValue!]: target.value,
};
}
}
fireEvent(this, "config-changed", { config: this._config });
private _valueChanged(ev: CustomEvent): void {
const config = ev.detail.value;
fireEvent(this, "config-changed", { config });
}
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
switch (schema.name) {
case "theme":
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
}
};
static get styles(): CSSResultGroup {
return css`
.error {
color: var(--error-color);
}
`;
return configElementStyle;
}
}