import "./InputDeviceCapabilties.css"; import { HaFormSchema } from "services/ha/types"; import { useSnackbar } from "notistack"; import { useRef } from "react"; import { prettyName } from "utils/formatting"; import { InputAutoComplete, Option } from "./InputAutoComplete"; import InputBoolean from "components/Inputs/Base/InputBoolean"; import { InputTime } from "../Base/InputTime"; import InputNumber from "components/Inputs/Base/InputNumber"; import InputText from "components/Inputs/Base/InputText"; 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 ( <> ); })}
); }