automation updates
This commit is contained in:
1 parent
473684efa6
commit
2a827c24f6
5 files changed
+113
-15
No files matched your search
@@ -34,6 +34,12 @@ export const useAutomationDB = (
|
||||
}
|
||||
return (autoMap[autoId] ?? [null, null])[1];
|
||||
},
|
||||
getAutomationState(autoId: string): string | null {
|
||||
if (newAuto && newAuto[0].id === autoId) {
|
||||
return newAuto[0].state;
|
||||
}
|
||||
return (autoMap[autoId] ?? [{ state: null }, null])[0].state;
|
||||
},
|
||||
isAutoNew(autoId: string) {
|
||||
return (newAuto && newAuto[0].id === autoId) ?? false;
|
||||
},
|
||||
|
||||
@@ -28,7 +28,7 @@ export const AutomationManagerLoaded: FC<
|
||||
onSelectedAutomationId={state.setSelectedAutomationId}
|
||||
onAutomationAdd={() => state.addNew()}
|
||||
onAutomationDelete={onAutomationDelete}
|
||||
onAutomationUpdate={() => {}}
|
||||
onAutomationUpdate={state.sideBarUpdateAutomation}
|
||||
/>
|
||||
<div className={["automation-manager--editor"].join(" ")}>
|
||||
{state.currentAutomationId !== null ? (
|
||||
@@ -39,7 +39,7 @@ export const AutomationManagerLoaded: FC<
|
||||
flipped: true,
|
||||
}}
|
||||
automation={state.currentAutomation}
|
||||
onUpdate={state.updateAutomation}
|
||||
onUpdate={state.editorUpdateAutomation}
|
||||
tagDB={state.tagsDB}
|
||||
/>
|
||||
) : (
|
||||
|
||||
@@ -5,21 +5,28 @@ import { AutomationData } from "types/automations";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useAutomationDB } from "./automationDB";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
import { AutomationManagerAutoUpdatable } from "../types";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export type UseAutomationManagerStateArgs = {
|
||||
configAutomations: AutomationData[];
|
||||
hassEntities: HassEntities;
|
||||
onUpdateTags: (aid: string, tags: Record<string, string>) => void;
|
||||
onAutomationAdd: (auto: AutomationData) => void;
|
||||
onAutomationUpdate: (aid: string, auto: AutomationData) => void;
|
||||
onAutomationStateChange: (eid: string, on: boolean) => void;
|
||||
};
|
||||
export const useAutomationManagerState = ({
|
||||
configAutomations,
|
||||
hassEntities,
|
||||
onUpdateTags,
|
||||
onAutomationAdd,
|
||||
onAutomationUpdate,
|
||||
onAutomationStateChange,
|
||||
}: UseAutomationManagerStateArgs) => {
|
||||
// state
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
const cookies = useAMSCookies();
|
||||
const tagsDB = useTagDB(
|
||||
configAutomations.map(({ metadata: { id }, tags }) => ({
|
||||
@@ -82,10 +89,44 @@ export const useAutomationManagerState = ({
|
||||
const auto = automationDB.addNew();
|
||||
methods.setSelectedAutomationId(auto.metadata.id, true);
|
||||
},
|
||||
updateAutomation(auto: AutomationData) {
|
||||
editorUpdateAutomation(auto: AutomationData) {
|
||||
if (methods.currentAutomationIsNew) {
|
||||
onAutomationAdd(auto);
|
||||
} else if (methods.currentAutomationId) {
|
||||
onAutomationUpdate(methods.currentAutomationId, auto);
|
||||
}
|
||||
},
|
||||
sideBarUpdateAutomation(
|
||||
a: AutomationManagerAutoUpdatable,
|
||||
aid: string,
|
||||
eid: string
|
||||
) {
|
||||
const previousAutoState = automationDB.getAutomationData(aid);
|
||||
if (previousAutoState) {
|
||||
if (
|
||||
previousAutoState.metadata.alias !== a.title ||
|
||||
previousAutoState.metadata.description !== a.description
|
||||
) {
|
||||
onAutomationUpdate(aid, {
|
||||
...previousAutoState,
|
||||
metadata: {
|
||||
...previousAutoState.metadata,
|
||||
alias: a.title,
|
||||
description: a.description,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
snackbr.enqueueSnackbar(
|
||||
`Failed to save automation "${aid}" contents - ${JSON.stringify(
|
||||
a
|
||||
)} -- not found in /config`,
|
||||
{ variant: "error" }
|
||||
);
|
||||
}
|
||||
const previousState = automationDB.getAutomationState(aid);
|
||||
if (previousState !== a.state) {
|
||||
onAutomationStateChange(eid, a.state === "on");
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user