device action builder

This commit is contained in:
ari committed 2022-06-17 13:37:25 -04:00
1 parent 6be6005531
commit 029df7932e
16 files changed
+725 -434

No files matched your search

@@ -1,36 +1,83 @@
import { InputDevice } from "components/Inputs/InputDevice";
import { InputEntity } from "components/Inputs/InputEntities";
import { InputList } from "components/Inputs/InputList";
import InputText from "components/Inputs/InputText";
import { DeviceAction } from "types/automations/actions";
import { OptionManager, updateActionData } from './OptionManager';
import { prettyName } from "utils/formatting";
import { OptionManager, updateActionData } from "./OptionManager";
export const ActionDeviceState: OptionManager<DeviceAction> = {
defaultState: () => ({
alias: "",
device_id: ""
device_id: "",
}),
isReady: () => true,
renderOptionList: (state, setState, entities) => {
Component: ({ state, setState, ha }) => {
const update = updateActionData(state, setState);
return <>
<InputDevice
value={state.device_id ?? ""}
onChange={device_id => update({ device_id: device_id ?? "" })}
/>
<InputText
textBoxFor="type"
label="Action"
value={state.type ?? ""}
onChange={type => update({ type })}
/>
<InputEntity
value={state.entity_id ?? ""}
onChange={entity_id => update({
entity_id: entity_id ? entity_id : undefined,
domain: entity_id ? entity_id.split('.')[0] : ""
})}
/>
</>
}
}
const options = ha.deviceExtras.useDeviceActions(state.device_id);
const caps = ha.deviceExtras.useDeviceCapalities(state as any);
// aliases
let actions: DeviceAction[] = [];
if (options.length > 0 && state.type) {
const currentTypeOptions = options.filter(
({ type }) => type === state.type
);
if (currentTypeOptions.length > 0) {
actions = currentTypeOptions[0].actions;
}
}
const entities = actions
.map(({ entity_id }) => entity_id)
.filter((x) => !!x) as string[];
// render
return (
<>
<InputDevice
value={state.device_id ?? ""}
onChange={(device_id) => update({ device_id: device_id ?? "" })}
/>
{options.length > 0 && (
<InputList
key={"actions" + state.device_id}
label="Action"
current={state.type}
onChange={(type) =>
setState({
device_id: state.device_id,
...options.filter((opt) => type === opt.type)[0].actions[0],
})
}
options={options.map(({ type }) => type)}
/>
)}
{entities.length > 0 && (
<InputEntity
key={"entities" + state.device_id}
label="Entity"
value={state.entity_id ?? ""}
multiple={false}
onChange={(entity_id) =>
update({ entity_id: entity_id ?? undefined })
}
preSelectedEntityIds={entities}
/>
)}
{caps?.extra_fields &&
caps.extra_fields.map((field) => (
<InputText
key={"extra" + state.device_id + field.name}
label={prettyName(field.name)}
value={state[field.name] ?? ""}
onChange={(v) =>
update({
[field.name]: v,
})
}
/>
))}
{actions.length + options.length === 0 && (
<>This device has no actions.</>
)}
</>
);
},
};