v1 of service editor

This commit is contained in:
ari committed 2022-06-14 15:06:02 -04:00
1 parent 0cecae5faa
commit 7b816c3fbf
16 files changed
+1185 -435

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
export interface TypedHassService {
name?: string;
description: string;
fields: Fields;
target?: Targets | null | undefined;
}
export type Fields = Record<string, FieldData>
export interface Targets {
device?: TargetData;
entity?: TargetData;
}
export interface TargetData {
domain?: string;
integration?: string;
}
export interface FieldData {
name?: string | undefined;
description: string;
required?: boolean;
example?: any;
selector?: FieldSelector;
advanced?: boolean;
default?: any;
}
export interface FieldSelector {
text?: SelectorText | null;
select?: SelectorSelect;
number?: SelectorNumber;
boolean?: null;
object?: null;
theme?: null;
addon?: null;
entity?: TargetData;
icon?: null;
color_rgb?: null;
color_temp?: SelectorColorTemp | null;
time?: null;
}
interface SelectorColorTemp {
min_mireds: number;
max_mireds: number;
}
interface SelectorNumber {
min: number;
max: number;
unit_of_measurement?: UnitOfMeasurement;
mode?: Mode;
step?: number;
}
enum Mode {
Box = "box",
}
enum UnitOfMeasurement {
Days = "days",
Empty = "%",
K = "K",
Seconds = "seconds",
Times = "times",
}
interface SelectorSelect {
options: OptionElement[];
}
type OptionElement = OptionClass | string;
interface OptionClass {
label: string;
value: string;
}
interface SelectorText {
multiline: boolean;
}
+35 -4
View File
@@ -6,11 +6,13 @@ import { DeviceRegistryItem } from "./types";
import { useHassCollection } from "./useHassCollection"
import { useHAConnection } from './connection';
import { useSnackbar } from "notistack";
import { TypedHassService } from "./fieldTypes";
export type Option<T = {}> = string | ({
export type BaseOption<T = {}> = {
id: string,
label: string,
} & T)
} & T
export type Option<T = {}> = string | BaseOption<T>;
export type EntityOption = Option<{ domain: string; }>
export type HAEntitiesState = ReturnType<typeof useHAEntities>;
export const useHAEntities = () => {
@@ -76,10 +78,33 @@ export const useHAEntities = () => {
}
}
export type ServiceOption = Option<{ domain: string, action: string, description: string }>;
export type ServiceOption = Option<{
domain: string,
action: string,
description: string,
data: TypedHassService,
}>;
export type HAServicesState = ReturnType<typeof useHAServices>;
export const useHAServices = () => {
const services = useHassCollection(servicesColl);
const methods = {
getOption(name: string): TypedHassService | null {
if (!services.ready) {
return null;
}
try {
const [domain, action] = name.split('.');
return {
name: services.collection.state[domain][action].name ?? `${domain}.${action}`,
description: services.collection.state[domain][action].description ?? "",
target: services.collection.state[domain][action].target,
fields: services.collection.state[domain][action].fields,
}
}catch(err: any) {
console.warn(err)
return null
}
},
getOptions(): ServiceOption[] {
if (!services.ready) {
return []
@@ -92,6 +117,12 @@ export const useHAServices = () => {
description: services.collection.state[domain][action].description ?? "",
domain,
action,
data: {
name: services.collection.state[domain][action].name ?? `${domain}.${action}`,
description: services.collection.state[domain][action].description ?? "",
target: services.collection.state[domain][action].target,
fields: services.collection.state[domain][action].fields,
}
})
)
return all.concat(opts);
@@ -109,7 +140,7 @@ export const useHAServices = () => {
}
return opt.label ?? opt.id
},
getID: (opt: DeviceRegistryOption): string => {
getID: (opt: ServiceOption): string => {
if (typeof opt === 'string') {
return opt
}
+144 -76
View File
@@ -1,85 +1,152 @@
import React, { FC, useEffect, useState } from "react";
import { ComponentMeta, ComponentStory } from '@storybook/react';
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useHA } from "haService";
import InputYaml from "components/Inputs/InputYaml";
import { InputList } from "components/Inputs/InputList";
import { Button } from "components/Inputs/Button";
import { ServiceEditor } from "components/ServiceEditor";
const Test: FC = () => {
const { services, callService } = useHA();
const { services, callService } = useHA();
const data = services.collection?.state ?? {};
const options = Object.keys(data).sort();
const data = services.collection?.state ?? {};
const options = Object.keys(data).sort();
const summary: Record<string, Record<string, { total: number, domains: string[], values: Set<string> }>> = {}
const fields: Record<string, Record<string, { total: number, values: Set<string> }>> = {}
const target: Record<string, Record<string, { total: number, values: Set<string> }>> = {}
// const flattened: any[] = [];
// Object.keys(data).forEach(domain => {
// Object.keys(data[domain]).forEach(
// service_name => {
// const service = data[domain][service_name];
// const all: any = {
// domain, service_name,
// }
Object.keys(data).forEach(domain =>
Object.keys(data[domain]).forEach(
action => {
const service = data[domain][action];
Object.keys(service.fields).forEach(field => {
const value = service.fields[field];
const valueType = `${typeof value}-${Array.isArray(value)}`;
fields[field] = fields[field] ?? {}
fields[field][valueType] = fields[field][valueType] ?? {
total: 0,
domains: [],
values: new Set()
}
fields[field][valueType].total += 1
fields[field][valueType].values.add(JSON.stringify(value))
// flattened.push({
// domain, service_name,
// fields: Object.keys(service.fields).map(k => ({
// name: k,
// data: service.fields[k]
// })),
// target: Object.keys(service.target ?? {}).map(k => ({
// name: k,
// data: (service.target as any)[k]
// }))
// })
// }
// )
// })
})
Object.keys(service.target ?? {}).forEach(field => {
const value = (service.target as any)[field];
const valueType = `${typeof value}-${Array.isArray(value)}`;
target[field] = target[field] ?? {}
target[field][valueType] = target[field][valueType] ?? {
total: 0,
domains: [],
values: new Set()
}
target[field][valueType].total += 1
target[field][valueType].values.add(JSON.stringify(value))
// console.log(flattened)
})
Object.keys(service).forEach(field => {
const value = (service as any)[field];
const valueType = `${typeof value}-${Array.isArray(value)}`;
summary[field] = summary[field] ?? {}
summary[field][valueType] = summary[field][valueType] ?? {
total: 0,
domains: [],
values: new Set()
}
summary[field][valueType].total += 1
summary[field][valueType].domains.push(`${domain}.${action}`)
summary[field][valueType].values.add(JSON.stringify(value))
})
}
)
)
// const summary: Record<string, Record<string, { total: number, domains: string[], values: Set<string> }>> = {}
// const fields: Record<string, Record<string, { total: number, values: Set<string> }>> = {}
// const target: Record<string, Record<string, { total: number, values: Set<string> }>> = {}
const [domain, setDomain] = useState(options[0] ?? "");
// Object.keys(data).forEach(domain =>
// Object.keys(data[domain]).forEach(
// action => {
// const service = data[domain][action];
// Object.keys(service.fields).forEach(field => {
// const value = service.fields[field];
// const valueType = `${typeof value}-${Array.isArray(value)}`;
// fields[field] = fields[field] ?? {}
// fields[field][valueType] = fields[field][valueType] ?? {
// total: 0,
// domains: [],
// values: new Set()
// }
// fields[field][valueType].total += 1
// fields[field][valueType].values.add(JSON.stringify(value))
const domains = Object.keys(data[domain] ?? "").sort();
// })
// Object.keys(service.target ?? {}).forEach(field => {
// const value = (service.target as any)[field];
// const valueType = `${typeof value}-${Array.isArray(value)}`;
// target[field] = target[field] ?? {}
// target[field][valueType] = target[field][valueType] ?? {
// total: 0,
// domains: [],
// values: new Set()
// }
// target[field][valueType].total += 1
// target[field][valueType].values.add(JSON.stringify(value))
const [service, setService] = useState(domains[0] ?? "");
// })
// Object.keys(service).forEach(field => {
// const value = (service as any)[field];
// const valueType = `${typeof value}-${Array.isArray(value)}`;
// summary[field] = summary[field] ?? {}
// summary[field][valueType] = summary[field][valueType] ?? {
// total: 0,
// domains: [],
// values: new Set()
// }
// summary[field][valueType].total += 1
// summary[field][valueType].domains.push(`${domain}.${action}`)
// summary[field][valueType].values.add(JSON.stringify(value))
// })
// }
// )
// )
useEffect(() => {
setService(domains[0] ?? "")
}, [domain, setService])
const [domain, setDomain] = useState(options[0] ?? "");
const domains = Object.keys(data[domain] ?? "").sort();
return <div style={{ overflow: 'auto', maxHeight: '100vh' }}>
<InputList label="Domains" current={domain} onChange={setDomain} options={options} />
<InputList label="Services" current={service} onChange={setService} options={domains} />
<Button onClick={() => callService(domain, service)}>Call</Button>
<InputYaml label="Service Data" value={(data[domain] ?? {})[service] ?? {}} onChange={() => { }} />
<ul style={{
const [service, setService] = useState(domains[0] ?? "");
const serviceDefn = (data[domain] ?? {})[service] ?? {};
useEffect(() => {
setService(domains[0] ?? "");
}, [domain, setService]);
let serviceEditElm = <>Failed to load</>;
try {
if (service) {
serviceEditElm = (
<div
style={{
border: "1px solid var(--primary-accent)",
maxWidth: "100vw",
padding: "2.5em",
margin: "2.5em",
}}
>
<ServiceEditor
service={serviceDefn}
serviceId={`${domain}.${service}`}
data={{ field: {}, target: {} }}
onUpdate={() => {}}
/>
</div>
);
}
} catch (err) {
serviceEditElm = <>{JSON.stringify(err)}</>;
}
return (
<div style={{ overflow: "auto", maxHeight: "100vh" }}>
<InputList
label="Domains"
current={domain}
onChange={setDomain}
options={options}
/>
<InputList
label="Services"
current={service}
onChange={setService}
options={domains}
/>
<Button onClick={() => callService(domain, service)}>Call</Button>
{serviceEditElm}
<InputYaml label="Service Data" value={serviceDefn} onChange={() => {}} />
<code>{JSON.stringify(serviceDefn, null, 4)}</code>
{/* <ul style={{
maxHeight: '20vh',
overflow: 'auto',
}}>
@@ -131,21 +198,22 @@ const Test: FC = () => {
</li>)}
</ul>
</li>)}
</ul>
</ul> */}
</div>
}
);
};
export default {
title: 'Services/HA Service',
component: Test,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {
}
} as ComponentMeta<typeof Test>
title: "Services/HA Service",
component: Test,
parameters: { actions: { argTypesRegex: "^on.*" } },
args: {},
} as ComponentMeta<typeof Test>;
export const Base: ComponentStory<typeof Test> = props => {
return <Page>
<Test {...props} />
export const Base: ComponentStory<typeof Test> = (props) => {
return (
<Page>
<Test {...props} />
</Page>
};
);
};