added ha websockets
This commit is contained in:
1 parent
cc930ee4ac
commit
878035e748
8 files changed
+146
-5
No files matched your search
@@ -1,5 +1,5 @@
|
||||
0:
|
||||
Type: Generic
|
||||
Type: Generic2
|
||||
1:
|
||||
Type: Generic
|
||||
2:
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
SKIP_PREFLIGHT_CHECK=true
|
||||
SKIP_PREFLIGHT_CHECK=true
|
||||
+3
-2
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { ErrorBoundary } from 'components/ErrorBoundary';
|
||||
|
||||
function App() {
|
||||
const api = useConnectedApiService();
|
||||
|
||||
return (
|
||||
<div className="app">
|
||||
<ErrorBoundary>
|
||||
|
||||
@@ -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>(haConnection);
|
||||
useDelayEffect(() => {
|
||||
if (conn.status !== haConnection.status) {
|
||||
setConn(haConnection);
|
||||
}
|
||||
}, [conn], conn.status === 'loading' ? 500 : 50000);
|
||||
return conn;
|
||||
};
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Collection, Connection } from "home-assistant-js-websocket";
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useHAConnection } from "./connection";
|
||||
|
||||
export type HACollectionState<T> = {
|
||||
ready: false,
|
||||
collection?: Collection<T>,
|
||||
} | {
|
||||
ready: true,
|
||||
collection: Collection<T>,
|
||||
}
|
||||
export const useHassCollection = <T>(getHACollection: (c: Connection) => Collection<T>) => {
|
||||
const [state, setState] = useState<HACollectionState<T>>({
|
||||
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;
|
||||
}
|
||||
+6
-1
@@ -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==
|
||||
|
||||
Reference in new issue
Block a user