added ha websockets
This commit is contained in:
1 parent
cc930ee4ac
commit
878035e748
8 files changed
+146
-5
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user