diff --git a/webapp/src/components/AutomationManager/Loaded/automationDB.ts b/webapp/src/components/AutomationManager/Loaded/automationDB.ts index 96f8f44..005eb7a 100644 --- a/webapp/src/components/AutomationManager/Loaded/automationDB.ts +++ b/webapp/src/components/AutomationManager/Loaded/automationDB.ts @@ -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; }, diff --git a/webapp/src/components/AutomationManager/Loaded/index.tsx b/webapp/src/components/AutomationManager/Loaded/index.tsx index 21c1772..f502037 100644 --- a/webapp/src/components/AutomationManager/Loaded/index.tsx +++ b/webapp/src/components/AutomationManager/Loaded/index.tsx @@ -28,7 +28,7 @@ export const AutomationManagerLoaded: FC< onSelectedAutomationId={state.setSelectedAutomationId} onAutomationAdd={() => state.addNew()} onAutomationDelete={onAutomationDelete} - onAutomationUpdate={() => {}} + onAutomationUpdate={state.sideBarUpdateAutomation} />
{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} /> ) : ( diff --git a/webapp/src/components/AutomationManager/Loaded/state.ts b/webapp/src/components/AutomationManager/Loaded/state.ts index d68e1c8..653b780 100644 --- a/webapp/src/components/AutomationManager/Loaded/state.ts +++ b/webapp/src/components/AutomationManager/Loaded/state.ts @@ -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) => 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"); } }, }; diff --git a/webapp/src/components/AutomationManager/index.stories.tsx b/webapp/src/components/AutomationManager/index.stories.tsx index 45d552a..02d99c3 100644 --- a/webapp/src/components/AutomationManager/index.stories.tsx +++ b/webapp/src/components/AutomationManager/index.stories.tsx @@ -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 ( { + if (!haEntities.loading) { + setHAE({ + ...haEntities, + entities: { + ...haEntities.entities, + [eid]: { + ...haEntities.entities[eid], + state: on ? "on" : "off", + }, + }, + }); + } + }} /> ); }, diff --git a/webapp/src/components/AutomationManager/index.tsx b/webapp/src/components/AutomationManager/index.tsx index cdd24a6..4546d57 100644 --- a/webapp/src/components/AutomationManager/index.tsx +++ b/webapp/src/components/AutomationManager/index.tsx @@ -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 = ({ 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 (
@@ -48,24 +52,55 @@ export const AutomationManager: FC = ({ // alias const configAutomations = api.state.automations.data; - const hassEntities = haEntites.collection.state; + const hassEntities = haEntities.collection.state; return ( 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({