target device & action device v2
This commit is contained in:
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[];
|
options: T[];
|
||||||
getLabel?: (opt: T) => string;
|
getLabel?: (opt: T) => string;
|
||||||
getID?: (opt: T) => string;
|
getID?: (opt: T) => string;
|
||||||
helperText?: string;
|
|
||||||
endAdornment?: ReactNode;
|
|
||||||
required?: boolean;
|
|
||||||
onlyShowLabel?: boolean;
|
|
||||||
} & InputAutoCompletePropsBase;
|
} & InputAutoCompletePropsBase;
|
||||||
export type InputAutoCompletePropsBase = {
|
export type InputAutoCompletePropsBase = {
|
||||||
label?: string;
|
label?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
required?: boolean;
|
||||||
|
onlyShowLabel?: boolean;
|
||||||
|
helperText?: string;
|
||||||
|
endAdornment?: ReactNode;
|
||||||
} & (
|
} & (
|
||||||
| {
|
| {
|
||||||
value: string | null;
|
value: string | null;
|
||||||
@@ -143,6 +144,7 @@ export function InputAutoComplete<T extends Option>(
|
|||||||
options={options}
|
options={options}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
getOptionLabel={getLabel as any}
|
getOptionLabel={getLabel as any}
|
||||||
|
disabled={props.disabled}
|
||||||
PopperComponent={(props) => {
|
PopperComponent={(props) => {
|
||||||
return (
|
return (
|
||||||
<Popper
|
<Popper
|
||||||
@@ -200,6 +202,7 @@ export function InputAutoComplete<T extends Option>(
|
|||||||
required={props.required}
|
required={props.required}
|
||||||
label={props.label ?? "Entity ID"}
|
label={props.label ?? "Entity ID"}
|
||||||
helperText={helperText}
|
helperText={helperText}
|
||||||
|
disabled={props.disabled}
|
||||||
error={error}
|
error={error}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ export type InputDeviceProps = InputAutoCompletePropsBase;
|
|||||||
export const InputDevice: FC<InputDeviceProps> = (props) => {
|
export const InputDevice: FC<InputDeviceProps> = (props) => {
|
||||||
// state
|
// state
|
||||||
const dr = useHADeviceRegistry();
|
const dr = useHADeviceRegistry();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<InputAutoComplete
|
<InputAutoComplete
|
||||||
{...props}
|
{...props}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
max-height: 100%;
|
max-height: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
min-width: 100%;
|
||||||
max-width: 20em;
|
max-width: 20em;
|
||||||
|
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
|
||||||
import { InputDevice } from "components/Inputs/InputDevice";
|
import { InputDevice } from "components/Inputs/InputDevice";
|
||||||
import { InputEntity } from "components/Inputs/InputEntities";
|
import { InputEntity } from "components/Inputs/InputEntities";
|
||||||
import { InputList } from "components/Inputs/InputList";
|
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
|
state.device_id && state.type && state.domain ? (state as any) : undefined
|
||||||
);
|
);
|
||||||
// aliases
|
// aliases
|
||||||
let actions: DeviceAction[] = [];
|
const actionId = (state.type ?? "") + (state.subtype ?? "");
|
||||||
if (options.length > 0 && state.type) {
|
const entities = ((options[actionId] ?? {}).data ?? [])
|
||||||
const currentTypeOptions = options.filter(
|
|
||||||
({ type }) => type === state.type
|
|
||||||
);
|
|
||||||
if (currentTypeOptions.length > 0) {
|
|
||||||
actions = currentTypeOptions[0].actions;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const entities = actions
|
|
||||||
.map(({ entity_id }) => entity_id)
|
.map(({ entity_id }) => entity_id)
|
||||||
.filter((x) => !!x) as string[];
|
.filter((x) => !!x) as string[];
|
||||||
// render
|
// render
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<InputDevice
|
<InputDevice
|
||||||
|
label="Device"
|
||||||
value={state.device_id ?? ""}
|
value={state.device_id ?? ""}
|
||||||
onChange={(device_id) => setState({ device_id: device_id ?? "" })}
|
onChange={(device_id) => setState({ device_id: device_id ?? "" })}
|
||||||
/>
|
/>
|
||||||
{options.length > 0 && (
|
<InputAutoComplete
|
||||||
<InputList
|
label="Action"
|
||||||
key={"actions" + state.device_id}
|
multiple={false}
|
||||||
label="Action"
|
value={actionId}
|
||||||
current={state.type}
|
options={Object.keys(options).map((k) => options[k])}
|
||||||
onChange={(type) =>
|
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({
|
setState({
|
||||||
device_id: state.device_id,
|
device_id: state.device_id,
|
||||||
...options.filter((opt) => type === opt.type)[0].actions[0],
|
});
|
||||||
})
|
|
||||||
}
|
}
|
||||||
options={options.map(({ type }) => type)}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
{entities.length > 0 && (
|
{entities.length > 0 && (
|
||||||
<InputEntity
|
<InputEntity
|
||||||
key={"entities" + state.device_id}
|
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.</>
|
<>This device has no actions.</>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,38 +1,78 @@
|
|||||||
import { OptionManager } from './OptionManager';
|
import { OptionManager } from "./OptionManager";
|
||||||
import { AutomationTriggerDevice } from 'types/automations/triggers';
|
import { AutomationTriggerDevice } from "types/automations/triggers";
|
||||||
import InputText from 'components/Inputs/InputText';
|
import InputText from "components/Inputs/InputText";
|
||||||
import { InputEntity } from 'components/Inputs/InputEntities';
|
import { InputEntity } from "components/Inputs/InputEntities";
|
||||||
import InputTextBubble from 'components/Inputs/InputTextBubble';
|
import InputTextBubble from "components/Inputs/InputTextBubble";
|
||||||
import { InputDevice } from 'components/Inputs/InputDevice';
|
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> = {
|
export const TriggerDevice: OptionManager<AutomationTriggerDevice> = {
|
||||||
defaultState: () => ({
|
defaultState: () => ({
|
||||||
"platform": 'device',
|
platform: "device",
|
||||||
}),
|
}),
|
||||||
isReady: () => true,
|
isReady: () => true,
|
||||||
renderOptionList: (state, setState, { getDomainList }) => {
|
Component: ({ state, setState, ha }) => {
|
||||||
return <>
|
const triggers: Record<
|
||||||
<InputTextBubble label="Domain" options={getDomainList()} value={state.domain} onChange={domain => setState({
|
string,
|
||||||
...state,
|
BaseOption<{
|
||||||
domain: domain ? domain[0] : "",
|
data: DeviceTypeTrigger;
|
||||||
})} />
|
}>
|
||||||
<InputText label="Type" value={state.type ?? ""} onChange={type => setState({
|
> = ha.deviceExtras
|
||||||
...state,
|
.useDeviceTriggers(
|
||||||
type
|
state.device_id
|
||||||
})} />
|
? {
|
||||||
<InputText label="Subtype" value={state.subtype ?? ""} onChange={subtype => setState({
|
deviceId: state.device_id,
|
||||||
...state,
|
}
|
||||||
subtype
|
: undefined
|
||||||
})} />
|
)
|
||||||
<InputDevice value={state.device_id ?? ""} onChange={device_id => setState({
|
.reduce(
|
||||||
...state,
|
(all, opt) => ({
|
||||||
device_id: device_id ?? "",
|
...all,
|
||||||
})} />
|
[opt.type + (opt.subtype ?? "")]: {
|
||||||
<InputEntity restrictToDomain={state.domain ? [state.domain] : undefined} value={state.entity_id ?? ""} multiple onChange={entity_id => setState({
|
id: opt.type + (opt.subtype ?? ""),
|
||||||
...state,
|
label:
|
||||||
entity_id
|
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({});
|
export const DeviceActionFilledExample = Action.bind({});
|
||||||
DeviceFilledExample.args = {
|
DeviceActionFilledExample.args = {
|
||||||
...DeviceFilledExample.args,
|
...DeviceActionFilledExample.args,
|
||||||
node: {
|
node: {
|
||||||
type: "toggle",
|
type: "toggle",
|
||||||
device_id: "29c3443815620745c4bf4100b11588fd",
|
device_id: "c9711be86b0301955f72380509285485",
|
||||||
entity_id: "light.switch_island",
|
|
||||||
domain: "light",
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -1,14 +1,26 @@
|
|||||||
|
import { BaseOption } from "components/Inputs/InputAutoComplete";
|
||||||
import { MessageBase } from "home-assistant-js-websocket";
|
import { MessageBase } from "home-assistant-js-websocket";
|
||||||
import { OptionsObject, useSnackbar } from "notistack";
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { prettyName } from "utils/formatting";
|
||||||
import { HASendMessage } from "./types";
|
import { HASendMessage } from "./types";
|
||||||
|
|
||||||
export type DeviceTypeAction = {
|
export type DeviceTypeAction = {
|
||||||
type: string;
|
type: string;
|
||||||
|
subtype?: string;
|
||||||
entity_id?: string;
|
entity_id?: string;
|
||||||
device_id?: string;
|
device_id?: string;
|
||||||
domain: 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 = {
|
export type DeviceTypeCapability = {
|
||||||
extra_fields: Array<{
|
extra_fields: Array<{
|
||||||
name: string;
|
name: string;
|
||||||
@@ -48,37 +60,43 @@ export const getDeviceExtraWsCalls = (callHA: HASendMessage) => {
|
|||||||
return stData;
|
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 {
|
return {
|
||||||
useDeviceActions: createUseThing<
|
useDeviceActions: createUseThing<
|
||||||
{ deviceId: string },
|
{ deviceId: string },
|
||||||
any[],
|
DeviceTypeAction[],
|
||||||
Array<{
|
Record<string, BaseOption<{ data: DeviceTypeAction[] }>>
|
||||||
type: string;
|
|
||||||
actions: DeviceTypeAction[];
|
|
||||||
}>
|
|
||||||
>(
|
>(
|
||||||
({ deviceId }) => ({
|
({ deviceId }) => ({
|
||||||
type: "device_automation/action/list",
|
type: "device_automation/action/list",
|
||||||
device_id: deviceId,
|
device_id: deviceId,
|
||||||
}),
|
}),
|
||||||
(data) => {
|
reduceDeviceThings,
|
||||||
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],
|
|
||||||
}));
|
|
||||||
},
|
|
||||||
[]
|
|
||||||
),
|
),
|
||||||
useDeviceCapalities: createUseThing<
|
useDeviceCapalities: createUseThing<
|
||||||
DeviceTypeAction,
|
DeviceTypeAction,
|
||||||
@@ -92,5 +110,17 @@ export const getDeviceExtraWsCalls = (callHA: HASendMessage) => {
|
|||||||
(data) => data,
|
(data) => data,
|
||||||
{}
|
{}
|
||||||
),
|
),
|
||||||
|
useDeviceTriggers: createUseThing<
|
||||||
|
{ deviceId: string },
|
||||||
|
DeviceTypeTrigger[],
|
||||||
|
DeviceTypeTrigger[]
|
||||||
|
>(
|
||||||
|
({ deviceId }) => ({
|
||||||
|
type: "device_automation/trigger/list",
|
||||||
|
device_id: deviceId,
|
||||||
|
}),
|
||||||
|
(data) => data,
|
||||||
|
[]
|
||||||
|
),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -56,6 +56,7 @@ export const DeviceAction = st.intersection([
|
|||||||
AutomationActionNodeBase,
|
AutomationActionNodeBase,
|
||||||
st.type({
|
st.type({
|
||||||
type: st.optional(st.string()),
|
type: st.optional(st.string()),
|
||||||
|
subtype: st.optional(st.string()),
|
||||||
device_id: st.optional(st.string()),
|
device_id: st.optional(st.string()),
|
||||||
entity_id: st.optional(st.string()),
|
entity_id: st.optional(st.string()),
|
||||||
domain: st.optional(st.string()),
|
domain: st.optional(st.string()),
|
||||||
|
|||||||
@@ -131,9 +131,19 @@ export const getDescriptionFromAutomationNode = <N extends AutomationNodeTypes>(
|
|||||||
return `Trigger ${node.event}`;
|
return `Trigger ${node.event}`;
|
||||||
}
|
}
|
||||||
if ("device_id" in node) {
|
if ("device_id" in node) {
|
||||||
return `${node.type ?? ""} on ${
|
let out = "";
|
||||||
node.device_id ? namer.getDeviceName(node.device_id) : ""
|
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) {
|
if ("choose" in node) {
|
||||||
return "Choose";
|
return "Choose";
|
||||||
|
|||||||
Reference in new issue
Block a user