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",
},
};
+53 -23
View File
@@ -1,14 +1,26 @@
import { BaseOption } from "components/Inputs/InputAutoComplete";
import { MessageBase } from "home-assistant-js-websocket";
import { OptionsObject, useSnackbar } from "notistack";
import { useEffect, useRef, useState } from "react";
import { prettyName } from "utils/formatting";
import { HASendMessage } from "./types";
export type DeviceTypeAction = {
type: string;
subtype?: string;
entity_id?: string;
device_id?: string;
domain: string;
};
export type DeviceTypeTrigger = {
type: string;
subtype?: string;
platform: string;
device_id: string;
entity_id?: string;
domain: string;
metadata: Record<string, any>;
} & Record<string, any>;
export type DeviceTypeCapability = {
extra_fields: Array<{
name: string;
@@ -48,37 +60,43 @@ export const getDeviceExtraWsCalls = (callHA: HASendMessage) => {
return stData;
};
};
const reduceDeviceThings = function <
Raw extends { type: string; subtype?: string; entity_id?: string }
>(raw: Raw[]) {
return raw.reduce<Record<string, BaseOption<{ data: Raw[] }>>>(
(all, opt: Raw) => {
const id = opt.type + (opt.subtype ?? "");
if (!all[id]) {
return {
...all,
[id]: {
id,
label:
prettyName(opt.type) + (opt.subtype ? ` ${opt.subtype}` : ""),
data: [opt],
},
};
} else {
all[id].data.push(opt);
return all;
}
},
{}
);
};
return {
useDeviceActions: createUseThing<
{ deviceId: string },
any[],
Array<{
type: string;
actions: DeviceTypeAction[];
}>
DeviceTypeAction[],
Record<string, BaseOption<{ data: DeviceTypeAction[] }>>
>(
({ deviceId }) => ({
type: "device_automation/action/list",
device_id: deviceId,
}),
(data) => {
const dataMap: Record<string, DeviceTypeAction[]> = data.reduce(
(all = {}, opt: any) => {
if (!all[opt.type]) {
all[opt.type] = [];
}
all[opt.type].push(opt);
return all;
},
{}
);
return Object.keys(dataMap).map((type) => ({
type,
actions: dataMap[type],
}));
},
[]
reduceDeviceThings,
{}
),
useDeviceCapalities: createUseThing<
DeviceTypeAction,
@@ -92,5 +110,17 @@ export const getDeviceExtraWsCalls = (callHA: HASendMessage) => {
(data) => data,
{}
),
useDeviceTriggers: createUseThing<
{ deviceId: string },
DeviceTypeTrigger[],
DeviceTypeTrigger[]
>(
({ deviceId }) => ({
type: "device_automation/trigger/list",
device_id: deviceId,
}),
(data) => data,
[]
),
};
};
+1
View File
@@ -56,6 +56,7 @@ export const DeviceAction = st.intersection([
AutomationActionNodeBase,
st.type({
type: st.optional(st.string()),
subtype: st.optional(st.string()),
device_id: st.optional(st.string()),
entity_id: st.optional(st.string()),
domain: st.optional(st.string()),
+13 -3
View File
@@ -131,9 +131,19 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
return `Trigger ${node.event}`;
}
if ("device_id" in node) {
return `${node.type ?? ""} on ${
node.device_id ? namer.getDeviceName(node.device_id) : ""
}`;
let out = "";
if (node.type) {
out += `${prettyName(node.type ?? "")} `;
}
if (node.subtype) {
out += node.subtype + " ";
}
if (node.entity_id) {
out += namer.getEntityName(node.entity_id);
} else if (node.device_id) {
out += namer.getDeviceName(node.device_id);
}
return out;
}
if ("choose" in node) {
return "Choose";