import { BaseOption, InputAutoComplete, } from "components/Inputs/AutoComplete/InputAutoComplete"; import { InputDevice } from "components/Inputs/AutoComplete/InputDevice"; import { InputDeviceCapabilties } from "components/Inputs/AutoComplete/InputDeviceCapabilties"; import { InputEntity } from "components/Inputs/AutoComplete/InputEntities"; import { cleanUpUndefined } from "utils/helpers"; import { DeviceBaseType, DeviceTypeCapability } from "haService/types"; import { AutomationDeviceState } from "types/automations/common"; export type DeviceEditorProps = { type: "action" | "condition" | "trigger"; state: Partial; setState: (n: Partial>) => void; options: Record>; caps: Partial; }; export function DeviceEditor({ type, state, setState, caps, options, }: DeviceEditorProps) { // aliases const update = (upd: Partial>) => setState( cleanUpUndefined({ ...state, ...upd, }) ); const actionId = (state.type ?? "") + (state.subtype ?? ""); const entities = ((options[actionId] ?? {}).data ?? []) .map(({ entity_id }) => entity_id) .filter((x) => !!x) as string[]; // render return ( <> setState({ device_id: device_id ?? "" })} /> options[k])} disabled={Object.keys(options).length === 0} onChange={(key) => { if (key) { const opt = options[key].data[0]; const updateObj: any = { ...opt }; delete updateObj["metadata"]; setState(updateObj); } else { setState({ device_id: state.device_id, }); } }} /> {entities.length > 0 && ( update({ entity_id: entity_id ?? undefined }) } preSelectedEntityIds={entities} /> )} {caps?.extra_fields && ( )} {Object.keys(options).length + entities.length === 0 && ( <>This device has no {type}s. )} ); }