removing input area for now
This commit is contained in:
1 parent
ae352ff244
commit
c6c1efddae
6 files changed
+76
-36
No files matched your search
@@ -7,16 +7,18 @@ import { useHAAreas } from "haService/AreaRegistry";
|
||||
|
||||
export type InputAreaProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[];
|
||||
restrictedIntegrations?: string[];
|
||||
};
|
||||
|
||||
export const InputArea: FC<InputAreaProps> = (props) => {
|
||||
// state
|
||||
const areas = useHAAreas();
|
||||
console.log(areas);
|
||||
return (
|
||||
<InputAutoComplete
|
||||
{...props}
|
||||
validateOption={(v) => areas.validateOptions(v, props.restrictToDomain)}
|
||||
options={areas.getOptions(props.restrictToDomain)}
|
||||
validateOption={(v) => areas.validateOptions(v)}
|
||||
options={areas.getOptions()}
|
||||
getID={areas.getID}
|
||||
getLabel={areas.getLabel}
|
||||
onlyShowLabel
|
||||
|
||||
@@ -147,6 +147,7 @@ export function InputAutoComplete<T extends Option>(
|
||||
PopperComponent={(props) => {
|
||||
return (
|
||||
<Popper
|
||||
key={Math.random().toString()}
|
||||
{...props}
|
||||
className={[props.className, "input-autocomplete--popper"].join(
|
||||
" "
|
||||
|
||||
@@ -44,7 +44,6 @@ export default function InputYaml<T extends {}>({
|
||||
const setText = (t: string) => {
|
||||
try {
|
||||
yaml.load(t);
|
||||
console.log("success!", yaml.load(t));
|
||||
setState({ text: t, error: undefined, hasChanged: true });
|
||||
} catch (_) {
|
||||
setState({ text: t, error: "! Invalid Yaml !", hasChanged: true });
|
||||
|
||||
@@ -76,6 +76,14 @@ export const ServiceEditorTarget: FC<ServiceEditorTargetProps> = (props) => {
|
||||
area_id,
|
||||
})
|
||||
}
|
||||
restrictToDomain={
|
||||
domainRestrictions.length === 0 ? undefined : domainRestrictions
|
||||
}
|
||||
restrictedIntegrations={
|
||||
integrationRestrictions.length === 0
|
||||
? undefined
|
||||
: integrationRestrictions
|
||||
}
|
||||
multiple
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -12,7 +12,8 @@ export interface AreaRegistryItem {
|
||||
export type AreaDB = Record<
|
||||
string,
|
||||
AreaRegistryItem & {
|
||||
entity: Partial<EntityRegisteryItem>;
|
||||
domains: Set<string>;
|
||||
integrations: Set<string>;
|
||||
}
|
||||
>;
|
||||
|
||||
@@ -25,18 +26,26 @@ export const fetchAreaRegistry = (conn: Connection) =>
|
||||
type: "config/entity_registry/list",
|
||||
}),
|
||||
]).then(([areas, er]) => {
|
||||
const areaDB = areas.reduce<AreaDB>((all, item) => {
|
||||
const areaDB: AreaDB = areas.reduce<AreaDB>((all, item) => {
|
||||
return {
|
||||
...all,
|
||||
[item.area_id]: {
|
||||
...item,
|
||||
entity: {},
|
||||
domains: new Set(),
|
||||
integrations: new Set(),
|
||||
},
|
||||
};
|
||||
}, {});
|
||||
er.forEach((eri) => {
|
||||
if (eri.area_id) {
|
||||
areaDB[eri.area_id].entity = eri;
|
||||
if (eri.entity_id) {
|
||||
areaDB[eri.area_id].domains.add(
|
||||
eri.entity_id.split(".")[0].toLowerCase()
|
||||
);
|
||||
}
|
||||
if (eri.platform) {
|
||||
areaDB[eri.area_id].domains.add(eri.platform);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -54,36 +63,61 @@ export const useHAAreas = () => {
|
||||
const areas = useHassCollection(areaRegistryColl);
|
||||
|
||||
const methods = {
|
||||
getAreaEntityDomain(areaId: string): null | string {
|
||||
areaHasEntityDomain(areaId: string, domains: string[]): boolean {
|
||||
if (!areas.ready) {
|
||||
return null;
|
||||
return true;
|
||||
}
|
||||
const entityId = areas.collection.state[areaId].entity.entity_id;
|
||||
if (entityId) {
|
||||
return entityId.toLowerCase().split(".")[0] ?? null;
|
||||
for (const d of domains) {
|
||||
if (!areas.collection.state[areaId].domains.has(d)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return true;
|
||||
},
|
||||
areaHasEntityIntegration(areaId: string, integrations: string[]): boolean {
|
||||
if (!areas.ready) {
|
||||
return true;
|
||||
}
|
||||
for (const d of integrations) {
|
||||
if (!areas.collection.state[areaId].integrations.has(d)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
validateOptions(
|
||||
options: string[],
|
||||
restrictedDomains?: string[]
|
||||
restrictedDomains?: string[],
|
||||
restrictedIntegrations?: string[]
|
||||
): null | string[] {
|
||||
let out = options;
|
||||
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 = [], areaId) => {
|
||||
const domain = methods.getAreaEntityDomain(areaId) ?? "";
|
||||
if (!domains.includes(domain)) {
|
||||
out = out.reduce<string[]>((all = [], areaId) => {
|
||||
if (!methods.areaHasEntityDomain(areaId, domains)) {
|
||||
return [...all, `${methods.getLabel(areaId)} is not ${domainsStr}`];
|
||||
}
|
||||
return all;
|
||||
}, []);
|
||||
if (out.length > 0) {
|
||||
return out;
|
||||
}
|
||||
} else if (restrictedIntegrations) {
|
||||
const integrations = restrictedIntegrations.map((x) => x.toLowerCase());
|
||||
const domainsStr =
|
||||
integrations.length > 1
|
||||
? `one of "${integrations.join(", ")}"`
|
||||
: `"${integrations[0]}"`;
|
||||
out = out.reduce<string[]>((all = [], areaId) => {
|
||||
if (!methods.areaHasEntityIntegration(areaId, integrations)) {
|
||||
return [...all, `${methods.getLabel(areaId)} is not ${domainsStr}`];
|
||||
}
|
||||
return all;
|
||||
}, []);
|
||||
}
|
||||
if (out.length > 0) {
|
||||
return out;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
@@ -100,25 +134,28 @@ export const useHAAreas = () => {
|
||||
}
|
||||
return opt;
|
||||
},
|
||||
getOptions(restrictToDomain?: string[]): AreaOption[] {
|
||||
getOptions(
|
||||
restrictToDomain?: string[],
|
||||
restrictedIntegrations?: string[]
|
||||
): AreaOption[] {
|
||||
if (!areas.ready) {
|
||||
return [];
|
||||
}
|
||||
let keys = Object.keys(areas.collection.state);
|
||||
if (restrictToDomain) {
|
||||
const domains = restrictToDomain.map((x) => x.toLowerCase());
|
||||
keys = keys.filter((key) => {
|
||||
const domain = methods.getAreaEntityDomain(key);
|
||||
if (domain) {
|
||||
return domains.includes(domain);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
keys = keys.filter((key) => methods.areaHasEntityDomain(key, domains));
|
||||
}
|
||||
if (restrictedIntegrations) {
|
||||
const integrations = restrictedIntegrations.map((x) => x.toLowerCase());
|
||||
keys = keys.filter((key) =>
|
||||
methods.areaHasEntityIntegration(key, integrations)
|
||||
);
|
||||
}
|
||||
return keys.map((key) => ({
|
||||
id: key,
|
||||
label: areas.collection.state[key].name ?? prettyName(key),
|
||||
data: areas.collection.state[key].entity,
|
||||
data: areas.collection.state[key],
|
||||
}));
|
||||
},
|
||||
getLabel: (opt: AreaOption): string => {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Option } from "components/Inputs/InputAutoComplete";
|
||||
import { useHassCollection } from "./useHassCollection";
|
||||
import { entitiesColl } from "home-assistant-js-websocket";
|
||||
import { useHAConnection } from "./connection";
|
||||
import { entitySourceColl } from "./additionalCollections";
|
||||
|
||||
export type EntityOption = Option<{ domain: string; integration: string }>;
|
||||
@@ -121,16 +120,10 @@ export const useHAEntities = () => {
|
||||
}
|
||||
if (restrictedIntegrations && entitySource.ready) {
|
||||
const integrations = restrictedIntegrations.map((x) => x.toLowerCase());
|
||||
console.log(entitySource);
|
||||
keys = keys.filter((key) => {
|
||||
const integration = (
|
||||
entitySource.collection.state[key]?.domain ?? ""
|
||||
).toLowerCase();
|
||||
console.log({
|
||||
[key]: integration,
|
||||
domain: integrations[0],
|
||||
include: integrations.includes(integration),
|
||||
});
|
||||
return integrations.includes(integration);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user