diff --git a/webapp/src/components/DeviceEditor/index.tsx b/webapp/src/components/DeviceEditor/index.tsx index 36b932b..af8e0ac 100644 --- a/webapp/src/components/DeviceEditor/index.tsx +++ b/webapp/src/components/DeviceEditor/index.tsx @@ -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({ preSelectedEntityIds={entities} /> )} - {caps?.extra_fields && - caps.extra_fields.map((field) => ( - - update({ - [field.name]: v, - }) - } - /> - ))} + {caps?.extra_fields && ( + + )} {Object.keys(options).length + entities.length === 0 && ( <>This device has no {type}s. )} diff --git a/webapp/src/components/Inputs/InputBoolean.tsx b/webapp/src/components/Inputs/InputBoolean.tsx index d3accfb..f07ed7b 100644 --- a/webapp/src/components/Inputs/InputBoolean.tsx +++ b/webapp/src/components/Inputs/InputBoolean.tsx @@ -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 - onChange(!value)} />} label={label} /> - + return ( + + onChange(!value)} + required={required} + placeholder={placeholder} + /> + } + label={label} + /> + + ); } diff --git a/webapp/src/components/Inputs/InputDeviceCapabilties.css b/webapp/src/components/Inputs/InputDeviceCapabilties.css new file mode 100644 index 0000000..a66f23e --- /dev/null +++ b/webapp/src/components/Inputs/InputDeviceCapabilties.css @@ -0,0 +1,6 @@ +.input-device-caps { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} \ No newline at end of file diff --git a/webapp/src/components/Inputs/InputDeviceCapabilties.tsx b/webapp/src/components/Inputs/InputDeviceCapabilties.tsx index fdcd54e..6121fd8 100644 --- a/webapp/src/components/Inputs/InputDeviceCapabilties.tsx +++ b/webapp/src/components/Inputs/InputDeviceCapabilties.tsx @@ -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> = { + defn: HaFormSchema[]; + state: T; + setState: (s: T) => void; }; +export function InputDeviceCapabilties>( + props: InputDeviceCapabiltiesProps +) { + const snackr = useSnackbar(); + const msgRef = useRef>({}); + return ( +
+ {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 ; + } else if (def.type === "string") { + return ; + } else if (def.type === "integer") { + return ( + + ); + } else if (def.type === "float") { + return ; + } else if (def.type === "constant") { + return ( + + ); + } else if (def.type === "positive_time_period_dict") { + return ; + } else if (def.type === "select") { + return ( + ({ + 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 ( + + ); + } + if (!msgRef.current[def.name + def.type]) { + msgRef.current[def.name + def.type] = true; + snackr.enqueueSnackbar( + + "{def.name}" is of a unimplemented type "{def.type}"!
+ Please report this to the maintainers.{" "} + For now, we leave you with a generic textbox!
+ DEBUG="{JSON.stringify(def)}" +
, + { + variant: "warning", + persist: true, + } + ); + } + return ( + <> + + + ); + })} +
+ ); +} diff --git a/webapp/src/haService/index.ts b/webapp/src/haService/index.ts index 716205a..64c02a9 100644 --- a/webapp/src/haService/index.ts +++ b/webapp/src/haService/index.ts @@ -31,12 +31,16 @@ export const useHAEntities = () => { } let state: Set = 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 = 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; diff --git a/webapp/src/haService/types.ts b/webapp/src/haService/types.ts index da4983a..07930f1 100644 --- a/webapp/src/haService/types.ts +++ b/webapp/src/haService/types.ts @@ -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; }; + +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[] | 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"; +} diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.ts index 4c8b567..3b874fc 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.ts @@ -61,7 +61,7 @@ export const getDescriptionFromAutomationNode = ( return out; } if (node.platform === "device") { - return getDescriptionForDeviceType(node, namer, true); + return getDescriptionForDeviceType(node as any, namer, true); } return node.platform; }