Skip to content

Commit

Permalink
Convert shopping list card editor to ha-form (#18376)
Browse files Browse the repository at this point in the history
* Convert shopping list card editor to ha-form

* hide todo from generated dashboard
  • Loading branch information
bramkragten authored Oct 24, 2023
1 parent 32edbd7 commit be1624f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/common/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
mdiCarCoolantLevel,
mdiCash,
mdiChatSleep,
mdiClipboardCheck,
mdiClipboardList,
mdiClock,
mdiCloudUpload,
mdiCog,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/common/generate-lovelace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const HIDE_DOMAIN = new Set([
"event",
"tts",
"stt",
"todo",
]);

const HIDE_PLATFORM = new Set(["mobile_app"]);
Expand Down
125 changes: 50 additions & 75 deletions src/panels/lovelace/editor/config-elements/hui-shopping-list-editor.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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;
private _valueChanged(ev: CustomEvent): void {
const config = ev.detail.value;
fireEvent(this, "config-changed", { config });
}

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,
};
}
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}`
);
}
fireEvent(this, "config-changed", { config: this._config });
}
};

static get styles(): CSSResultGroup {
return css`
.error {
color: var(--error-color);
}
`;
return configElementStyle;
}
}

Expand Down

0 comments on commit be1624f

Please sign in to comment.