diff --git a/sample_config/.shortumations/tags.yaml b/sample_config/.shortumations/tags.yaml index f5ef990..a972a80 100644 --- a/sample_config/.shortumations/tags.yaml +++ b/sample_config/.shortumations/tags.yaml @@ -1,5 +1,5 @@ 0: - Type: Generic + Type: Generic2 1: Type: Generic 2: diff --git a/webapp/.env b/webapp/.env index 6f809cc..7d910f1 100644 --- a/webapp/.env +++ b/webapp/.env @@ -1 +1 @@ -SKIP_PREFLIGHT_CHECK=true +SKIP_PREFLIGHT_CHECK=true \ No newline at end of file diff --git a/webapp/package.json b/webapp/package.json index a71d683..a04acca 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -13,6 +13,7 @@ "@types/react": "^17.0.0", "@types/react-dom": "^17.0.0", "@uiw/react-codemirror": "^4.7.0", + "home-assistant-js-websocket": "^7.1.0", "jss": "^10.8.2", "react": "^17.0.2", "react-dom": "^17.0.2", @@ -80,10 +81,10 @@ "@types/chroma-js": "^2.1.3", "@types/js-yaml": "^4.0.5", "jest-environment-jsdom": "^27.5.1", - "ts-node": "^10.4.0" + "ts-node": "^10.8.0" }, "moduleDirectories": [ "node_modules", "src" ] -} +} \ No newline at end of file diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index b3dfdff..7245e83 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -6,6 +6,7 @@ import { ErrorBoundary } from 'components/ErrorBoundary'; function App() { const api = useConnectedApiService(); + return (
diff --git a/webapp/src/haService/connection.ts b/webapp/src/haService/connection.ts new file mode 100644 index 0000000..dcfb8ed --- /dev/null +++ b/webapp/src/haService/connection.ts @@ -0,0 +1,79 @@ +import { useState } from 'react'; +import { + getAuth, + createConnection, + ERR_HASS_HOST_REQUIRED, + createLongLivedTokenAuth, + Auth, + Connection, +} from "home-assistant-js-websocket"; +import { useDelayEffect } from 'utils/useDelay'; + +const HASS_URL = process.env.REACT_APP_HASS_URL as string; +const HASS_TOKEN = process.env.REACT_APP_HASS_TOKEN; +export type HAConnection = { + status: 'loading' +} | { + status: 'loaded', + connection: Connection, +} | { + status: 'error', + error: any, +}; +let haConnection: HAConnection = { + status: 'loading', +}; + +const createAuth = async () => { + console.log("detecting auth") + let auth: Auth + if (HASS_TOKEN) { + console.log("trying token") + return createLongLivedTokenAuth(HASS_URL, HASS_TOKEN); + } + try { + console.log("trying to get from mem") + auth = await getAuth(); + return auth + } catch (err) { + if (err !== ERR_HASS_HOST_REQUIRED) { + throw err + } + } + try { + console.log("trying to get from url") + auth = await getAuth({ hassUrl: HASS_URL }); + return auth + } catch (err) { + throw err + } +} + +async function startHAConnection() { + try { + const auth = await createAuth(); + const connection = await createConnection({ auth }); + haConnection = { + status: 'loaded', + connection, + } + return haConnection; + } catch (error) { + haConnection = { + status: 'error', + error, + } + throw error + } +} + +startHAConnection().then(console.log).catch(console.error); +export const useHAConnection = (): HAConnection => { + const [conn, setConn] = useState(haConnection); + useDelayEffect(() => { + if (conn.status !== haConnection.status) { + setConn(haConnection); + } + }, [conn], conn.status === 'loading' ? 500 : 50000); + return conn; +}; \ No newline at end of file diff --git a/webapp/src/haService/index.ts b/webapp/src/haService/index.ts new file mode 100644 index 0000000..0c4fc0a --- /dev/null +++ b/webapp/src/haService/index.ts @@ -0,0 +1,13 @@ +import { entitiesColl, servicesColl } from "home-assistant-js-websocket" +import { useHassCollection } from "./useHassCollection" + + +export const useHAService = () => { + const entities = useHassCollection(entitiesColl); + const services = useHassCollection(servicesColl); + + return { + entities, + services, + } +} \ No newline at end of file diff --git a/webapp/src/haService/useHassCollection.ts b/webapp/src/haService/useHassCollection.ts new file mode 100644 index 0000000..215d460 --- /dev/null +++ b/webapp/src/haService/useHassCollection.ts @@ -0,0 +1,42 @@ +import { Collection, Connection } from "home-assistant-js-websocket"; +import { useState, useEffect } from 'react'; +import { useHAConnection } from "./connection"; + +export type HACollectionState = { + ready: false, + collection?: Collection, +} | { + ready: true, + collection: Collection, +} +export const useHassCollection = (getHACollection: (c: Connection) => Collection) => { + const [state, setState] = useState>({ + ready: false + }); + const conn = useHAConnection(); + + useEffect(() => { + if (conn.status === 'loaded' && !state.ready) { + const collection = getHACollection(conn.connection); + setState({ + ready: false, + collection, + }) + return collection.subscribe(d => { + if (d && !state.ready) { + setState({ + ready: true, + collection, + }) + } else if (!d && state.ready) { + setState({ + ready: true, + collection, + }) + } + }) + } + }, [conn, getHACollection, state.ready]); + + return state; +} \ No newline at end of file diff --git a/webapp/yarn.lock b/webapp/yarn.lock index c682365..24d99e5 100644 --- a/webapp/yarn.lock +++ b/webapp/yarn.lock @@ -8431,6 +8431,11 @@ hoist-non-react-statics@^3.2.0, hoist-non-react-statics@^3.3.0, hoist-non-react- dependencies: react-is "^16.7.0" +home-assistant-js-websocket@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/home-assistant-js-websocket/-/home-assistant-js-websocket-7.1.0.tgz#ebcf1f4ee980d5c45610d4cb1ab09ddf3e579125" + integrity sha512-P2KZApv0Y6T+VY8hS6SzE+jxaIHBJT2eIg3EhmM92P5HJN3545vWn3YhdkOyKEcEmm2DowqM4aSWA+TyhIKqRw== + hoopy@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.4.tgz#609207d661100033a9a9402ad3dea677381c1b1d" @@ -14859,7 +14864,7 @@ ts-dedent@^2.0.0: resolved "https://registry.yarnpkg.com/ts-dedent/-/ts-dedent-2.2.0.tgz#39e4bd297cd036292ae2394eb3412be63f563bb5" integrity sha512-q5W7tVM71e2xjHZTlgfTDoPF/SmqKG5hddq9SzR49CH2hayqRKJtQ4mtRlSxKaJlR/+9rEM+mnBHf7I2/BQcpQ== -ts-node@^10.4.0: +ts-node@^10.8.0: version "10.8.0" resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.8.0.tgz#3ceb5ac3e67ae8025c1950626aafbdecb55d82ce" integrity sha512-/fNd5Qh+zTt8Vt1KbYZjRHCE9sI5i7nqfD/dzBBRDeVXZXS6kToW6R7tTU6Nd4XavFs0mAVCg29Q//ML7WsZYA==