diff --git a/src/data/group.ts b/src/data/group.ts index 4b154672fe..fbaaab4faf 100644 --- a/src/data/group.ts +++ b/src/data/group.ts @@ -17,6 +17,11 @@ export interface GroupEntity extends HassEntityBase { attributes: GroupEntityAttributes; } +export interface GroupPreview { + state: string; + attributes: Record; +} + export const computeGroupDomain = ( stateObj: GroupEntity ): string | undefined => { @@ -27,35 +32,15 @@ export const computeGroupDomain = ( return uniqueDomains.length === 1 ? uniqueDomains[0] : undefined; }; -export const subscribePreviewGroupSensor = ( +export const subscribePreviewGroup = ( hass: HomeAssistant, flow_id: string, flow_type: "config_flow" | "options_flow", user_input: Record, - callback: (preview: { - state: string; - attributes: Record; - }) => void + callback: (preview: GroupPreview) => void ): Promise => hass.connection.subscribeMessage(callback, { - type: "group/sensor/start_preview", - flow_id, - flow_type, - user_input, - }); - -export const subscribePreviewGroupBinarySensor = ( - hass: HomeAssistant, - flow_id: string, - flow_type: "config_flow" | "options_flow", - user_input: Record, - callback: (preview: { - state: string; - attributes: Record; - }) => void -): Promise => - hass.connection.subscribeMessage(callback, { - type: "group/binary_sensor/start_preview", + type: "group/start_preview", flow_id, flow_type, user_input, diff --git a/src/dialogs/config-flow/previews/flow-preview-group_binary_sensor.ts b/src/dialogs/config-flow/previews/flow-preview-group.ts similarity index 74% rename from src/dialogs/config-flow/previews/flow-preview-group_binary_sensor.ts rename to src/dialogs/config-flow/previews/flow-preview-group.ts index 927481aa42..25f6d3cb08 100644 --- a/src/dialogs/config-flow/previews/flow-preview-group_binary_sensor.ts +++ b/src/dialogs/config-flow/previews/flow-preview-group.ts @@ -2,19 +2,20 @@ 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 { subscribePreviewGroupBinarySensor } from "../../../data/group"; +import { GroupPreview, subscribePreviewGroup } from "../../../data/group"; import { HomeAssistant } from "../../../types"; import "./entity-preview-row"; +import { debounce } from "../../../common/util/debounce"; -@customElement("flow-preview-group_binary_sensor") -class FlowPreviewGroupBinarySensor extends LitElement { +@customElement("flow-preview-group") +class FlowPreviewGroup extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property() public flowType!: FlowType; public handler!: string; - public stepId!: string; + @property() public stepId!: string; @property() public flowId!: string; @@ -34,7 +35,7 @@ class FlowPreviewGroupBinarySensor extends LitElement { willUpdate(changedProps) { if (changedProps.has("stepData")) { - this._subscribePreview(); + this._debouncedSubscribePreview(); } } @@ -45,13 +46,10 @@ class FlowPreviewGroupBinarySensor extends LitElement { >`; } - private _setPreview = (preview: { - state: string; - attributes: Record; - }) => { + private _setPreview = (preview: GroupPreview) => { const now = new Date().toISOString(); this._preview = { - entity_id: "binary_sensor.flow_preview", + entity_id: `${this.stepId}.flow_preview`, last_changed: now, last_updated: now, context: { id: "", parent_id: null, user_id: null }, @@ -59,6 +57,10 @@ class FlowPreviewGroupBinarySensor extends LitElement { }; }; + private _debouncedSubscribePreview = debounce(() => { + this._subscribePreview(); + }, 250); + private async _subscribePreview() { if (this._unsub) { (await this._unsub)(); @@ -68,7 +70,7 @@ class FlowPreviewGroupBinarySensor extends LitElement { return; } try { - this._unsub = subscribePreviewGroupBinarySensor( + this._unsub = subscribePreviewGroup( this.hass, this.flowId, this.flowType, @@ -83,6 +85,6 @@ class FlowPreviewGroupBinarySensor extends LitElement { declare global { interface HTMLElementTagNameMap { - "flow-preview-group_binary_sensor": FlowPreviewGroupBinarySensor; + "flow-preview-group": FlowPreviewGroup; } } diff --git a/src/dialogs/config-flow/previews/flow-preview-group_sensor.ts b/src/dialogs/config-flow/previews/flow-preview-group_sensor.ts deleted file mode 100644 index 4da52df762..0000000000 --- a/src/dialogs/config-flow/previews/flow-preview-group_sensor.ts +++ /dev/null @@ -1,92 +0,0 @@ -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 { subscribePreviewGroupSensor } from "../../../data/group"; -import { HomeAssistant } from "../../../types"; -import "./entity-preview-row"; - -@customElement("flow-preview-group_sensor") -class FlowPreviewGroupSensor extends LitElement { - @property({ attribute: false }) public hass!: HomeAssistant; - - @property() public flowType!: FlowType; - - public handler!: string; - - 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._subscribePreview(); - } - } - - protected render() { - return html``; - } - - private _setPreview = (preview: { - state: string; - attributes: Record; - }) => { - const now = new Date().toISOString(); - this._preview = { - entity_id: "sensor.flow_preview", - last_changed: now, - last_updated: now, - context: { id: "", parent_id: null, user_id: null }, - ...preview, - }; - }; - - private async _subscribePreview() { - if (this._unsub) { - (await this._unsub)(); - this._unsub = undefined; - } - if (this.flowType === "repair_flow") { - return; - } - if (!this.stepData.type) { - this._preview = undefined; - return; - } - try { - this._unsub = subscribePreviewGroupSensor( - this.hass, - this.flowId, - this.flowType, - this.stepData, - this._setPreview - ); - } catch (err) { - this._preview = undefined; - } - } -} - -declare global { - interface HTMLElementTagNameMap { - "flow-preview-group_sensor": FlowPreviewGroupSensor; - } -}