-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust schedule helper UI with minute granularity (#21073)
* Adjust schedule helper UI with minute granularity * Update en.json * ha-button
- Loading branch information
Showing
6 changed files
with
192 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/panels/config/helpers/forms/dialog-schedule-block-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { CSSResultGroup, html, LitElement, nothing } from "lit"; | ||
import { property, state } from "lit/decorators"; | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import { createCloseHeading } from "../../../../components/ha-dialog"; | ||
import "../../../../components/ha-form/ha-form"; | ||
import "../../../../components/ha-button"; | ||
import { haStyleDialog } from "../../../../resources/styles"; | ||
import { HomeAssistant } from "../../../../types"; | ||
import { | ||
ScheduleBlockInfo, | ||
ScheduleBlockInfoDialogParams, | ||
} 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; | ||
|
||
@state() private _error?: Record<string, string>; | ||
|
||
@state() private _data?: ScheduleBlockInfo; | ||
|
||
@state() private _params?: ScheduleBlockInfoDialogParams; | ||
|
||
public showDialog(params: ScheduleBlockInfoDialogParams): void { | ||
this._params = params; | ||
this._error = undefined; | ||
this._data = params.block; | ||
} | ||
|
||
public closeDialog(): void { | ||
this._params = undefined; | ||
this._data = undefined; | ||
fireEvent(this, "dialog-closed", { dialog: this.localName }); | ||
} | ||
|
||
protected render() { | ||
if (!this._params || !this._data) { | ||
return nothing; | ||
} | ||
|
||
return html` | ||
<ha-dialog | ||
open | ||
@closed=${this.closeDialog} | ||
.heading=${createCloseHeading( | ||
this.hass, | ||
this.hass!.localize( | ||
"ui.dialogs.helper_settings.schedule.edit_schedule_block" | ||
) | ||
)} | ||
> | ||
<div> | ||
<ha-form | ||
.hass=${this.hass} | ||
.schema=${SCHEMA} | ||
.data=${this._data} | ||
.error=${this._error} | ||
.computeLabel=${this._computeLabelCallback} | ||
@value-changed=${this._valueChanged} | ||
></ha-form> | ||
</div> | ||
<ha-button | ||
slot="secondaryAction" | ||
class="warning" | ||
@click=${this._deleteBlock} | ||
> | ||
${this.hass!.localize("ui.common.delete")} | ||
</ha-button> | ||
<ha-button slot="primaryAction" @click=${this._updateBlock}> | ||
${this.hass!.localize("ui.common.save")} | ||
</ha-button> | ||
</ha-dialog> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent) { | ||
this._error = undefined; | ||
this._data = ev.detail.value; | ||
} | ||
|
||
private _updateBlock() { | ||
try { | ||
this._params!.updateBlock!(this._data!); | ||
this.closeDialog(); | ||
} catch (err: any) { | ||
this._error = { base: err ? err.message : "Unknown error" }; | ||
} | ||
} | ||
|
||
private _deleteBlock() { | ||
try { | ||
this._params!.deleteBlock!(); | ||
this.closeDialog(); | ||
} catch (err: any) { | ||
this._error = { base: err ? err.message : "Unknown error" }; | ||
} | ||
} | ||
|
||
private _computeLabelCallback = (schema: SchemaUnion<typeof 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"); | ||
} | ||
return ""; | ||
}; | ||
|
||
static get styles(): CSSResultGroup { | ||
return [haStyleDialog]; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"dialog-schedule-block-info": DialogScheduleBlockInfo; | ||
} | ||
} | ||
|
||
customElements.define("dialog-schedule-block-info", DialogScheduleBlockInfo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/panels/config/helpers/forms/show-dialog-schedule-block-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
|
||
export interface ScheduleBlockInfo { | ||
from: string; | ||
to: string; | ||
} | ||
|
||
export interface ScheduleBlockInfoDialogParams { | ||
block: ScheduleBlockInfo; | ||
updateBlock?: (update: ScheduleBlockInfo) => void; | ||
deleteBlock?: () => void; | ||
} | ||
|
||
export const loadScheduleBlockInfoDialog = () => | ||
import("./dialog-schedule-block-info"); | ||
|
||
export const showScheduleBlockInfoDialog = ( | ||
element: HTMLElement, | ||
params: ScheduleBlockInfoDialogParams | ||
): void => { | ||
fireEvent(element, "show-dialog", { | ||
dialogTag: "dialog-schedule-block-info", | ||
dialogImport: loadScheduleBlockInfoDialog, | ||
dialogParams: params, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters