From c6c1efddae0221a9163f7e428700e1fb6c9bf46a Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 19 Jun 2022 02:55:19 -0400 Subject: [PATCH] removing input area for now --- webapp/src/components/Inputs/InputArea.tsx | 6 +- .../components/Inputs/InputAutoComplete.tsx | 1 + webapp/src/components/Inputs/InputYaml.tsx | 1 - .../ServiceEditor/ServiceEditorTarget.tsx | 8 ++ webapp/src/haService/AreaRegistry.ts | 89 +++++++++++++------ webapp/src/haService/HAEntities.ts | 7 -- 6 files changed, 76 insertions(+), 36 deletions(-) diff --git a/webapp/src/components/Inputs/InputArea.tsx b/webapp/src/components/Inputs/InputArea.tsx index 2a50114..49d6357 100644 --- a/webapp/src/components/Inputs/InputArea.tsx +++ b/webapp/src/components/Inputs/InputArea.tsx @@ -7,16 +7,18 @@ import { useHAAreas } from "haService/AreaRegistry"; export type InputAreaProps = InputAutoCompletePropsBase & { restrictToDomain?: string[]; + restrictedIntegrations?: string[]; }; export const InputArea: FC = (props) => { // state const areas = useHAAreas(); + console.log(areas); return ( 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 diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index 69aec6d..b33eb02 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -147,6 +147,7 @@ export function InputAutoComplete( PopperComponent={(props) => { return ( ({ 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 }); diff --git a/webapp/src/components/ServiceEditor/ServiceEditorTarget.tsx b/webapp/src/components/ServiceEditor/ServiceEditorTarget.tsx index 214c7d8..260ac1f 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorTarget.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorTarget.tsx @@ -76,6 +76,14 @@ export const ServiceEditorTarget: FC = (props) => { area_id, }) } + restrictToDomain={ + domainRestrictions.length === 0 ? undefined : domainRestrictions + } + restrictedIntegrations={ + integrationRestrictions.length === 0 + ? undefined + : integrationRestrictions + } multiple /> diff --git a/webapp/src/haService/AreaRegistry.ts b/webapp/src/haService/AreaRegistry.ts index dd1defc..329d9e2 100644 --- a/webapp/src/haService/AreaRegistry.ts +++ b/webapp/src/haService/AreaRegistry.ts @@ -12,7 +12,8 @@ export interface AreaRegistryItem { export type AreaDB = Record< string, AreaRegistryItem & { - entity: Partial; + domains: Set; + integrations: Set; } >; @@ -25,18 +26,26 @@ export const fetchAreaRegistry = (conn: Connection) => type: "config/entity_registry/list", }), ]).then(([areas, er]) => { - const areaDB = areas.reduce((all, item) => { + const areaDB: AreaDB = areas.reduce((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((all = [], areaId) => { - const domain = methods.getAreaEntityDomain(areaId) ?? ""; - if (!domains.includes(domain)) { + out = out.reduce((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((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 => { diff --git a/webapp/src/haService/HAEntities.ts b/webapp/src/haService/HAEntities.ts index ddf68e5..c039ffc 100644 --- a/webapp/src/haService/HAEntities.ts +++ b/webapp/src/haService/HAEntities.ts @@ -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); }); }