mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 08:16:36 +00:00
Add helpers to list when searching in add integration (#12159)
This commit is contained in:
parent
062f21aa91
commit
86afd883a5
@ -35,6 +35,7 @@ import { documentationUrl } from "../../util/documentation-url";
|
|||||||
import { showAlertDialog } from "../generic/show-dialog-box";
|
import { showAlertDialog } from "../generic/show-dialog-box";
|
||||||
import {
|
import {
|
||||||
DataEntryFlowDialogParams,
|
DataEntryFlowDialogParams,
|
||||||
|
FlowHandlers,
|
||||||
LoadingReason,
|
LoadingReason,
|
||||||
} from "./show-dialog-data-entry-flow";
|
} from "./show-dialog-data-entry-flow";
|
||||||
import "./step-flow-abort";
|
import "./step-flow-abort";
|
||||||
@ -85,7 +86,7 @@ class DataEntryFlowDialog extends LitElement {
|
|||||||
|
|
||||||
@state() private _areas?: AreaRegistryEntry[];
|
@state() private _areas?: AreaRegistryEntry[];
|
||||||
|
|
||||||
@state() private _handlers?: string[];
|
@state() private _handlers?: FlowHandlers;
|
||||||
|
|
||||||
@state() private _handler?: string;
|
@state() private _handler?: string;
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { html } from "lit";
|
import { html } from "lit";
|
||||||
import { caseInsensitiveStringCompare } from "../../common/string/compare";
|
|
||||||
import {
|
import {
|
||||||
createConfigFlow,
|
createConfigFlow,
|
||||||
deleteConfigFlow,
|
deleteConfigFlow,
|
||||||
@ -23,17 +22,12 @@ export const showConfigFlowDialog = (
|
|||||||
showFlowDialog(element, dialogParams, {
|
showFlowDialog(element, dialogParams, {
|
||||||
loadDevicesAndAreas: true,
|
loadDevicesAndAreas: true,
|
||||||
getFlowHandlers: async (hass) => {
|
getFlowHandlers: async (hass) => {
|
||||||
const [handlers] = await Promise.all([
|
const [integrations, helpers] = await Promise.all([
|
||||||
getConfigFlowHandlers(hass, "integration"),
|
getConfigFlowHandlers(hass, "integration"),
|
||||||
|
getConfigFlowHandlers(hass, "helper"),
|
||||||
hass.loadBackendTranslation("title", undefined, true),
|
hass.loadBackendTranslation("title", undefined, true),
|
||||||
]);
|
]);
|
||||||
|
return { integrations, helpers };
|
||||||
return handlers.sort((handlerA, handlerB) =>
|
|
||||||
caseInsensitiveStringCompare(
|
|
||||||
domainToName(hass.localize, handlerA),
|
|
||||||
domainToName(hass.localize, handlerB)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
createFlow: async (hass, handler) => {
|
createFlow: async (hass, handler) => {
|
||||||
const [step] = await Promise.all([
|
const [step] = await Promise.all([
|
||||||
|
@ -12,10 +12,14 @@ import {
|
|||||||
} from "../../data/data_entry_flow";
|
} from "../../data/data_entry_flow";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
|
|
||||||
|
export interface FlowHandlers {
|
||||||
|
integrations: string[];
|
||||||
|
helpers: string[];
|
||||||
|
}
|
||||||
export interface FlowConfig {
|
export interface FlowConfig {
|
||||||
loadDevicesAndAreas: boolean;
|
loadDevicesAndAreas: boolean;
|
||||||
|
|
||||||
getFlowHandlers?: (hass: HomeAssistant) => Promise<string[]>;
|
getFlowHandlers?: (hass: HomeAssistant) => Promise<FlowHandlers>;
|
||||||
|
|
||||||
createFlow(hass: HomeAssistant, handler: string): Promise<DataEntryFlowStep>;
|
createFlow(hass: HomeAssistant, handler: string): Promise<DataEntryFlowStep>;
|
||||||
|
|
||||||
|
@ -26,11 +26,13 @@ import { HomeAssistant } from "../../types";
|
|||||||
import { brandsUrl } from "../../util/brands-url";
|
import { brandsUrl } from "../../util/brands-url";
|
||||||
import { documentationUrl } from "../../util/documentation-url";
|
import { documentationUrl } from "../../util/documentation-url";
|
||||||
import { configFlowContentStyles } from "./styles";
|
import { configFlowContentStyles } from "./styles";
|
||||||
|
import { FlowHandlers } from "./show-dialog-data-entry-flow";
|
||||||
|
|
||||||
interface HandlerObj {
|
interface HandlerObj {
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
is_add?: boolean;
|
is_add?: boolean;
|
||||||
|
is_helper?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
@ -46,7 +48,7 @@ declare global {
|
|||||||
class StepFlowPickHandler extends LitElement {
|
class StepFlowPickHandler extends LitElement {
|
||||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property({ attribute: false }) public handlers!: string[];
|
@property({ attribute: false }) public handlers!: FlowHandlers;
|
||||||
|
|
||||||
@property() public initialFilter?: string;
|
@property() public initialFilter?: string;
|
||||||
|
|
||||||
@ -57,8 +59,12 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
private _height?: number;
|
private _height?: number;
|
||||||
|
|
||||||
private _filterHandlers = memoizeOne(
|
private _filterHandlers = memoizeOne(
|
||||||
(h: string[], filter?: string, _localize?: LocalizeFunc) => {
|
(
|
||||||
const handlers: HandlerObj[] = h.map((handler) => ({
|
h: FlowHandlers,
|
||||||
|
filter?: string,
|
||||||
|
_localize?: LocalizeFunc
|
||||||
|
): [HandlerObj[], HandlerObj[]] => {
|
||||||
|
const integrations: HandlerObj[] = h.integrations.map((handler) => ({
|
||||||
name: domainToName(this.hass.localize, handler),
|
name: domainToName(this.hass.localize, handler),
|
||||||
slug: handler,
|
slug: handler,
|
||||||
}));
|
}));
|
||||||
@ -70,17 +76,31 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
minMatchCharLength: 2,
|
minMatchCharLength: 2,
|
||||||
threshold: 0.2,
|
threshold: 0.2,
|
||||||
};
|
};
|
||||||
const fuse = new Fuse(handlers, options);
|
const helpers: HandlerObj[] = h.helpers.map((handler) => ({
|
||||||
return fuse.search(filter).map((result) => result.item);
|
name: domainToName(this.hass.localize, handler),
|
||||||
|
slug: handler,
|
||||||
|
is_helper: true,
|
||||||
|
}));
|
||||||
|
return [
|
||||||
|
new Fuse(integrations, options)
|
||||||
|
.search(filter)
|
||||||
|
.map((result) => result.item),
|
||||||
|
new Fuse(helpers, options)
|
||||||
|
.search(filter)
|
||||||
|
.map((result) => result.item),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
return handlers.sort((a, b) =>
|
return [
|
||||||
caseInsensitiveStringCompare(a.name, b.name)
|
integrations.sort((a, b) =>
|
||||||
);
|
caseInsensitiveStringCompare(a.name, b.name)
|
||||||
|
),
|
||||||
|
[],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
const handlers = this._getHandlers();
|
const [integrations, helpers] = this._getHandlers();
|
||||||
|
|
||||||
const addDeviceRows: HandlerObj[] = ["zha", "zwave_js"]
|
const addDeviceRows: HandlerObj[] = ["zha", "zwave_js"]
|
||||||
.filter((domain) => isComponentLoaded(this.hass, domain))
|
.filter((domain) => isComponentLoaded(this.hass, domain))
|
||||||
@ -115,8 +135,8 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
<li divider padded class="divider" role="separator"></li>
|
<li divider padded class="divider" role="separator"></li>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
${handlers.length
|
${integrations.length
|
||||||
? handlers.map((handler) => this._renderRow(handler))
|
? integrations.map((handler) => this._renderRow(handler))
|
||||||
: html`
|
: html`
|
||||||
<p>
|
<p>
|
||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
@ -139,6 +159,12 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
>.
|
>.
|
||||||
</p>
|
</p>
|
||||||
`}
|
`}
|
||||||
|
${helpers.length
|
||||||
|
? html`
|
||||||
|
<li divider padded class="divider" role="separator"></li>
|
||||||
|
${helpers.map((handler) => this._renderRow(handler))}
|
||||||
|
`
|
||||||
|
: ""}
|
||||||
</mwc-list>
|
</mwc-list>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
@ -162,7 +188,7 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
})}
|
})}
|
||||||
referrerpolicy="no-referrer"
|
referrerpolicy="no-referrer"
|
||||||
/>
|
/>
|
||||||
<span>${handler.name}</span>
|
<span>${handler.name} ${handler.is_helper ? " (helper)" : ""}</span>
|
||||||
${handler.is_add ? "" : html`<ha-icon-next slot="meta"></ha-icon-next>`}
|
${handler.is_add ? "" : html`<ha-icon-next slot="meta"></ha-icon-next>`}
|
||||||
</mwc-list-item>
|
</mwc-list-item>
|
||||||
`;
|
`;
|
||||||
@ -236,6 +262,13 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (handler.is_helper) {
|
||||||
|
navigate(`/config/helpers/add?domain=${handler.slug}`);
|
||||||
|
// This closes dialog.
|
||||||
|
fireEvent(this, "flow-update");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
fireEvent(this, "handler-picked", {
|
fireEvent(this, "handler-picked", {
|
||||||
handler: handler.slug,
|
handler: handler.slug,
|
||||||
});
|
});
|
||||||
@ -250,7 +283,7 @@ class StepFlowPickHandler extends LitElement {
|
|||||||
|
|
||||||
if (handlers.length > 0) {
|
if (handlers.length > 0) {
|
||||||
fireEvent(this, "handler-picked", {
|
fireEvent(this, "handler-picked", {
|
||||||
handler: handlers[0].slug,
|
handler: handlers[0][0].slug,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user