From 88f67230fca2c203ead0daeb1ef57be0548ccb6e Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 23 Jan 2024 12:04:23 +0100 Subject: [PATCH] Preview for time_date helper (#19476) --- src/data/time_date.ts | 21 +++++ .../previews/flow-preview-time_date.ts | 94 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/data/time_date.ts create mode 100644 src/dialogs/config-flow/previews/flow-preview-time_date.ts diff --git a/src/data/time_date.ts b/src/data/time_date.ts new file mode 100644 index 0000000000..5f572cb658 --- /dev/null +++ b/src/data/time_date.ts @@ -0,0 +1,21 @@ +import { UnsubscribeFunc } from "home-assistant-js-websocket"; +import { HomeAssistant } from "../types"; + +export interface TimeDatePreview { + state: string; + attributes: Record; +} + +export const subscribePreviewTimeDate = ( + hass: HomeAssistant, + flow_id: string, + flow_type: "config_flow" | "options_flow", + user_input: Record, + callback: (preview: TimeDatePreview) => void +): Promise => + hass.connection.subscribeMessage(callback, { + type: "time_date/start_preview", + flow_id, + flow_type, + user_input, + }); diff --git a/src/dialogs/config-flow/previews/flow-preview-time_date.ts b/src/dialogs/config-flow/previews/flow-preview-time_date.ts new file mode 100644 index 0000000000..fd4676ec43 --- /dev/null +++ b/src/dialogs/config-flow/previews/flow-preview-time_date.ts @@ -0,0 +1,94 @@ +import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket"; +import { LitElement, html } from "lit"; +import { customElement, property, state } from "lit/decorators"; +import { FlowType } from "../../../data/data_entry_flow"; +import { + TimeDatePreview, + subscribePreviewTimeDate, +} from "../../../data/time_date"; +import { HomeAssistant } from "../../../types"; +import "./entity-preview-row"; +import { debounce } from "../../../common/util/debounce"; + +@customElement("flow-preview-time_date") +class FlowPreviewTimeDate extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property() public flowType!: FlowType; + + public handler!: string; + + @property() public stepId!: string; + + @property() public flowId!: string; + + @property() public stepData!: Record; + + @state() private _preview?: HassEntity; + + private _unsub?: Promise; + + disconnectedCallback(): void { + super.disconnectedCallback(); + if (this._unsub) { + this._unsub.then((unsub) => unsub()); + this._unsub = undefined; + } + } + + willUpdate(changedProps) { + if (changedProps.has("stepData")) { + this._debouncedSubscribePreview(); + } + } + + protected render() { + return html``; + } + + private _setPreview = (preview: TimeDatePreview) => { + const now = new Date().toISOString(); + this._preview = { + entity_id: `${this.stepId}.___flow_preview___`, + last_changed: now, + last_updated: now, + context: { id: "", parent_id: null, user_id: null }, + ...preview, + }; + }; + + private _debouncedSubscribePreview = debounce(() => { + this._subscribePreview(); + }, 250); + + private async _subscribePreview() { + if (this._unsub) { + (await this._unsub)(); + this._unsub = undefined; + } + if (this.flowType === "repair_flow") { + return; + } + try { + this._unsub = subscribePreviewTimeDate( + this.hass, + this.flowId, + this.flowType, + this.stepData, + this._setPreview + ); + await this._unsub; + } catch (err) { + this._preview = undefined; + } + } +} + +declare global { + interface HTMLElementTagNameMap { + "flow-preview-time_date": FlowPreviewTimeDate; + } +}