target device & action device v2

This commit is contained in:
ari committed 2022-06-17 16:06:51 -04:00
1 parent 04f3e6194f
commit a32e365d86
9 files changed
+173 -92

No files matched your search

@@ -19,14 +19,15 @@ export type InputAutoCompleteProps<T extends Option<any>> = {
options: T[];
getLabel?: (opt: T) => string;
getID?: (opt: T) => string;
helperText?: string;
endAdornment?: ReactNode;
required?: boolean;
onlyShowLabel?: boolean;
} & InputAutoCompletePropsBase;
export type InputAutoCompletePropsBase = {
label?: string;
className?: string;
disabled?: boolean;
required?: boolean;
onlyShowLabel?: boolean;
helperText?: string;
endAdornment?: ReactNode;
} & (
| {
value: string | null;
@@ -143,6 +144,7 @@ export function InputAutoComplete<T extends Option>(
options={options}
onChange={onChange}
getOptionLabel={getLabel as any}
disabled={props.disabled}
PopperComponent={(props) => {
return (
<Popper
@@ -200,6 +202,7 @@ export function InputAutoComplete<T extends Option>(
required={props.required}
label={props.label ?? "Entity ID"}
helperText={helperText}
disabled={props.disabled}
error={error}
/>
)}
@@ -10,7 +10,6 @@ export type InputDeviceProps = InputAutoCompletePropsBase;
export const InputDevice: FC<InputDeviceProps> = (props) => {
// state
const dr = useHADeviceRegistry();
return (
<InputAutoComplete
{...props}
+1
View File
@@ -13,6 +13,7 @@
max-height: 100%;
height: 100%;
width: 100%;
min-width: 100%;
max-width: 20em;
text-overflow: ellipsis;
@@ -1,3 +1,4 @@
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
import { InputDevice } from "components/Inputs/InputDevice";
import { InputEntity } from "components/Inputs/InputEntities";
import { InputList } from "components/Inputs/InputList";
@@ -24,39 +25,37 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
state.device_id && state.type && state.domain ? (state as any) : undefined
);
// 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
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 ?? "" })}
/>
{options.length > 0 && (
<InputList
key={"actions" + state.device_id}
label="Action"
current={state.type}
onChange={(type) =>
<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,
...options.filter((opt) => type === opt.type)[0].actions[0],
})
});
}
options={options.map(({ type }) => type)}
/>
)}
}}
/>
{entities.length > 0 && (
<InputEntity
key={"entities" + state.device_id}
@@ -82,7 +81,7 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
}
/>
))}
{actions.length + options.length === 0 && (
{Object.keys(options).length + entities.length === 0 && (
<>This device has no actions.</>
)}
</>
@@ -1,38 +1,78 @@
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 { 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";
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
defaultState: () => ({
"platform": 'device',
platform: "device",
}),
isReady: () => true,
renderOptionList: (state, setState, { getDomainList }) => {
return <>
<InputTextBubble label="Domain" options={getDomainList()} value={state.domain} onChange={domain => setState({
...state,
domain: domain ? domain[0] : "",
})} />
<InputText label="Type" value={state.type ?? ""} onChange={type => setState({
...state,
type
})} />
<InputText label="Subtype" value={state.subtype ?? ""} onChange={subtype => setState({
...state,
subtype
})} />
<InputDevice value={state.device_id ?? ""} onChange={device_id => setState({
...state,
device_id: device_id ?? "",
})} />
<InputEntity restrictToDomain={state.domain ? [state.domain] : undefined} value={state.entity_id ?? ""} multiple onChange={entity_id => setState({
...state,
entity_id
})} />
</>
}
}
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" })
}
/>
<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,
});
}
}}
/>
</>
);
},
};
@@ -124,13 +124,11 @@ StateTriggerExample.args = {
},
};
export const DeviceFilledExample = Action.bind({});
DeviceFilledExample.args = {
...DeviceFilledExample.args,
export const DeviceActionFilledExample = Action.bind({});
DeviceActionFilledExample.args = {
...DeviceActionFilledExample.args,
node: {
type: "toggle",
device_id: "29c3443815620745c4bf4100b11588fd",
entity_id: "light.switch_island",
domain: "light",
device_id: "c9711be86b0301955f72380509285485",
},
};