From 968eae7727bd6fab2517a1247442a0bf998590bc Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 9 May 2019 15:36:05 -0700 Subject: [PATCH] Add external step (#3183) * Add external step * Automatically open external step --- src/data/config_entries.ts | 19 ++++ src/dialogs/config-flow/dialog-config-flow.ts | 10 +- src/dialogs/config-flow/step-flow-external.ts | 106 ++++++++++++++++++ src/translations/en.json | 6 + 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/dialogs/config-flow/step-flow-external.ts diff --git a/src/data/config_entries.ts b/src/data/config_entries.ts index f54a84d6da..e0b773df70 100644 --- a/src/data/config_entries.ts +++ b/src/data/config_entries.ts @@ -3,6 +3,15 @@ import { createCollection } from "home-assistant-js-websocket"; import { debounce } from "../common/util/debounce"; import { LocalizeFunc } from "../common/translations/localize"; +export interface DataEntryFlowProgressedEvent { + type: "data_entry_flow_progressed"; + data: { + handler: string; + flow_id: string; + refresh: boolean; + }; +} + export interface ConfigEntry { entry_id: string; domain: string; @@ -38,6 +47,15 @@ export interface ConfigFlowStepForm { description_placeholders: { [key: string]: string }; } +export interface ConfigFlowStepExternal { + type: "external"; + flow_id: string; + handler: string; + step_id: string; + url: string; + description_placeholders: { [key: string]: string }; +} + export interface ConfigFlowStepCreateEntry { type: "create_entry"; version: number; @@ -60,6 +78,7 @@ export interface ConfigFlowStepAbort { export type ConfigFlowStep = | ConfigFlowStepForm + | ConfigFlowStepExternal | ConfigFlowStepCreateEntry | ConfigFlowStepAbort; diff --git a/src/dialogs/config-flow/dialog-config-flow.ts b/src/dialogs/config-flow/dialog-config-flow.ts index 35930183e1..dc49c96c51 100644 --- a/src/dialogs/config-flow/dialog-config-flow.ts +++ b/src/dialogs/config-flow/dialog-config-flow.ts @@ -34,6 +34,7 @@ import { HaConfigFlowParams } from "./show-dialog-config-flow"; import "./step-flow-pick-handler"; import "./step-flow-loading"; import "./step-flow-form"; +import "./step-flow-external"; import "./step-flow-abort"; import "./step-flow-create-entry"; import { @@ -155,6 +156,13 @@ class ConfigFlowDialog extends LitElement { .hass=${this.hass} > ` + : this._step.type === "external" + ? html` + + ` : this._step.type === "abort" ? html` + ${localize( + `component.${step.handler}.config.step.${step.step_id}.title` + )} + +
+

+ ${localize( + "ui.panel.config.integrations.config_flow.external_step.description" + )} +

+ ${description + ? html` + + ` + : ""} + +
+ `; + } + + protected firstUpdated(changedProps) { + super.firstUpdated(changedProps); + this.hass.connection.subscribeEvents( + async (ev) => { + if (ev.data.flow_id !== this.step.flow_id) { + return; + } + + const step = await fetchConfigFlow(this.hass, this.step.flow_id); + fireEvent(this, "flow-update", { step }); + }, + "data_entry_flow_progressed" + ); + window.open(this.step.url); + } + + static get styles(): CSSResultArray { + return [ + configFlowContentStyles, + css` + .open-button { + text-align: center; + padding: 24px 0; + } + .open-button a { + text-decoration: none; + } + `, + ]; + } +} + +declare global { + interface HTMLElementTagNameMap { + "step-flow-external": StepFlowExternal; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index a232b91784..ffb9016cd7 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -850,6 +850,12 @@ "device_unavailable": "device unavailable", "entity_unavailable": "entity unavailable", "no_area": "No Area" + }, + "config_flow": { + "external_step": { + "description": "This step requires you to visit an external website to be completed.", + "open_site": "Open website" + } } }, "users": {