diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index d916075..18257e8 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -19,14 +19,15 @@ export type InputAutoCompleteProps> = { 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( options={options} onChange={onChange} getOptionLabel={getLabel as any} + disabled={props.disabled} PopperComponent={(props) => { return ( ( required={props.required} label={props.label ?? "Entity ID"} helperText={helperText} + disabled={props.disabled} error={error} /> )} diff --git a/webapp/src/components/Inputs/InputDevice.tsx b/webapp/src/components/Inputs/InputDevice.tsx index a8f56a1..462b95f 100644 --- a/webapp/src/components/Inputs/InputDevice.tsx +++ b/webapp/src/components/Inputs/InputDevice.tsx @@ -10,7 +10,6 @@ export type InputDeviceProps = InputAutoCompletePropsBase; export const InputDevice: FC = (props) => { // state const dr = useHADeviceRegistry(); - return ( = { 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 ( <> setState({ device_id: device_id ?? "" })} /> - {options.length > 0 && ( - + 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 && ( = { } /> ))} - {actions.length + options.length === 0 && ( + {Object.keys(options).length + entities.length === 0 && ( <>This device has no actions. )} diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx b/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx index 87609b2..ebde520 100644 --- a/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx @@ -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 = { defaultState: () => ({ - "platform": 'device', + platform: "device", }), isReady: () => true, - renderOptionList: (state, setState, { getDomainList }) => { - return <> - setState({ - ...state, - domain: domain ? domain[0] : "", - })} /> - setState({ - ...state, - type - })} /> - setState({ - ...state, - subtype - })} /> - setState({ - ...state, - device_id: device_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 ( + <> + + setState({ device_id: device_id ?? "", platform: "device" }) + } + /> + 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, + }); + } + }} + /> + + ); + }, +}; diff --git a/webapp/src/components/NodeEditor/index.stories.tsx b/webapp/src/components/NodeEditor/index.stories.tsx index 8ecf0fa..dc080d1 100644 --- a/webapp/src/components/NodeEditor/index.stories.tsx +++ b/webapp/src/components/NodeEditor/index.stories.tsx @@ -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", }, }; diff --git a/webapp/src/haService/extras.ts b/webapp/src/haService/extras.ts index e189448..3292af1 100644 --- a/webapp/src/haService/extras.ts +++ b/webapp/src/haService/extras.ts @@ -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; +} & Record; + 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>>( + (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> >( ({ deviceId }) => ({ type: "device_automation/action/list", device_id: deviceId, }), - (data) => { - const dataMap: Record = 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, + [] + ), }; }; diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index f3f18ac..747dbbf 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -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()), diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.ts index 9a8343b..3c81f46 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.ts @@ -131,9 +131,19 @@ export const getDescriptionFromAutomationNode = ( 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";