generic device editor
This commit is contained in:
1 parent
ab8ecfeefb
commit
1ce934fd54
11 files changed
+322
-311
No files matched your search
@@ -1,11 +1,6 @@
|
||||
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
|
||||
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 { DeviceEditor } from "components/DeviceEditor";
|
||||
import { DeviceAction } from "types/automations/actions";
|
||||
import { prettyName } from "utils/formatting";
|
||||
import { OptionManager, updateActionData } from "./OptionManager";
|
||||
import { OptionManager } from "./OptionManager";
|
||||
export const ActionDeviceState: OptionManager<DeviceAction> = {
|
||||
defaultState: () => ({
|
||||
alias: "",
|
||||
@@ -13,7 +8,6 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
|
||||
}),
|
||||
isReady: () => true,
|
||||
Component: ({ state, setState, ha }) => {
|
||||
const update = updateActionData(state, setState);
|
||||
const options = ha.deviceExtras.useDeviceActions(
|
||||
state.device_id
|
||||
? {
|
||||
@@ -24,67 +18,14 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
|
||||
const caps = ha.deviceExtras.useDeviceCapalities(
|
||||
state.device_id && state.type && state.domain ? (state as any) : undefined
|
||||
);
|
||||
// aliases
|
||||
const actionId = (state.type ?? "") + (state.subtype ?? "");
|
||||
const entities = ((options[actionId] ?? {}).data ?? [])
|
||||
.map(({ entity_id }) => entity_id)
|
||||
.filter((x) => !!x) as string[];
|
||||
// render
|
||||
return (
|
||||
<>
|
||||
<InputDevice
|
||||
label="Device"
|
||||
value={state.device_id ?? ""}
|
||||
onChange={(device_id) => setState({ device_id: device_id ?? "" })}
|
||||
/>
|
||||
<InputAutoComplete
|
||||
label="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="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 as any)[field.name] ?? ""}
|
||||
onChange={(v) =>
|
||||
update({
|
||||
[field.name]: v,
|
||||
})
|
||||
}
|
||||
/>
|
||||
))}
|
||||
{Object.keys(options).length + entities.length === 0 && (
|
||||
<>This device has no actions.</>
|
||||
)}
|
||||
</>
|
||||
<DeviceEditor
|
||||
state={state}
|
||||
setState={setState}
|
||||
options={options}
|
||||
caps={caps}
|
||||
type="action"
|
||||
/>
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -1,16 +1,6 @@
|
||||
import { OptionManager } from "./OptionManager";
|
||||
import { AutomationTriggerDevice } from "types/automations/triggers";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import { InputEntity } from "components/Inputs/InputEntities";
|
||||
import InputTextBubble from "components/Inputs/InputTextBubble";
|
||||
import { InputDevice } from "components/Inputs/InputDevice";
|
||||
import InputAutoText from "components/Inputs/InputAutoText";
|
||||
import {
|
||||
BaseOption,
|
||||
InputAutoComplete,
|
||||
} from "components/Inputs/InputAutoComplete";
|
||||
import { prettyName } from "utils/formatting";
|
||||
import { DeviceTypeTrigger } from "haService/extras";
|
||||
import { DeviceEditor } from "components/DeviceEditor";
|
||||
|
||||
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||
defaultState: () => ({
|
||||
@@ -18,61 +8,31 @@ export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||
}),
|
||||
isReady: () => true,
|
||||
Component: ({ state, setState, ha }) => {
|
||||
const triggers: Record<
|
||||
string,
|
||||
BaseOption<{
|
||||
data: DeviceTypeTrigger;
|
||||
}>
|
||||
> = ha.deviceExtras
|
||||
.useDeviceTriggers(
|
||||
state.device_id
|
||||
? {
|
||||
deviceId: state.device_id,
|
||||
}
|
||||
: undefined
|
||||
)
|
||||
.reduce(
|
||||
(all, opt) => ({
|
||||
...all,
|
||||
[opt.type + (opt.subtype ?? "")]: {
|
||||
id: opt.type + (opt.subtype ?? ""),
|
||||
label:
|
||||
prettyName(opt.type) + (opt.subtype ? ` ${opt.subtype}` : ""),
|
||||
data: opt,
|
||||
},
|
||||
}),
|
||||
{}
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<InputDevice
|
||||
label="Device"
|
||||
value={state.device_id ?? ""}
|
||||
onChange={(device_id) =>
|
||||
setState({ device_id: device_id ?? "", platform: "device" })
|
||||
// state
|
||||
const triggers = ha.deviceExtras.useDeviceTriggers(
|
||||
state.device_id
|
||||
? {
|
||||
deviceId: state.device_id,
|
||||
}
|
||||
/>
|
||||
<InputAutoComplete
|
||||
label="Event"
|
||||
multiple={false}
|
||||
value={(state.type ?? "") + (state.subtype ?? "")}
|
||||
options={Object.keys(triggers).map((k) => triggers[k])}
|
||||
disabled={Object.keys(triggers).length === 0}
|
||||
onChange={(key) => {
|
||||
if (key) {
|
||||
const opt = triggers[key];
|
||||
const updateObj: any = { ...opt.data };
|
||||
delete updateObj["metadata"];
|
||||
setState(updateObj);
|
||||
} else {
|
||||
setState({
|
||||
device_id: state.device_id,
|
||||
platform: state.platform,
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
: undefined
|
||||
);
|
||||
const caps = ha.deviceExtras.useDeviceTriggerCapalities(
|
||||
state.device_id && state.type && state.domain ? (state as any) : undefined
|
||||
);
|
||||
|
||||
return (
|
||||
<DeviceEditor
|
||||
type="trigger"
|
||||
state={state}
|
||||
setState={(s) =>
|
||||
setState({
|
||||
platform: "device",
|
||||
...s,
|
||||
})
|
||||
}
|
||||
options={triggers}
|
||||
caps={caps}
|
||||
/>
|
||||
);
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user