100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
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 "services/haService/types";
|
|
import { AutomationDeviceState } from "types/automations/common";
|
|
import { useLang } from "services/lang";
|
|
|
|
export type DeviceEditorProps<DBT extends DeviceBaseType> = {
|
|
type: "action" | "condition" | "trigger";
|
|
state: Partial<AutomationDeviceState>;
|
|
setState: (n: Partial<AutomationDeviceState & Record<string, any>>) => void;
|
|
options: Record<string, BaseOption<{ data: DBT[] }>>;
|
|
caps: Partial<DeviceTypeCapability>;
|
|
};
|
|
export function DeviceEditor<DBT extends DeviceBaseType>({
|
|
type,
|
|
state,
|
|
setState,
|
|
caps,
|
|
options,
|
|
}: DeviceEditorProps<DBT>) {
|
|
// state
|
|
const langStore = useLang();
|
|
// aliases
|
|
const update = (upd: Partial<AutomationDeviceState & Record<string, any>>) =>
|
|
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 (
|
|
<>
|
|
<InputDevice
|
|
key="devices"
|
|
label={langStore.get("DEVICE")}
|
|
value={state.device_id ?? ""}
|
|
onChange={(device_id) => setState({ device_id: device_id ?? "" })}
|
|
/>
|
|
<InputAutoComplete
|
|
key="action"
|
|
label={langStore.get("ACTION")}
|
|
multiple={false}
|
|
value={actionId}
|
|
options={Object.keys(options).map((k) => 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 && (
|
|
<InputEntity
|
|
key={"entities" + state.device_id}
|
|
label={langStore.get("ENTITY")}
|
|
value={state.entity_id ?? ""}
|
|
multiple={false}
|
|
onChange={(entity_id) =>
|
|
update({ entity_id: entity_id ?? undefined })
|
|
}
|
|
preSelectedEntityIds={entities}
|
|
/>
|
|
)}
|
|
{caps?.extra_fields && (
|
|
<InputDeviceCapabilties
|
|
key="caps"
|
|
state={state}
|
|
setState={setState}
|
|
defn={caps.extra_fields}
|
|
/>
|
|
)}
|
|
{Object.keys(options).length + entities.length === 0 && (
|
|
<>
|
|
{langStore.get("WARNING_DEVICE_NO_TYPES", {
|
|
type: `${type}s`,
|
|
})}
|
|
</>
|
|
)}
|
|
</>
|
|
);
|
|
}
|