Skip to content

Commit

Permalink
adjust for multiple sub types
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten committed Dec 10, 2024
1 parent ffb006b commit 270ab1d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion demo/src/stubs/config_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const mockConfigEntries = (hass: MockHomeAssistant) => {
supports_remove_device: false,
supports_unload: true,
supports_reconfigure: true,
supports_subentries: false,
supported_subentries: [],
pref_disable_new_entities: false,
pref_disable_polling: false,
disabled_by: null,
Expand Down
2 changes: 1 addition & 1 deletion gallery/src/pages/misc/integration-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const createConfigEntry = (
supports_remove_device: false,
supports_unload: true,
supports_reconfigure: true,
supports_subentries: false,
supported_subentries: [],
num_subentries: 0,
disabled_by: null,
pref_disable_new_entities: false,
Expand Down
2 changes: 1 addition & 1 deletion src/data/config_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ConfigEntry {
supports_remove_device: boolean;
supports_unload: boolean;
supports_reconfigure: boolean;
supports_subentries: boolean;
supported_subentries: string[];
num_subentries: number;
pref_disable_new_entities: boolean;
pref_disable_polling: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/data/sub_config_flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ const HEADERS = {

export const createSubConfigFlow = (
hass: HomeAssistant,
handler: string,
entry_id?: string
configEntryId: string,
subFlowType: string
) =>
hass.callApi<DataEntryFlowStep>(
"POST",
"config/config_entries/subentries/flow",
{
handler,
handler: [configEntryId, subFlowType],
show_advanced_options: Boolean(hass.userData?.showAdvanced),
entry_id,
},
HEADERS
);
Expand Down
37 changes: 19 additions & 18 deletions src/dialogs/config-flow/show-dialog-sub-config-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export const loadSubConfigFlowDialog = loadDataEntryFlowDialog;
export const showSubConfigFlowDialog = (
element: HTMLElement,
configEntry: ConfigEntry,
flowType: string,
dialogParams: Omit<DataEntryFlowDialogParams, "flowConfig">
): void =>
showFlowDialog(element, dialogParams, {
flowType: "config_subentries_flow",
showDevices: true,
createFlow: async (hass, handler) => {
const [step] = await Promise.all([
createSubConfigFlow(hass, handler, dialogParams.entryId),
createSubConfigFlow(hass, handler, flowType),
hass.loadFragmentTranslation("config"),
hass.loadBackendTranslation("config_subentries", configEntry.domain),
hass.loadBackendTranslation("selector", configEntry.domain),
Expand All @@ -49,7 +50,7 @@ export const showSubConfigFlowDialog = (

renderAbortDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.abort.${step.reason}`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.abort.${step.reason}`,
step.description_placeholders
);

Expand All @@ -63,15 +64,15 @@ export const showSubConfigFlowDialog = (
renderShowFormStepHeader(hass, step) {
return (
hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.title`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.title`,
step.description_placeholders
) || hass.localize(`component.${configEntry.domain}.title`)
);
},

renderShowFormStepDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.description`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.description`,
step.description_placeholders
);
return description
Expand All @@ -84,30 +85,30 @@ export const showSubConfigFlowDialog = (
renderShowFormStepFieldLabel(hass, step, field, options) {
if (field.type === "expandable") {
return hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.sections.${field.name}.name`
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.sections.${field.name}.name`
);
}

const prefix = options?.path?.[0] ? `sections.${options.path[0]}.` : "";

return (
hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.${prefix}data.${field.name}`
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.${prefix}data.${field.name}`
) || field.name
);
},

renderShowFormStepFieldHelper(hass, step, field, options) {
if (field.type === "expandable") {
return hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.sections.${field.name}.description`
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.sections.${field.name}.description`
);
}

const prefix = options?.path?.[0] ? `sections.${options.path[0]}.` : "";

const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.${prefix}data_description.${field.name}`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.${prefix}data_description.${field.name}`,
step.description_placeholders
);

Expand All @@ -119,7 +120,7 @@ export const showSubConfigFlowDialog = (
renderShowFormStepFieldError(hass, step, error) {
return (
hass.localize(
`component.${step.translation_domain || step.translation_domain || configEntry.domain}.config_subentries.error.${error}`,
`component.${step.translation_domain || step.translation_domain || configEntry.domain}.config_subentries.${flowType}.error.${error}`,
step.description_placeholders
) || error
);
Expand All @@ -132,7 +133,7 @@ export const showSubConfigFlowDialog = (
renderShowFormStepSubmitButton(hass, step) {
return (
hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.submit`
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.submit`
) ||
hass.localize(
`ui.panel.config.integrations.config_flow.${
Expand All @@ -145,7 +146,7 @@ export const showSubConfigFlowDialog = (
renderExternalStepHeader(hass, step) {
return (
hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.title`
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.title`
) ||
hass.localize(
"ui.panel.config.integrations.config_flow.external_step.open_site"
Expand All @@ -155,7 +156,7 @@ export const showSubConfigFlowDialog = (

renderExternalStepDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.${step.step_id}.description`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.description`,
step.description_placeholders
);

Expand All @@ -179,7 +180,7 @@ export const showSubConfigFlowDialog = (

renderCreateEntryDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.create_entry.${
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.create_entry.${
step.description || "default"
}`,
step.description_placeholders
Expand Down Expand Up @@ -207,14 +208,14 @@ export const showSubConfigFlowDialog = (
renderShowFormProgressHeader(hass, step) {
return (
hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.title`
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.title`
) || hass.localize(`component.${configEntry.domain}.title`)
);
},

renderShowFormProgressDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.progress.${step.progress_action}`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.progress.${step.progress_action}`,
step.description_placeholders
);
return description
Expand All @@ -227,15 +228,15 @@ export const showSubConfigFlowDialog = (
renderMenuHeader(hass, step) {
return (
hass.localize(
`component.${configEntry.domain}.config_subentries.step.${step.step_id}.title`,
`component.${configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.title`,
step.description_placeholders
) || hass.localize(`component.${configEntry.domain}.title`)
);
},

renderMenuDescription(hass, step) {
const description = hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.description`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.description`,
step.description_placeholders
);
return description
Expand All @@ -247,7 +248,7 @@ export const showSubConfigFlowDialog = (

renderMenuOption(hass, step, option) {
return hass.localize(
`component.${step.translation_domain || configEntry.domain}.config_subentries.step.${step.step_id}.menu_options.${option}`,
`component.${step.translation_domain || configEntry.domain}.config_subentries.${flowType}.step.${step.step_id}.menu_options.${option}`,
step.description_placeholders
);
},
Expand Down
12 changes: 7 additions & 5 deletions src/panels/config/integrations/ha-config-integration-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,16 +933,18 @@ class HaConfigIntegrationPage extends SubscribeMixin(LitElement) {
)}
</ha-md-menu-item>
${item.supports_subentries
? html`<ha-md-menu-item
${item.supported_subentries.map(
(flowType) =>
html`<ha-md-menu-item
@click=${this._addSubEntry}
.entry=${item}
.flowType=${flowType}
graphic="icon"
>
<ha-svg-icon slot="start" .path=${mdiPlus}></ha-svg-icon>
Add sub entry</ha-md-menu-item
Add ${flowType}</ha-md-menu-item
>`
: nothing}
)}
<ha-md-divider role="separator" tabindex="-1"></ha-md-divider>
Expand Down Expand Up @@ -1589,7 +1591,7 @@ class HaConfigIntegrationPage extends SubscribeMixin(LitElement) {
}

private async _addSubEntry(ev) {
showSubConfigFlowDialog(this, ev.target.entry, {
showSubConfigFlowDialog(this, ev.target.entry, ev.target.flowType, {
startFlowHandler: ev.target.entry.entry_id,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class HaConfigIntegrationsDashboard extends SubscribeMixin(LitElement) {
supports_remove_device: false,
supports_unload: false,
supports_reconfigure: false,
supports_subentries: false,
supported_subentries: [],
num_subentries: 0,
pref_disable_new_entities: false,
pref_disable_polling: false,
Expand Down

0 comments on commit 270ab1d

Please sign in to comment.