diff --git a/webapp/src/components/AutomationManager/index.stories.tsx b/webapp/src/components/AutomationManager/index.stories.tsx index 02d99c3..127c250 100644 --- a/webapp/src/components/AutomationManager/index.stories.tsx +++ b/webapp/src/components/AutomationManager/index.stories.tsx @@ -20,7 +20,7 @@ const { make, componentMeta } = makeStory({ api={useMockApiService(args.automations ?? [], !args.automations)} haEntities={useMockHAEntities(haEntities)} onAutomationStateChange={(eid, on) => { - if (!haEntities.loading) { + if (!haEntities.loading && !haEntities.error) { setHAE({ ...haEntities, entities: { @@ -49,7 +49,7 @@ const { make, componentMeta } = makeStory({ export default componentMeta; export const Loading = make({}); -export const Errored = make({ +export const ErroredByAPI = make({ automations: undefined, haProps: { loading: false, @@ -57,6 +57,16 @@ export const Errored = make({ entitySource: {}, }, }); +export const ErroredByWS = make({ + automations: [], + haProps: { + loading: false, + error: { + entities: { originalError: "1", message: "Failed to connect" }, + entitySource: { originalError: "1", message: "Failed to connect" }, + }, + }, +}); export const Empty = make({ automations: [], haProps: { diff --git a/webapp/src/components/AutomationManager/index.tsx b/webapp/src/components/AutomationManager/index.tsx index 4f3d5d6..053be2c 100644 --- a/webapp/src/components/AutomationManager/index.tsx +++ b/webapp/src/components/AutomationManager/index.tsx @@ -25,6 +25,19 @@ export const AutomationManager: FC = ({ const [isSaving, setIsSaving] = useState(false); // handle bad states + if (!haEntities.ready && haEntities.error) { + return ( +
+ + Failed to connect to websocket, please check that your access token is + properly setup + {" "} +
+ {JSON.stringify(haEntities.error)} +
+ ); + } + if (!haEntities.ready || !api.state.automations.ready) { return (
diff --git a/webapp/src/haService/HAEntities.mock.ts b/webapp/src/haService/HAEntities.mock.ts index 805dc0c..cafbfeb 100644 --- a/webapp/src/haService/HAEntities.mock.ts +++ b/webapp/src/haService/HAEntities.mock.ts @@ -8,8 +8,16 @@ export type MockHAEntitiesProps = } | { loading: false; + error?: false; entities: HassEntities; entitySource: Record; + } + | { + loading: false; + error: { + entities: any; + entitySource: any; + }; }; export const useMockHAEntities = (args: MockHAEntitiesProps) => { if (args.loading) { @@ -23,7 +31,7 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => { ready: false, } ); - } else { + } else if (!args.error) { return createHAEntitiesState( { additional: {}, @@ -36,5 +44,18 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => { collection: args.entitySource, } ); + } else { + return createHAEntitiesState( + { + additional: {}, + ready: false, + error: args.error.entities, + }, + { + additional: {}, + ready: false, + error: args.error.entitySource, + } + ); } }; diff --git a/webapp/src/haService/useHassCollection.ts b/webapp/src/haService/useHassCollection.ts index 4f42a84..623bcca 100644 --- a/webapp/src/haService/useHassCollection.ts +++ b/webapp/src/haService/useHassCollection.ts @@ -6,6 +6,7 @@ type _HACollectionState = | { ready: false; collection?: undefined; + error?: string; } | { ready: true; @@ -26,19 +27,23 @@ export const useHassCollection = ( if (conn.status === "loaded") { return getHACollection(conn.connection); } - }, [conn.status]); + }, [conn, getHACollection]); useEffect(() => { if (collection) { - setState({ - ready: true, - collection: collection.state, - }); - const unsub = collection.subscribe((d) => { + if (collection.state) { setState({ ready: true, - collection: d, + collection: collection.state, }); + } + const unsub = collection.subscribe((d) => { + if (d) { + setState({ + ready: true, + collection: d, + }); + } }); return () => { unsub(); @@ -46,6 +51,23 @@ export const useHassCollection = ( } }, [collection, state.ready]); + useEffect(() => { + if (conn.status === "error") { + if (!state.ready && conn.error !== state.error) { + setState({ + ready: false, + error: conn.error, + }); + } + } + }, [conn]); + + if (state.ready) { + if (!state.collection) { + throw Error("What?"); + } + } + return { ...state, get additional(): Partial {