Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/entity/compute_state_domain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HassEntity } from "home-assistant-js-websocket";
import type { HassEntity } from "home-assistant-js-websocket";
import { computeDomain } from "./compute_domain";

export const computeStateDomain = (stateObj: HassEntity) =>
Expand Down
12 changes: 5 additions & 7 deletions src/components/ha-selector/ha-selector-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export class HaAreaSelector extends LitElement {
oldSelector !== this.selector &&
this.selector.area.device?.integration
) {
this._loadConfigEntries();
getConfigEntries(this.hass, {
domain: this.selector.area.device.integration,
}).then((entries) => {
this._configEntries = entries;
});
}
}
}
Expand Down Expand Up @@ -85,12 +89,6 @@ export class HaAreaSelector extends LitElement {
}
return true;
};

private async _loadConfigEntries() {
this._configEntries = (await getConfigEntries(this.hass)).filter(
(entry) => entry.domain === this.selector.area.device?.integration
);
}
}

declare global {
Expand Down
12 changes: 5 additions & 7 deletions src/components/ha-selector/ha-selector-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export class HaDeviceSelector extends LitElement {
if (changedProperties.has("selector")) {
const oldSelector = changedProperties.get("selector");
if (oldSelector !== this.selector && this.selector.device?.integration) {
this._loadConfigEntries();
getConfigEntries(this.hass, {
domain: this.selector.device.integration,
}).then((entries) => {
this._configEntries = entries;
});
}
}
}
Expand Down Expand Up @@ -88,12 +92,6 @@ export class HaDeviceSelector extends LitElement {
}
return true;
};

private async _loadConfigEntries() {
this._configEntries = (await getConfigEntries(this.hass)).filter(
(entry) => entry.domain === this.selector.device.integration
);
}
}

