Skip to content

Commit

Permalink
Allow attaching additional data to schedule in UI (#22798)
Browse files Browse the repository at this point in the history
* Allow attaching additional data to schedule in UI

* use expandable
  • Loading branch information
karwosts authored Nov 15, 2024
1 parent 991cf83 commit d47966c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
55 changes: 40 additions & 15 deletions src/panels/config/helpers/forms/dialog-schedule-block-info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CSSResultGroup } from "lit";
import { html, LitElement, nothing } from "lit";
import memoizeOne from "memoize-one";
import { property, state } from "lit/decorators";
import { fireEvent } from "../../../../common/dom/fire_event";
import { createCloseHeading } from "../../../../components/ha-dialog";
Expand All @@ -13,19 +14,6 @@ import type {
} from "./show-dialog-schedule-block-info";
import type { SchemaUnion } from "../../../../components/ha-form/types";

const SCHEMA = [
{
name: "from",
required: true,
selector: { time: { no_second: true } },
},
{
name: "to",
required: true,
selector: { time: { no_second: true } },
},
];

class DialogScheduleBlockInfo extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

Expand All @@ -35,10 +23,39 @@ class DialogScheduleBlockInfo extends LitElement {

@state() private _params?: ScheduleBlockInfoDialogParams;

private _expand = false;

private _schema = memoizeOne((expand: boolean) => [
{
name: "from",
required: true,
selector: { time: { no_second: true } },
},
{
name: "to",
required: true,
selector: { time: { no_second: true } },
},
{
name: "advanced_settings",
type: "expandable" as const,
flatten: true,
expanded: expand,
schema: [
{
name: "data",
required: false,
selector: { object: {} },
},
],
},
]);

public showDialog(params: ScheduleBlockInfoDialogParams): void {
this._params = params;
this._error = undefined;
this._data = params.block;
this._expand = !!params.block?.data;
}

public closeDialog(): void {
Expand Down Expand Up @@ -66,7 +83,7 @@ class DialogScheduleBlockInfo extends LitElement {
<div>
<ha-form
.hass=${this.hass}
.schema=${SCHEMA}
.schema=${this._schema(this._expand)}
.data=${this._data}
.error=${this._error}
.computeLabel=${this._computeLabelCallback}
Expand Down Expand Up @@ -110,12 +127,20 @@ class DialogScheduleBlockInfo extends LitElement {
}
}

private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
) => {
switch (schema.name) {
case "from":
return this.hass!.localize("ui.dialogs.helper_settings.schedule.start");
case "to":
return this.hass!.localize("ui.dialogs.helper_settings.schedule.end");
case "data":
return this.hass!.localize("ui.dialogs.helper_settings.schedule.data");
case "advanced_settings":
return this.hass!.localize(
"ui.dialogs.helper_settings.schedule.advanced_settings"
);
}
return "";
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fireEvent } from "../../../../common/dom/fire_event";
export interface ScheduleBlockInfo {
from: string;
to: string;
data?: Record<string, any>;
}

export interface ScheduleBlockInfoDialogParams {
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,9 @@
"confirm_delete": "Do you want to delete this item?",
"edit_schedule_block": "Edit schedule block",
"start": "Start",
"end": "End"
"end": "End",
"data": "Additional data",
"advanced_settings": "Advanced settings"
},
"template": {
"time": "[%key:ui::panel::developer-tools::tabs::templates::time%]",
Expand Down

0 comments on commit d47966c

Please sign in to comment.