Skip to content

Commit

Permalink
Make config flow previews more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Jul 12, 2024
1 parent 3f34dac commit f51f746
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 242 deletions.
21 changes: 0 additions & 21 deletions src/data/group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
HassEntityAttributeBase,
HassEntityBase,
UnsubscribeFunc,
} from "home-assistant-js-websocket";
import { computeDomain } from "../common/entity/compute_domain";
import { HomeAssistant } from "../types";

interface GroupEntityAttributes extends HassEntityAttributeBase {
entity_id: string[];
Expand All @@ -17,11 +15,6 @@ export interface GroupEntity extends HassEntityBase {
attributes: GroupEntityAttributes;
}

export interface GroupPreview {
state: string;
attributes: Record<string, any>;
}

export const computeGroupDomain = (
stateObj: GroupEntity
): string | undefined => {
Expand All @@ -31,17 +24,3 @@ export const computeGroupDomain = (
];
return uniqueDomains.length === 1 ? uniqueDomains[0] : undefined;
};

export const subscribePreviewGroup = (
hass: HomeAssistant,
flow_id: string,
flow_type: "config_flow" | "options_flow",
user_input: Record<string, any>,
callback: (preview: GroupPreview) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(callback, {
type: "group/start_preview",
flow_id,
flow_type,
user_input,
});
12 changes: 8 additions & 4 deletions src/data/threshold.ts → src/data/preview.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { HomeAssistant } from "../types";

export interface ThresholdPreview {
export interface GenericPreview {
state: string;
attributes: Record<string, any>;
}

export const subscribePreviewThreshold = (
export const subscribePreviewGeneric = (
hass: HomeAssistant,
domain: string,
flow_id: string,
flow_type: "config_flow" | "options_flow",
user_input: Record<string, any>,
callback: (preview: ThresholdPreview) => void
callback: (preview: GenericPreview) => void
): Promise<UnsubscribeFunc> =>
hass.connection.subscribeMessage(callback, {
type: "threshold/start_preview",
type: `${domain}/start_preview`,
flow_id,
flow_type,
user_input,
});

export const previewModule = (domain: string): string =>
["template"].includes(domain) ? domain : "generic";
21 changes: 0 additions & 21 deletions src/data/time_date.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ 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 {
ThresholdPreview,
subscribePreviewThreshold,
} from "../../../data/threshold";
import { GenericPreview, subscribePreviewGeneric } from "../../../data/preview";
import { HomeAssistant } from "../../../types";
import "./entity-preview-row";
import { debounce } from "../../../common/util/debounce";
import { fireEvent } from "../../../common/dom/fire_event";

@customElement("flow-preview-threshold")
class FlowPreviewThreshold extends LitElement {
@customElement("flow-preview-generic")
class FlowPreviewGeneric extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property() public flowType!: FlowType;

public handler!: string;

@property() public domain!: string;

@property() public stepId!: string;

@property() public flowId!: string;
Expand Down Expand Up @@ -55,7 +54,7 @@ class FlowPreviewThreshold extends LitElement {
></entity-preview-row>`;
}

private _setPreview = (preview: ThresholdPreview) => {
private _setPreview = (preview: GenericPreview) => {
const now = new Date().toISOString();
this._preview = {
entity_id: `${this.stepId}.___flow_preview___`,
Expand All @@ -79,14 +78,14 @@ class FlowPreviewThreshold extends LitElement {
return;
}
try {
this._unsub = subscribePreviewThreshold(
this._unsub = subscribePreviewGeneric(
this.hass,
this.domain,
this.flowId,
this.flowType,
this.stepData,
this._setPreview
);
await this._unsub;
fireEvent(this, "set-flow-errors", { errors: {} });
} catch (err: any) {
if (typeof err.message === "string") {
Expand All @@ -103,6 +102,6 @@ class FlowPreviewThreshold extends LitElement {

declare global {
interface HTMLElementTagNameMap {
"flow-preview-threshold": FlowPreviewThreshold;
"flow-preview-generic": FlowPreviewGeneric;
}
}
90 changes: 0 additions & 90 deletions src/dialogs/config-flow/previews/flow-preview-group.ts

This file was deleted.

94 changes: 0 additions & 94 deletions src/dialogs/config-flow/previews/flow-preview-time_date.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/dialogs/config-flow/step-flow-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { HomeAssistant } from "../../types";
import type { FlowConfig } from "./show-dialog-data-entry-flow";
import { configFlowContentStyles } from "./styles";
import { haStyle } from "../../resources/styles";
import { previewModule } from "../../data/preview";

@customElement("step-flow-form")
class StepFlowForm extends LitElement {
Expand Down Expand Up @@ -76,8 +77,9 @@ class StepFlowForm extends LitElement {
"ui.panel.config.integrations.config_flow.preview"
)}:
</h3>
${dynamicElement(`flow-preview-${this.step.preview}`, {
${dynamicElement(`flow-preview-${previewModule(step.preview)}`, {
hass: this.hass,
domain: step.preview,
flowType: this.flowConfig.flowType,
handler: step.handler,
stepId: step.step_id,
Expand Down Expand Up @@ -120,7 +122,7 @@ class StepFlowForm extends LitElement {
protected willUpdate(changedProps: PropertyValues): void {
super.willUpdate(changedProps);
if (changedProps.has("step") && this.step?.preview) {
import(`./previews/flow-preview-${this.step.preview}`);
import(`./previews/flow-preview-${previewModule(this.step.preview)}`);
}
}

Expand Down

0 comments on commit f51f746

Please sign in to comment.