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");
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -7,16 +7,32 @@ import {
|
||||
} from "haService/HAEntities.mock";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { createMockAuto } from "utils/mocks";
|
||||
import { useState } from "react";
|
||||
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: (args: {
|
||||
automations?: AutomationData[];
|
||||
haProps: MockHAEntitiesProps;
|
||||
}) => {
|
||||
const [haEntities, setHAE] = useState(args.haProps);
|
||||
return (
|
||||
<AutomationManager
|
||||
api={useMockApiService(args.automations ?? [], !args.automations)}
|
||||
haEntites={useMockHAEntities(args.haProps)}
|
||||
haEntities={useMockHAEntities(haEntities)}
|
||||
onAutomationStateChange={(eid, on) => {
|
||||
if (!haEntities.loading) {
|
||||
setHAE({
|
||||
...haEntities,
|
||||
entities: {
|
||||
...haEntities.entities,
|
||||
[eid]: {
|
||||
...haEntities.entities[eid],
|
||||
state: on ? "on" : "off",
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
|
||||
@@ -8,20 +8,24 @@ import { ApiService } from "apiService/core";
|
||||
import { HAEntitiesState } from "haService/HAEntities";
|
||||
import { AutomationManagerLoaded } from "./Loaded";
|
||||
import { Alert } from "@mui/material";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
export type AutomationManagerProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
onAutomationStateChange: (eid: string, on: boolean) => void;
|
||||
haEntities: HAEntitiesState;
|
||||
api: ApiService;
|
||||
};
|
||||
|
||||
export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
api,
|
||||
haEntites,
|
||||
haEntities,
|
||||
onAutomationStateChange,
|
||||
}) => {
|
||||
const snackbr = useSnackbar();
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
// handle bad states
|
||||
if (!haEntites.ready || !api.state.automations.ready) {
|
||||
if (!haEntities.ready || !api.state.automations.ready) {
|
||||
return (
|
||||
<div className="automation-manager loading">
|
||||
<div className="automation-manager--sidebar loading">
|
||||
@@ -48,24 +52,55 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
|
||||
// alias
|
||||
const configAutomations = api.state.automations.data;
|
||||
const hassEntities = haEntites.collection.state;
|
||||
const hassEntities = haEntities.collection.state;
|
||||
return (
|
||||
<AutomationManagerLoaded
|
||||
configAutomations={configAutomations.data}
|
||||
hassEntities={hassEntities}
|
||||
onAutomationStateChange={onAutomationStateChange}
|
||||
onAutomationAdd={(auto) =>
|
||||
api.updateAutomation({
|
||||
index: configAutomations.totalItems + 1,
|
||||
auto,
|
||||
})
|
||||
}
|
||||
onAutomationDelete={(aid) =>
|
||||
api.removeAutomation({
|
||||
index: configAutomations.data.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
),
|
||||
})
|
||||
}
|
||||
onAutomationDelete={(aid) => {
|
||||
const index = configAutomations.data.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
);
|
||||
if (index >= 0) {
|
||||
api.removeAutomation({
|
||||
index,
|
||||
});
|
||||
} else {
|
||||
snackbr.enqueueSnackbar(
|
||||
"Failed to delete automation, this may be resolved by a refresh or reboot of Home Assistant",
|
||||
{
|
||||
variant: "error",
|
||||
}
|
||||
);
|
||||
}
|
||||
}}
|
||||
onAutomationUpdate={(aid, auto) => {
|
||||
const index = configAutomations.data.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
);
|
||||
if (index >= 0) {
|
||||
api.updateAutomation({
|
||||
index: configAutomations.data.findIndex(
|
||||
({ metadata }) => metadata.id === aid
|
||||
),
|
||||
auto,
|
||||
});
|
||||
} else {
|
||||
snackbr.enqueueSnackbar(
|
||||
"Failed to update automation, this may be resolved by a refresh or reboot of Home Assistant.",
|
||||
{
|
||||
variant: "error",
|
||||
}
|
||||
);
|
||||
}
|
||||
}}
|
||||
onUpdateTags={async (aid, t) => {
|
||||
setIsSaving(true);
|
||||
await api.updateTags({
|
||||
|
||||
Reference in new issue
Block a user