sync'd up
This commit is contained in:
1 parent
7cb1a5d907
commit
a391eae7c5
19 files changed
+320
-170
No files matched your search
@@ -1,5 +1,8 @@
|
||||
import "./AutoInfoBox.css";
|
||||
import { AutomationMetadata } from "types/automations";
|
||||
import {
|
||||
AutomationMetadata,
|
||||
AutomationShortumationMetadata,
|
||||
} from "types/automations";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import InputText from "components/Inputs/Base/InputText";
|
||||
import { FC, ReactNode, useRef, useState } from "react";
|
||||
@@ -9,7 +12,7 @@ import { TagDB } from "components/AutomationManager/TagDB";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
|
||||
export interface AutoInfoBoxProps {
|
||||
metadata: AutomationMetadata;
|
||||
metadata: AutomationMetadata & Partial<AutomationShortumationMetadata>;
|
||||
tags: Array<[string, string]>;
|
||||
onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void;
|
||||
tagDB: TagDB;
|
||||
|
||||
@@ -26,16 +26,16 @@ import { DAGAutomationGraph } from "components/DAGGraph";
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import { InputTextView } from "components/Inputs/InputTextView";
|
||||
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, BareAutomationData } from "types/automations";
|
||||
import { MiniFailure } from "types/validators/helper";
|
||||
|
||||
import { AutoInfoBox } from "./AutoInfoBox";
|
||||
import { useAutomatioEditorState, EditorData } from "./state";
|
||||
|
||||
interface Props {
|
||||
automation?: AutomationData;
|
||||
automation?: AutomationData | BareAutomationData;
|
||||
dims: DAGDims;
|
||||
onUpdate: (auto: AutomationData) => void;
|
||||
onUpdate: (auto: AutomationData | BareAutomationData) => void;
|
||||
onTrigger: () => void;
|
||||
onDelete: () => void;
|
||||
tagDB: TagDB;
|
||||
@@ -215,7 +215,7 @@ export const AutomationEditor: FC<Props> = ({
|
||||
<Button
|
||||
className={"automation-editor--flow-wrapper--toolbar--save-btn"}
|
||||
onClick={save}
|
||||
disabled={state.status !== "changed" && !isNew}
|
||||
disabled={state.status !== "changed"}
|
||||
>
|
||||
Save <CheckMarkIcon color="#bf4" />
|
||||
</Button>
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { AutomationData, AutomationMetadata } from "types/automations";
|
||||
import {
|
||||
AutomationData,
|
||||
AutomationMetadata,
|
||||
BareAutomationData,
|
||||
} from "types/automations";
|
||||
import * as v from "types/validators/autmation";
|
||||
import { getFailures } from "types/validators/helper";
|
||||
import { MiniFailure } from "../../types/validators/helper";
|
||||
import { getFailures, MiniFailure } from "types/validators/helper";
|
||||
|
||||
export type EditorData = ReturnType<typeof genEditorData>;
|
||||
export type EditorState =
|
||||
@@ -26,12 +29,18 @@ export type EditorState =
|
||||
export type EditorStatus = EditorState["status"];
|
||||
|
||||
export const useAutomatioEditorState = (
|
||||
automation: AutomationData | undefined,
|
||||
onSave: (a: AutomationData) => void
|
||||
automation: AutomationData | BareAutomationData | undefined,
|
||||
onSave: (a: AutomationData | BareAutomationData) => void
|
||||
) => {
|
||||
// state
|
||||
const [state, update] = useState<EditorState>({
|
||||
status: "loading",
|
||||
});
|
||||
|
||||
// helpers
|
||||
const validate = (data: any) => {
|
||||
return getFailures(data, v.AutomationData);
|
||||
};
|
||||
const makeUpdate =
|
||||
<K extends keyof EditorData, D extends EditorData[K]>(name: K) =>
|
||||
(data: D) =>
|
||||
@@ -69,26 +78,9 @@ export const useAutomatioEditorState = (
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (automation) {
|
||||
const failures = getFailures(automation, v.AutomationData);
|
||||
if (failures) {
|
||||
update({
|
||||
status: "invalid",
|
||||
failures,
|
||||
data: genEditorData(automation),
|
||||
});
|
||||
} else {
|
||||
update({
|
||||
status: "unchanged",
|
||||
data: genEditorData(automation),
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [automation]);
|
||||
|
||||
return {
|
||||
validate: (data: any) => getFailures(data, v.AutomationData),
|
||||
// pub methods
|
||||
const methods = {
|
||||
validate,
|
||||
updateTrigger: makeUpdate("trigger"),
|
||||
updateSequence: makeUpdate("action"),
|
||||
updateCondition: makeUpdate("condition"),
|
||||
@@ -137,9 +129,30 @@ export const useAutomatioEditorState = (
|
||||
return state;
|
||||
},
|
||||
};
|
||||
|
||||
// effects
|
||||
useEffect(() => {
|
||||
if (automation) {
|
||||
const failures = validate(automation);
|
||||
if (failures) {
|
||||
update({
|
||||
status: "invalid",
|
||||
failures,
|
||||
data: genEditorData(automation),
|
||||
});
|
||||
} else {
|
||||
update({
|
||||
status: "unchanged",
|
||||
data: genEditorData(automation),
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [automation]);
|
||||
|
||||
return methods;
|
||||
};
|
||||
|
||||
const genEditorData = (automation: AutomationData) => {
|
||||
const genEditorData = (automation: AutomationData | BareAutomationData) => {
|
||||
const { tags: rawTags, trigger, action, condition, ...metadata } = automation;
|
||||
|
||||
const tags: [string, string][] = Object.keys(rawTags).map((tagName) => [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { HassEntities } from "home-assistant-js-websocket";
|
||||
import { useEffect, useState } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, BareAutomationData } from "types/automations";
|
||||
import { defaultAutomation } from "utils/defaults";
|
||||
import { TagDB } from "../TagDB";
|
||||
import { AutomationManagerAuto } from "../types";
|
||||
@@ -12,7 +12,9 @@ export const useAutomationDB = (
|
||||
) => {
|
||||
const autoMap = genMapping(haEntities, configAutomations, tagsDB);
|
||||
const [newAuto, setNewAuto] =
|
||||
useState<null | [AutomationManagerAuto, AutomationData]>(null);
|
||||
useState<
|
||||
null | [AutomationManagerAuto, AutomationData | BareAutomationData]
|
||||
>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (newAuto && autoMap[newAuto[0].id]) {
|
||||
@@ -30,7 +32,9 @@ export const useAutomationDB = (
|
||||
},
|
||||
getAutomationData(
|
||||
autoId: string
|
||||
): null | [AutomationManagerAuto, AutomationData | null] {
|
||||
):
|
||||
| null
|
||||
| [AutomationManagerAuto, AutomationData | BareAutomationData | null] {
|
||||
if (newAuto && newAuto[0].id === autoId) {
|
||||
return newAuto;
|
||||
}
|
||||
@@ -54,9 +58,9 @@ export const useAutomationDB = (
|
||||
tags: {},
|
||||
issue: "New Automation",
|
||||
isNew: true,
|
||||
source_file: auto.source_file,
|
||||
source_file_type: auto.source_file_type,
|
||||
configuration_key: auto.configuration_key,
|
||||
source_file: "n/a",
|
||||
source_file_type: "n/a",
|
||||
configuration_key: "n/a",
|
||||
},
|
||||
auto,
|
||||
]);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { HassEntities } from "home-assistant-js-websocket";
|
||||
import { useState } from "react";
|
||||
import { useCookies } from "react-cookie";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, BareAutomationData } from "types/automations";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useAutomationDB } from "./automationDB";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
@@ -12,7 +12,7 @@ export type UseAutomationManagerStateArgs = {
|
||||
configAutomations: AutomationData[];
|
||||
hassEntities: HassEntities;
|
||||
onUpdateTags: (aid: string, tags: Record<string, string>) => void;
|
||||
onAutomationAdd: (auto: AutomationData) => void;
|
||||
onAutomationAdd: (auto: BareAutomationData) => void;
|
||||
onAutomationUpdate: (auto: AutomationData) => void;
|
||||
onAutomationStateChange: (eid: string, on: boolean) => void;
|
||||
onAutomationRun: (eid: string) => void;
|
||||
@@ -68,7 +68,7 @@ export const useAutomationManagerState = ({
|
||||
get tagsDB() {
|
||||
return tagsDB;
|
||||
},
|
||||
get currentAutomation(): AutomationData | null {
|
||||
get currentAutomation(): AutomationData | BareAutomationData | null {
|
||||
return currentAutomation === null ? null : currentAutomation[1];
|
||||
},
|
||||
get currentAutomationIsNew() {
|
||||
@@ -95,11 +95,11 @@ export const useAutomationManagerState = ({
|
||||
const auto = automationDB.addNew();
|
||||
methods.setSelectedAutomationId(auto.id, true);
|
||||
},
|
||||
editorUpdateAutomation(auto: AutomationData) {
|
||||
editorUpdateAutomation(auto: AutomationData | BareAutomationData) {
|
||||
if (methods.currentAutomationIsNew) {
|
||||
onAutomationAdd(auto);
|
||||
} else if (methods.currentAutomationId) {
|
||||
onAutomationUpdate(auto);
|
||||
onAutomationUpdate(auto as AutomationData);
|
||||
}
|
||||
},
|
||||
sideBarUpdateAutomation(
|
||||
@@ -108,7 +108,13 @@ export const useAutomationManagerState = ({
|
||||
eid: string
|
||||
) {
|
||||
const previousAuto = automationDB.getAutomationData(aid);
|
||||
if (previousAuto && previousAuto[1]) {
|
||||
if (
|
||||
previousAuto &&
|
||||
previousAuto[1] &&
|
||||
"configuration_key" in previousAuto[1] &&
|
||||
"source_file" in previousAuto[1] &&
|
||||
"source_file_type" in previousAuto[1]
|
||||
) {
|
||||
const previousAutoState = previousAuto[1];
|
||||
if (
|
||||
previousAutoState.alias !== a.title ||
|
||||
|
||||
@@ -19,10 +19,10 @@ const { make, componentMeta } = makeStory({
|
||||
<AutomationManager
|
||||
api={useMockApiService(args.automations ?? [], !args.automations)}
|
||||
haEntities={useMockHAEntities(haEntities)}
|
||||
refreshAutomations={() => {}}
|
||||
triggerAutomation={() => {}}
|
||||
forceDeleteAutomation={() => {}}
|
||||
onAutomationStateChange={(eid, on) => {
|
||||
refreshAutomations={async () => {}}
|
||||
triggerAutomation={async () => {}}
|
||||
forceDeleteAutomation={async () => {}}
|
||||
onAutomationStateChange={async (eid, on) => {
|
||||
if (!haEntities.loading && !haEntities.error) {
|
||||
setHAE({
|
||||
...haEntities,
|
||||
|
||||
@@ -14,12 +14,13 @@ import { getFailures } from "types/validators/helper";
|
||||
import * as av from "types/validators/autmation";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
import InputYaml from "components/Inputs/Base/InputYaml";
|
||||
import { APIResponse } from "apiService/types";
|
||||
|
||||
export type AutomationManagerProps = {
|
||||
onAutomationStateChange: (eid: string, on: boolean) => void;
|
||||
refreshAutomations: () => void;
|
||||
forceDeleteAutomation: (eid: string) => void;
|
||||
triggerAutomation: (eid: string) => void;
|
||||
onAutomationStateChange: (eid: string, on: boolean) => Promise<any>;
|
||||
refreshAutomations: () => Promise<any>;
|
||||
forceDeleteAutomation: (eid: string) => Promise<any>;
|
||||
triggerAutomation: (eid: string) => Promise<any>;
|
||||
haEntities: HAEntitiesState;
|
||||
api: ApiService;
|
||||
};
|
||||
@@ -27,16 +28,78 @@ export type AutomationManagerProps = {
|
||||
export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
api,
|
||||
haEntities,
|
||||
onAutomationStateChange,
|
||||
refreshAutomations,
|
||||
forceDeleteAutomation,
|
||||
triggerAutomation,
|
||||
onAutomationStateChange: _onAutomationStateChange,
|
||||
refreshAutomations: _refreshAutomations,
|
||||
forceDeleteAutomation: _forceDeleteAutomation,
|
||||
triggerAutomation: _triggerAutomation,
|
||||
}) => {
|
||||
const snackbr = useSnackbar();
|
||||
const confirm = useConfirm();
|
||||
const validationsShown = useRef(false);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [isSaving, setIsSaving] = useState<string[]>([]);
|
||||
|
||||
// helpers
|
||||
function wrapPromise<T>(
|
||||
name: string,
|
||||
errorMessage: string,
|
||||
makePromise: () => Promise<T>
|
||||
) {
|
||||
setIsSaving(isSaving.concat([name]));
|
||||
return makePromise()
|
||||
.then((resp) => {
|
||||
setIsSaving(isSaving.filter((k) => k !== name));
|
||||
return resp;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
snackbr.enqueueSnackbar(errorMessage.replace("{{err}}", String(err)), {
|
||||
variant: "error",
|
||||
});
|
||||
setIsSaving(isSaving.filter((k) => k !== name));
|
||||
});
|
||||
}
|
||||
const wrapApiPromise = (
|
||||
name: string,
|
||||
errorMessage: string,
|
||||
makePromise: () => Promise<APIResponse<any>>
|
||||
) => {
|
||||
wrapPromise(name, errorMessage, makePromise).then((resp) => {
|
||||
if (resp) {
|
||||
if (resp.ok) {
|
||||
refreshAutomations();
|
||||
} else {
|
||||
snackbr.enqueueSnackbar(errorMessage.replace("{{err}}", resp.error), {
|
||||
variant: "error",
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const onAutomationStateChange = (eid: string, on: boolean) =>
|
||||
wrapPromise(
|
||||
"changing automation state",
|
||||
'failed to toggle autonation due to "{{err}}"',
|
||||
() => _onAutomationStateChange(eid, on)
|
||||
);
|
||||
const refreshAutomations = () =>
|
||||
wrapPromise(
|
||||
"refreshing automations",
|
||||
'failed refreshing automations due to "{{err}}"',
|
||||
() => _refreshAutomations()
|
||||
);
|
||||
const forceDeleteAutomation = (eid: string) =>
|
||||
wrapPromise("deleting automations", 'failed delete due to "{{err}}"', () =>
|
||||
_forceDeleteAutomation(eid)
|
||||
);
|
||||
const triggerAutomation = (eid: string) =>
|
||||
wrapPromise(
|
||||
"triggering automations",
|
||||
'failed trigger due to "{{err}}"',
|
||||
() => _triggerAutomation(eid)
|
||||
);
|
||||
|
||||
// effects
|
||||
useEffect(() => {
|
||||
refreshAutomations();
|
||||
// eslint-disable-next-line
|
||||
@@ -144,7 +207,6 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
}
|
||||
|
||||
// alias
|
||||
|
||||
const hassEntities = haEntities.collection;
|
||||
const configAutomations = api.state.automations.data;
|
||||
|
||||
@@ -155,15 +217,24 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
hassEntities,
|
||||
}}
|
||||
>
|
||||
{isSaving.length > 0 && (
|
||||
<>
|
||||
<span>{isSaving.join(" and ")}...</span>
|
||||
<LinearProgress />
|
||||
</>
|
||||
)}
|
||||
<AutomationManagerLoaded
|
||||
configAutomations={configAutomations.data}
|
||||
hassEntities={hassEntities}
|
||||
onAutomationRun={triggerAutomation}
|
||||
onAutomationStateChange={onAutomationStateChange}
|
||||
onAutomationAdd={async (auto) => {
|
||||
await api.updateAutomation(auto);
|
||||
refreshAutomations();
|
||||
}}
|
||||
onAutomationAdd={(auto) =>
|
||||
wrapApiPromise(
|
||||
"adding automation",
|
||||
"Failed to create automation, recieved error: {{err}}!",
|
||||
() => api.createAutomation(auto) as any
|
||||
)
|
||||
}
|
||||
onAutomationDelete={(aid, eid) => {
|
||||
const autoToDelete = configAutomations.data.find(
|
||||
({ id }) => id === aid
|
||||
@@ -188,30 +259,25 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
}
|
||||
// refreshAutomations();
|
||||
}}
|
||||
onAutomationUpdate={(auto) => {
|
||||
api
|
||||
.updateAutomation(auto)
|
||||
.then(() => refreshAutomations())
|
||||
.catch(() => {
|
||||
snackbr.enqueueSnackbar(
|
||||
"Failed to update automation, this may be resolved by a refresh or reboot of Home Assistant.",
|
||||
{
|
||||
variant: "error",
|
||||
}
|
||||
);
|
||||
});
|
||||
}}
|
||||
onAutomationUpdate={(auto) =>
|
||||
wrapApiPromise(
|
||||
"updating automation",
|
||||
"Failed to update automation, recieved error: {{err}}!",
|
||||
() => api.updateAutomation(auto) as any
|
||||
)
|
||||
}
|
||||
onUpdateTags={async (aid, t) => {
|
||||
setIsSaving(true);
|
||||
await api.updateTags({
|
||||
automation_id: aid,
|
||||
tags: t,
|
||||
});
|
||||
setIsSaving(false);
|
||||
wrapApiPromise(
|
||||
"updating tags",
|
||||
"Failed to update tags, recieved error: {{err}}!",
|
||||
() =>
|
||||
api.updateTags({
|
||||
automation_id: aid,
|
||||
tags: t,
|
||||
}) as any
|
||||
);
|
||||
}}
|
||||
>
|
||||
{isSaving && <LinearProgress />}
|
||||
</AutomationManagerLoaded>
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user