declare global {
Expand Down
5 changes: 2 additions & 3 deletions src/components/ha-selector/ha-selector-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ export class HaTargetSelector extends SubscribeMixin(LitElement) {
private async _loadConfigEntries() {
this._configEntries = (await getConfigEntries(this.hass)).filter(
(entry) =>
entry.domain ===
(this.selector.target.device?.integration ||
this.selector.target.entity?.integration)
entry.domain === this.selector.target.device?.integration ||
entry.domain === this.selector.target.entity?.integration
);
}

Expand Down
20 changes: 18 additions & 2 deletions src/data/config_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,24 @@ export const ERROR_STATES: ConfigEntry["state"][] = [
"setup_retry",
];

export const getConfigEntries = (hass: HomeAssistant) =>
hass.callApi<ConfigEntry[]>("GET", "config/config_entries/entry");
export const getConfigEntries = (
hass: HomeAssistant,
filters?: { type?: "helper" | "integration"; domain?: string }
): Promise<ConfigEntry[]> => {
const params = new URLSearchParams();
if (filters) {
if (filters.type) {
params.append("type", filters.type);
}
if (filters.domain) {
params.append("domain", filters.domain);
}
}
return hass.callApi<ConfigEntry[]>(
"GET",
`config/config_entries/entry?${params.toString()}`
);
};

export const updateConfigEntry = (
hass: HomeAssistant,
Expand Down
10 changes: 8 additions & 2 deletions src/data/config_flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ export const ignoreConfigFlow = (
export const deleteConfigFlow = (hass: HomeAssistant, flowId: string) =>
hass.callApi("DELETE", `config/config_entries/flow/${flowId}`);

export const getConfigFlowHandlers = (hass: HomeAssistant) =>
hass.callApi<string[]>("GET", "config/config_entries/flow_handlers");
export const getConfigFlowHandlers = (
hass: HomeAssistant,
type?: "helper" | "integration"
) =>
hass.callApi<string[]>(
"GET",
`config/config_entries/flow_handlers${type ? `?type=${type}` : ""}`
);

export const fetchConfigFlowInProgress = (
conn: Connection
Expand Down
8 changes: 4 additions & 4 deletions src/data/energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ const getEnergyData = async (
end?: Date
): Promise<EnergyData> => {
const [configEntries, entityRegistryEntries, info] = await Promise.all([
getConfigEntries(hass),
getConfigEntries(hass, { domain: "co2signal" }),
subscribeOne(hass.connection, subscribeEntityRegistry),
getEnergyInfo(hass),
]);

const co2SignalConfigEntry = configEntries.find(
(entry) => entry.domain === "co2signal"
);
const co2SignalConfigEntry = configEntries.length
? configEntries[0]
: undefined;

let co2SignalEntity: string | undefined;

Expand Down
71 changes: 71 additions & 0 deletions src/data/helpers_crud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { fetchCounter, updateCounter, deleteCounter } from "./counter";
import {
fetchInputBoolean,
updateInputBoolean,
deleteInputBoolean,
} from "./input_boolean";
import {
fetchInputButton,
updateInputButton,
deleteInputButton,
} from "./input_button";
import {
fetchInputDateTime,
updateInputDateTime,
deleteInputDateTime,
} from "./input_datetime";
import {
fetchInputNumber,
updateInputNumber,
deleteInputNumber,
} from "./input_number";
import {
fetchInputSelect,
updateInputSelect,
deleteInputSelect,
} from "./input_select";
import { fetchInputText, updateInputText, deleteInputText } from "./input_text";
import { fetchTimer, updateTimer, deleteTimer } from "./timer";

export const HELPERS_CRUD = {
input_boolean: {
fetch: fetchInputBoolean,
update: updateInputBoolean,
delete: deleteInputBoolean,
},
input_button: {
fetch: fetchInputButton,
update: updateInputButton,
delete: deleteInputButton,
},
input_text: {
fetch: fetchInputText,
update: updateInputText,
delete: deleteInputText,
},
input_number: {
fetch: fetchInputNumber,
update: updateInputNumber,
delete: deleteInputNumber,
},
input_datetime: {
fetch: fetchInputDateTime,
update: updateInputDateTime,
delete: deleteInputDateTime,
},
input_select: {
fetch: fetchInputSelect,
update: updateInputSelect,
delete: deleteInputSelect,
},
counter: {
fetch: fetchCounter,
update: updateCounter,
delete: deleteCounter,
},
timer: {
fetch: fetchTimer,
update: updateTimer,
delete: deleteTimer,
},
};
2 changes: 1 addition & 1 deletion src/dialogs/config-flow/show-dialog-config-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const showConfigFlowDialog = (
loadDevicesAndAreas: true,
getFlowHandlers: async (hass) => {
const [handlers] = await Promise.all([
getConfigFlowHandlers(hass),
getConfigFlowHandlers(hass, "integration"),
hass.loadBackendTranslation("title", undefined, true),
]);

Expand Down
9 changes: 5 additions & 4 deletions src/dialogs/config-flow/step-flow-pick-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ class StepFlowPickHandler extends LitElement {

if (handler.is_add) {
if (handler.slug === "zwave_js") {
const entries = await getConfigEntries(this.hass);
const entry = entries.find((ent) => ent.domain === "zwave_js");
const entries = await getConfigEntries(this.hass, {
domain: "zwave_js",
});

if (!entry) {
if (!entries.length) {
return;
}

showZWaveJSAddNodeDialog(this, {
entry_id: entry.entry_id,
entry_id: entries[0].entry_id,
});
} else if (handler.slug === "zha") {
navigate("/config/zha/add");
Expand Down
4 changes: 2 additions & 2 deletions src/onboarding/onboarding-integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ class OnboardingIntegrations extends LitElement {
}

private async _loadConfigEntries() {
const entries = await getConfigEntries(this.hass!);
// We filter out the config entry for the local weather and rpi_power.
const entries = await getConfigEntries(this.hass!, { type: "integration" });
// We filter out the config entries that are automatically created during onboarding.
// It is one that we create automatically and it will confuse the user
// if it starts showing up during onboarding.
this._entries = entries.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ export class HaDeviceInfoZWaveJS extends LitElement {
return;
}

const configEntries = await getConfigEntries(this.hass);
const configEntries = await getConfigEntries(this.hass, {
domain: "zwave_js",
});
let zwaveJsConfEntries = 0;
for (const entry of configEntries) {
if (entry.domain !== "zwave_js") {
continue;
}
if (zwaveJsConfEntries) {
this._multipleConfigEntries = true;
}
Expand Down
54 changes: 26 additions & 28 deletions src/panels/config/energy/components/ha-energy-grid-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class EnergyGridSettings extends LitElement {
@property({ attribute: false })
public validationResult?: EnergyPreferencesValidation;

@state() private _configEntries?: ConfigEntry[];
@state() private _co2ConfigEntry?: ConfigEntry;

protected firstUpdated() {
this._fetchCO2SignalConfigEntries();
Expand Down Expand Up @@ -195,28 +195,28 @@ export class EnergyGridSettings extends LitElement {
"ui.panel.config.energy.grid.grid_carbon_footprint"
)}
</h3>
${this._configEntries?.map(
(entry) => html`<div class="row" .entry=${entry}>
<img
referrerpolicy="no-referrer"
src=${brandsUrl({
domain: "co2signal",
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
/>
<span class="content">${entry.title}</span>
<a href=${`/config/integrations#config_entry=${entry.entry_id}`}>
<ha-icon-button .path=${mdiPencil}></ha-icon-button>
</a>
<ha-icon-button
@click=${this._removeCO2Sensor}
.path=${mdiDelete}
></ha-icon-button>
</div>`
)}
${this._configEntries?.length === 0
? html`
${this._co2ConfigEntry
? html`<div class="row" .entry=${this._co2ConfigEntry}>
<img
referrerpolicy="no-referrer"
src=${brandsUrl({
domain: "co2signal",
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
/>
<span class="content">${this._co2ConfigEntry.title}</span>
<a
href=${`/config/integrations#config_entry=${this._co2ConfigEntry.entry_id}`}
>
<ha-icon-button .path=${mdiPencil}></ha-icon-button>
</a>
<ha-icon-button
@click=${this._removeCO2Sensor}
.path=${mdiDelete}
></ha-icon-button>
</div>`
: html`
<div class="row border-bottom">
<img
referrerpolicy="no-referrer"
Expand All @@ -232,17 +232,15 @@ export class EnergyGridSettings extends LitElement {
)}
</mwc-button>
</div>
`
: ""}
`}
</div>
</ha-card>
`;
}

private async _fetchCO2SignalConfigEntries() {
this._configEntries = (await getConfigEntries(this.hass)).filter(
(entry) => entry.domain === "co2signal"
);
const entries = await getConfigEntries(this.hass, { domain: "co2signal" });
this._co2ConfigEntry = entries.length ? entries[0] : undefined;
}

private _addCO2Sensor() {
Expand Down
14 changes: 11 additions & 3 deletions src/panels/config/energy/dialogs/dialog-energy-solar-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,17 @@ export class DialogEnergySolarSettings

private async _fetchSolarForecastConfigEntries() {
const domains = this._params!.info.solar_forecast_domains;
this._configEntries = (await getConfigEntries(this.hass)).filter((entry) =>
domains.includes(entry.domain)
);
this._configEntries =
domains.length === 0
? []
: domains.length === 1
? await getConfigEntries(this.hass, {
type: "integration",
domain: domains[0],
})
: (await getConfigEntries(this.hass, { type: "integration" })).filter(
(entry) => domains.includes(entry.domain)
);
}

private _handleForecastChanged(ev: CustomEvent) {
Expand Down
Loading