entity filter by integration

This commit is contained in:
ari committed 2022-06-19 02:06:51 -04:00
1 parent 34a4bd93bd
commit dde8d76772
15 files changed
+397 -263

No files matched your search

+6 -128
View File
@@ -1,7 +1,9 @@
import { entitiesColl, servicesColl } from "home-assistant-js-websocket";
import { servicesColl } from "home-assistant-js-websocket";
import { Namer } from "utils/formatting";
import { deviceRegistryColl } from "./DeviceRegistry";
import { entityRegistryColl } from "./EntityRegistry";
import {
deviceRegistryColl,
entityRegistryColl,
} from "./additionalCollections";
import { DeviceRegistryItem, HASendMessage } from "./types";
import { useHassCollection } from "./useHassCollection";
import { useHAConnection } from "./connection";
@@ -9,131 +11,7 @@ import { useSnackbar } from "notistack";
import { TypedHassService } from "./fieldTypes";
import { Option } from "components/Inputs/InputAutoComplete";
import { getDeviceExtraWsCalls } from "./extras";
export type EntityOption = Option<{ domain: string }>;
export type HAEntitiesState = ReturnType<typeof useHAEntities>;
export const useHAEntities = () => {
const entities = useHassCollection(entitiesColl, (state) => ({
domains: Array.from(
new Set(Object.keys(state).map((x) => x.split(".")[0])).keys()
).sort(),
}));
const methods = {
getStates(inp: string[] | string, attribute?: string): string[] {
if (!entities.ready) {
return [];
}
let entityIds: string[] = [];
if (typeof inp === "string") {
entityIds = [inp];
} else if (Array.isArray(inp)) {
entityIds = inp;
}
let state: Set<string> = new Set();
entityIds.forEach((entityId) => {
if (entities.collection.state[entityId]) {
if (!attribute) {
state.add(entities.collection.state[entityId].state);
} else {
const val = (entities.collection.state[entityId]?.attributes ?? {})[
attribute
];
if (val) {
state.add(val);
}
}
}
});
return Array.from(state.keys());
},
getAttributes(inp: string[] | string): string[] {
if (!entities.ready) {
return [];
}
let entityIds: string[] = [];
if (typeof inp === "string") {
entityIds = [inp];
} else if (Array.isArray(inp)) {
entityIds = inp;
}
let attributes: Set<string> = new Set();
entityIds.forEach((entityId) => {
const thisAttrs = new Set(
Object.keys(entities.collection.state[entityId]?.attributes ?? {})
);
if (attributes.size === 0) {
attributes = thisAttrs;
} else {
attributes = new Set([...attributes].filter((a) => thisAttrs.has(a)));
}
});
return Array.from(attributes.keys());
},
getDomainList(): string[] {
return entities.additional?.domains ?? [];
},
validateOptions(
options: string[],
restrictedDomains?: string[]
): null | string[] {
if (restrictedDomains) {
const domains = restrictedDomains.map((x) => x.toLowerCase());
const domainsStr =
domains.length > 1
? `one of "${domains.join(", ")}"`
: `"${domains[0]}"`;
const out = options.reduce<string[]>((all = [], key) => {
if (!domains.includes(key.toLowerCase().split(".")[0])) {
return [...all, `${methods.getLabel(key)} is not ${domainsStr}`];
}
return all;
}, []);
if (out.length > 0) {
return out;
}
}
return null;
},
getOptions(restrictToDomain?: string[]): EntityOption[] {
if (!entities.ready) {
return [];
}
let keys = Object.keys(entities.collection.state);
if (restrictToDomain) {
const domains = restrictToDomain.map((x) => x.toLowerCase());
keys = keys.filter((key) =>
domains.includes(key.toLowerCase().split(".")[0])
);
}
return keys.map((key) => ({
id: key,
label: entities.collection.state[key].attributes.friendly_name ?? key,
domain: key.split(".")[0] ?? "n/a",
}));
},
getLabel: (opt: EntityOption): string => {
if (typeof opt === "string") {
if (entities.ready) {
return (
entities.collection.state[opt]?.attributes.friendly_name ?? opt
);
}
return opt;
}
return opt.label ?? opt.id;
},
getID: (opt: EntityOption): string => {
if (typeof opt === "string") {
return opt;
}
return opt.id;
},
};
return {
...entities,
...methods,
};
};
import { useHAEntities } from "./HAEntities";
export type ServiceOption = Option<{
domain: string;