Skip to content

Commit

Permalink
Subscribe to config entries in helper config (home-assistant#17835)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Sep 6, 2023
1 parent 3c48157 commit 14e6f5e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
11 changes: 6 additions & 5 deletions src/components/ha-conversation-agent-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ export class HaConversationAgentPicker extends LitElement {
if (!this._configEntry) {
return;
}
showOptionsFlowDialog(
this,
this._configEntry,
await fetchIntegrationManifest(this.hass, this._configEntry.domain)
);
showOptionsFlowDialog(this, this._configEntry, {
manifest: await fetchIntegrationManifest(
this.hass,
this._configEntry.domain
),
});
}

static get styles(): CSSResultGroup {
Expand Down
7 changes: 4 additions & 3 deletions src/dialogs/config-flow/show-dialog-options-flow.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { html } from "lit";
import { ConfigEntry } from "../../data/config_entries";
import { domainToName, IntegrationManifest } from "../../data/integration";
import { domainToName } from "../../data/integration";
import {
createOptionsFlow,
deleteOptionsFlow,
fetchOptionsFlow,
handleOptionsFlowStep,
} from "../../data/options_flow";
import {
DataEntryFlowDialogParams,
loadDataEntryFlowDialog,
showFlowDialog,
} from "./show-dialog-data-entry-flow";
Expand All @@ -17,14 +18,14 @@ export const loadOptionsFlowDialog = loadDataEntryFlowDialog;
export const showOptionsFlowDialog = (
element: HTMLElement,
configEntry: ConfigEntry,
manifest?: IntegrationManifest | null
dialogParams?: Omit<DataEntryFlowDialogParams, "flowConfig">
): void =>
showFlowDialog(
element,
{
startFlowHandler: configEntry.entry_id,
domain: configEntry.domain,
manifest,
...dialogParams,
},
{
flowType: "options_flow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ export class EntityRegistrySettingsEditor extends LitElement {
}

private async _showOptionsFlow() {
showOptionsFlowDialog(this, this.helperConfigEntry!, null);
showOptionsFlowDialog(this, this.helperConfigEntry!);
}

private _switchAsDomainsSorted = memoizeOne(
Expand Down
61 changes: 33 additions & 28 deletions src/panels/config/helpers/ha-config-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
import { mdiAlertCircle, mdiPencilOff, mdiPlus } from "@mdi/js";
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
import { HassEntity } from "home-assistant-js-websocket";
import { LitElement, PropertyValues, TemplateResult, html } from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
Expand All @@ -16,7 +16,10 @@ import "../../../components/ha-fab";
import "../../../components/ha-icon";
import "../../../components/ha-state-icon";
import "../../../components/ha-svg-icon";
import { ConfigEntry, getConfigEntries } from "../../../data/config_entries";
import {
ConfigEntry,
subscribeConfigEntries,
} from "../../../data/config_entries";
import { getConfigFlowHandlers } from "../../../data/config_flow";
import {
EntityRegistryEntry,
Expand Down Expand Up @@ -76,6 +79,33 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {

@state() private _configEntries?: Record<string, ConfigEntry>;

public hassSubscribe() {
return [
subscribeConfigEntries(
this.hass,
async (messages) => {
const newEntries = this._configEntries
? { ...this._configEntries }
: {};
messages.forEach((message) => {
if (message.type === null || message.type === "added") {
newEntries[message.entry.entry_id] = message.entry;
} else if (message.type === "removed") {
delete newEntries[message.entry.entry_id];
} else if (message.type === "updated") {
newEntries[message.entry.entry_id] = message.entry;
}
});
this._configEntries = newEntries;
},
{ type: ["helper"] }
),
subscribeEntityRegistry(this.hass.connection!, (entries) => {
this._entityEntries = groupByOne(entries, (entry) => entry.entity_id);
}),
];
}

private _columns = memoizeOne(
(narrow: boolean, localize: LocalizeFunc): DataTableColumnContainer => {
const columns: DataTableColumnContainer = {
Expand Down Expand Up @@ -256,7 +286,6 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {

protected firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
this._getConfigEntries();
if (this.route.path === "/add") {
this._handleAdd();
}
Expand Down Expand Up @@ -313,9 +342,6 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
return;
}
showConfigFlowDialog(this, {
dialogClosedCallback: () => {
this._getConfigEntries();
},
startFlowHandler: domain,
showAdvanced: this.hass.userData?.showAdvanced,
});
Expand Down Expand Up @@ -366,21 +392,6 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
}
}

public hassSubscribe(): UnsubscribeFunc[] {
return [
subscribeEntityRegistry(this.hass.connection!, (entries) => {
this._entityEntries = groupByOne(entries, (entry) => entry.entity_id);
}),
];
}

private async _getConfigEntries() {
this._configEntries = groupByOne(
await getConfigEntries(this.hass, { type: ["helper"] }),
(entry) => entry.entry_id
);
}

private async _openEditDialog(ev: CustomEvent): Promise<void> {
const id = (ev.detail as RowClickedEvent).id;
if (id.includes(".")) {
Expand All @@ -391,12 +402,6 @@ export class HaConfigHelpers extends SubscribeMixin(LitElement) {
}

private _createHelpler() {
showHelperDetailDialog(this, {
dialogClosedCallback: (params) => {
if (params.flowFinished) {
this._getConfigEntries();
}
},
});
showHelperDetailDialog(this, {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ class HaConfigIntegrationPage extends SubscribeMixin(LitElement) {
showOptionsFlowDialog(
this,
ev.target.closest(".config_entry").configEntry,
this._manifest
{ manifest: this._manifest }
);
}

Expand Down

0 comments on commit 14e6f5e

Please sign in to comment.