mirror of
https://github.com/home-assistant/frontend.git
synced 2026-05-23 16:50:57 +01:00
b73707751a
* WIP new dialog use states * WIP add zwave rebuild network routes details * Enhance Z-Wave JS rebuild network routes dialog with loading indicators and improved progress handling * Remove hass param from `domain-icon` * Remove unneeded @states * List more compact * fix prettier * fix tests * Rename device context to getDeviceArea
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import type { ConfigEntry } from "../../data/config_entries";
|
|
import { deleteConfigEntry } from "../../data/config_entries";
|
|
import type { EntityRegistryEntry } from "../../data/entity/entity_registry";
|
|
import { removeEntityRegistryEntry } from "../../data/entity/entity_registry";
|
|
import { HELPERS_CRUD } from "../../data/helpers_crud";
|
|
import type { IntegrationManifest } from "../../data/integration";
|
|
import type { Helper } from "../../panels/config/helpers/const";
|
|
import { isHelperDomain } from "../../panels/config/helpers/const";
|
|
import type { HomeAssistant } from "../../types";
|
|
import { isComponentLoaded } from "../config/is_component_loaded";
|
|
import { computeDomain } from "./compute_domain";
|
|
|
|
export const isDeletableEntity = (
|
|
hass: HomeAssistant,
|
|
entity_id: string,
|
|
manifests: IntegrationManifest[],
|
|
entityRegistry: EntityRegistryEntry[],
|
|
configEntries: ConfigEntry[],
|
|
fetchedHelpers: Helper[]
|
|
): boolean => {
|
|
const restored = !!hass.states[entity_id]?.attributes.restored;
|
|
if (restored) {
|
|
return true;
|
|
}
|
|
|
|
const domain = computeDomain(entity_id);
|
|
const entityRegEntry = entityRegistry.find((e) => e.entity_id === entity_id);
|
|
if (isHelperDomain(domain)) {
|
|
return !!(
|
|
isComponentLoaded(hass.config, domain) &&
|
|
entityRegEntry &&
|
|
fetchedHelpers.some((e) => e.id === entityRegEntry.unique_id)
|
|
);
|
|
}
|
|
|
|
const configEntryId = entityRegEntry?.config_entry_id;
|
|
if (!configEntryId) {
|
|
return false;
|
|
}
|
|
const configEntry = configEntries.find((e) => e.entry_id === configEntryId);
|
|
return (
|
|
manifests.find((m) => m.domain === configEntry?.domain)
|
|
?.integration_type === "helper"
|
|
);
|
|
};
|
|
|
|
export const deleteEntity = (
|
|
hass: HomeAssistant,
|
|
entity_id: string,
|
|
manifests: IntegrationManifest[],
|
|
entityRegistry: EntityRegistryEntry[],
|
|
configEntries: ConfigEntry[],
|
|
fetchedHelpers: Helper[]
|
|
) => {
|
|
// This function assumes the entity_id already was validated by isDeletableEntity and does not repeat all those checks.
|
|
const domain = computeDomain(entity_id);
|
|
const entityRegEntry = entityRegistry.find((e) => e.entity_id === entity_id);
|
|
if (isHelperDomain(domain)) {
|
|
if (isComponentLoaded(hass.config, domain)) {
|
|
if (
|
|
entityRegEntry &&
|
|
fetchedHelpers.some((e) => e.id === entityRegEntry.unique_id)
|
|
) {
|
|
HELPERS_CRUD[domain].delete(hass, entityRegEntry.unique_id);
|
|
return;
|
|
}
|
|
}
|
|
const stateObj = hass.states[entity_id];
|
|
if (!stateObj?.attributes.restored) {
|
|
return;
|
|
}
|
|
removeEntityRegistryEntry(hass, entity_id);
|
|
return;
|
|
}
|
|
|
|
const configEntryId = entityRegEntry?.config_entry_id;
|
|
const configEntry = configEntryId
|
|
? configEntries.find((e) => e.entry_id === configEntryId)
|
|
: undefined;
|
|
const isHelperEntryType = configEntry
|
|
? manifests.find((m) => m.domain === configEntry.domain)
|
|
?.integration_type === "helper"
|
|
: false;
|
|
|
|
if (isHelperEntryType) {
|
|
deleteConfigEntry(hass, configEntryId!);
|
|
return;
|
|
}
|
|
|
|
removeEntityRegistryEntry(hass, entity_id);
|
|
};
|