device editor working with special caps

This commit is contained in:
ari committed 2022-06-18 23:23:08 -04:00
1 parent debe48f2b4
commit 1496bc66ad
7 files changed
+233 -34

No files matched your search

+8 -13
View File
@@ -3,6 +3,7 @@ import {
InputAutoComplete,
} from "components/Inputs/InputAutoComplete";
import { InputDevice } from "components/Inputs/InputDevice";
import { InputDeviceCapabilties } from "components/Inputs/InputDeviceCapabilties";
import { InputEntity } from "components/Inputs/InputEntities";
import InputText from "components/Inputs/InputText";
import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager";
@@ -75,19 +76,13 @@ export function DeviceEditor<DBT extends DeviceBaseType>({
preSelectedEntityIds={entities}
/>
)}
{caps?.extra_fields &&
caps.extra_fields.map((field) => (
<InputText
key={"extra" + state.device_id + field.name}
label={prettyName(field.name)}
value={(state as any)[field.name] ?? ""}
onChange={(v) =>
update({
[field.name]: v,
})
}
/>
))}
{caps?.extra_fields && (
<InputDeviceCapabilties
state={state}
setState={setState}
defn={caps.extra_fields}
/>
)}
{Object.keys(options).length + entities.length === 0 && (
<>This device has no {type}s.</>
)}
+22 -5
View File
@@ -1,6 +1,6 @@
import Checkbox from "@mui/material/Checkbox";
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormGroup from "@mui/material/FormGroup";
import FormControlLabel from "@mui/material/FormControlLabel";
export interface Props {
label: string;
@@ -8,6 +8,8 @@ export interface Props {
onChange: (v: boolean) => void;
disabled?: boolean;
className?: string;
required?: boolean;
placeholder?: string;
}
export default function InputBoolean({
label,
@@ -15,8 +17,23 @@ export default function InputBoolean({
onChange,
disabled = false,
className,
required,
placeholder,
}: Props) {
return <FormGroup className={className}>
<FormControlLabel disabled={disabled} control={<Checkbox checked={value} onChange={() => onChange(!value)} />} label={label} />
</FormGroup>
return (
<FormGroup className={className}>
<FormControlLabel
disabled={disabled}
control={
<Checkbox
checked={value}
onChange={() => onChange(!value)}
required={required}
placeholder={placeholder}
/>
}
label={label}
/>
</FormGroup>
);
}
@@ -0,0 +1,6 @@
.input-device-caps {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
@@ -1,5 +1,120 @@
import { FC } from "react";
import "./InputDeviceCapabilties.css";
import { HaFormSchema } from "haService/types";
import { useSnackbar } from "notistack";
import { useRef } from "react";
import { prettyName } from "utils/formatting";
import { InputAutoComplete, Option } from "./InputAutoComplete";
import InputBoolean from "./InputBoolean";
import { InputTime } from "./InputDayTime";
import InputNumber from "./InputNumber";
import InputText from "./InputText";
export const InputDeviceCapabilties: FC<{}> = () => {
return <></>;
export type InputDeviceCapabiltiesProps<T extends Record<string, any>> = {
defn: HaFormSchema[];
state: T;
setState: (s: T) => void;
};
export function InputDeviceCapabilties<T extends Record<string, any>>(
props: InputDeviceCapabiltiesProps<T>
) {
const snackr = useSnackbar();
const msgRef = useRef<Record<string, boolean>>({});
return (
<div className="input-device-caps">
{props.defn.map((def, i) => {
const prop = {
key: String(i),
className: "input-device-cap",
label:
prettyName(def.name) +
" " +
(JSON.stringify(def.description) ?? ""),
required: def.required,
placeholder: def.default,
value: props.state[def.name],
onChange: (v: any) =>
props.setState({
...props.state,
[def.name]: v,
}),
};
if (def.type === "boolean") {
return <InputBoolean {...prop} />;
} else if (def.type === "string") {
return <InputText {...prop} />;
} else if (def.type === "integer") {
return (
<InputNumber
{...prop}
step={1}
max={def.valueMax}
min={def.valueMin}
/>
);
} else if (def.type === "float") {
return <InputNumber {...prop} step={0.01} />;
} else if (def.type === "constant") {
return (
<InputText {...prop} value={def.value ?? props.state[def.name]} />
);
} else if (def.type === "positive_time_period_dict") {
return <InputTime {...prop} />;
} else if (def.type === "select") {
return (
<InputAutoComplete
{...prop}
multiple={false}
options={def.options.map(([value, label]) => ({
id: value,
label: prettyName(label),
}))}
/>
);
} else if (def.type === "multi_select") {
let options: Option[] = [];
if (Array.isArray(def.options)) {
options = def.options.map((opt) =>
Array.isArray(opt)
? {
id: opt[0],
label: prettyName(opt[1]),
}
: {
id: opt,
label: prettyName(opt),
}
);
} else {
options = Object.entries(def.options).map(([id, label]) => ({
id,
label: prettyName(label),
}));
}
return (
<InputAutoComplete {...prop} multiple={true} options={options} />
);
}
if (!msgRef.current[def.name + def.type]) {
msgRef.current[def.name + def.type] = true;
snackr.enqueueSnackbar(
<span>
"{def.name}" is of a unimplemented type "{def.type}"! <br />
Please report this to the maintainers.{" "}
<b>For now, we leave you with a generic textbox!</b> <br />
DEBUG="{JSON.stringify(def)}"
</span>,
{
variant: "warning",
persist: true,
}
);
}
return (
<>
<InputText {...prop} />
</>
);
})}
</div>
);
}
+11 -7
View File
@@ -31,12 +31,16 @@ export const useHAEntities = () => {
}
let state: Set<string> = new Set();
entityIds.forEach((entityId) => {
if (!attribute) {
state.add(entities.collection.state[entityId].state);
} else {
const val = entities.collection.state[entityId].attributes[attribute];
if (val) {
state.add(val);
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);
}
}
}
});
@@ -55,7 +59,7 @@ export const useHAEntities = () => {
let attributes: Set<string> = new Set();
entityIds.forEach((entityId) => {
const thisAttrs = new Set(
Object.keys(entities.collection.state[entityId].attributes)
Object.keys(entities.collection.state[entityId]?.attributes ?? {})
);
if (attributes.size === 0) {
attributes = thisAttrs;
+67 -5
View File
@@ -39,9 +39,71 @@ export type DeviceConditionType = DeviceBaseType & {
condition: "device";
};
export type DeviceTypeCapability = {
extra_fields: Array<{
name: string;
required?: true;
type: string;
}>;
extra_fields: Array<HaFormSchema>;
};
export type HaFormSchema =
| HaFormConstantSchema
| HaFormStringSchema
| HaFormIntegerSchema
| HaFormFloatSchema
| HaFormBooleanSchema
| HaFormSelectSchema
| HaFormMultiSelectSchema
| HaFormTimeSchema
| HaFormGridSchema;
export interface HaFormBaseSchema {
name: string;
default?: any;
required?: boolean;
description?: {
suffix?: string;
suggested_value?: any;
};
}
export interface HaFormGridSchema extends HaFormBaseSchema {
type: "grid";
name: "";
column_min_width?: string;
schema: HaFormSchema[];
}
export interface HaFormConstantSchema extends HaFormBaseSchema {
type: "constant";
value?: string;
}
export interface HaFormIntegerSchema extends HaFormBaseSchema {
type: "integer";
valueMin?: number;
valueMax?: number;
}
export interface HaFormSelectSchema extends HaFormBaseSchema {
type: "select";
options: Array<[string, string]>;
}
export interface HaFormMultiSelectSchema extends HaFormBaseSchema {
type: "multi_select";
options: Record<string, string> | string[] | Array<[string, string]>;
}
export interface HaFormFloatSchema extends HaFormBaseSchema {
type: "float";
}
export interface HaFormStringSchema extends HaFormBaseSchema {
type: "string";
format?: string;
}
export interface HaFormBooleanSchema extends HaFormBaseSchema {
type: "boolean";
}
export interface HaFormTimeSchema extends HaFormBaseSchema {
type: "positive_time_period_dict";
}
+1 -1
View File
@@ -61,7 +61,7 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
return out;
}
if (node.platform === "device") {
return getDescriptionForDeviceType(node, namer, true);
return getDescriptionForDeviceType(node as any, namer, true);
}
return node.platform;
}