all the latest socket work
This commit is contained in:
1 parent
a530e5e6e0
commit
bc773154c6
18 files changed
+117
-61
No files matched your search
+8
-1
@@ -3,4 +3,11 @@ webapp/build
|
||||
webapp/.storybook
|
||||
webapp/.vscode
|
||||
webapp/.env.local
|
||||
webapp/tmux.sh
|
||||
webapp/tmux.sh
|
||||
api/.env
|
||||
api/.coverage
|
||||
api/venv
|
||||
api/build
|
||||
api/.mypy_cache
|
||||
api/.vscode
|
||||
api/.coverage
|
||||
+2
-1
@@ -10,8 +10,8 @@ ARG BUILD_VERSION
|
||||
|
||||
# <--- Environment Variables -->
|
||||
ENV HASSIO_TOKEN ""
|
||||
ENV SUPERVISOR_TOKEN ""
|
||||
ENV BUILD_VERSION $BUILD_VERSION
|
||||
ENV HASS_URL "http://supervisor/"
|
||||
|
||||
# <--- System Wide Dependencies -->
|
||||
ENV LANG C.UTF-8
|
||||
@@ -52,3 +52,4 @@ LABEL \
|
||||
ENTRYPOINT [ "/app/bin/run.sh" ]
|
||||
CMD [ "/app" ]
|
||||
|
||||
LABEL org.opencontainers.image.source https://github.com/asosnovsky/Shortumation
|
||||
@@ -43,5 +43,4 @@ clean:
|
||||
run-local:
|
||||
rm -f web
|
||||
ln -s ../webapp/build web
|
||||
cd ../webapp && yarn build
|
||||
USE_EXAMPLE_REPO=1 $(VENV_BIN)/uvicorn src.app:app --reload
|
||||
@@ -1,8 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from src.env import BUILD_VERSION
|
||||
from src.env import BUILD_VERSION, HASS_URL, HASSIO_TOKEN
|
||||
from src.hass_config import HassConfig
|
||||
|
||||
|
||||
@@ -10,9 +8,11 @@ def make_details_router(hass_config: HassConfig) -> APIRouter:
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/")
|
||||
def read_ping():
|
||||
def read_details():
|
||||
return {
|
||||
"version": BUILD_VERSION,
|
||||
"hassio_token": HASSIO_TOKEN,
|
||||
"hass_url": HASS_URL,
|
||||
"config": hass_config.get_configuration_path(),
|
||||
"tags": hass_config.get_automation_tags_path(),
|
||||
"autos": hass_config.get_automation_path(),
|
||||
|
||||
@@ -4,5 +4,7 @@ from decouple import Csv, config
|
||||
|
||||
ROOT_FOLDER = Path(__file__).parent.parent.absolute()
|
||||
BUILD_VERSION = config("BUILD_VERSION", "0.0.0")
|
||||
HASSIO_TOKEN = config("HASSIO_TOKEN", "")
|
||||
HASS_URL = config("HASS_URL", "/")
|
||||
LOG_LEVEL = config("LOG_LEVEL", "INFO")
|
||||
ORIGIN = config("ORIGIN", cast=Csv(), default="*")
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
name: Shortumation
|
||||
version: v0.1.14
|
||||
slug: shortumation
|
||||
image: asosnovsky/shortumation-{arch}
|
||||
image: ghcr.io/asosnovsky/shortumation-{arch}
|
||||
url: "https://github.com/asosnovsky/Shortumation"
|
||||
description: "A homeassitant addon which provides better UI for automations & scripts."
|
||||
arch:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
version: '3'
|
||||
services:
|
||||
addon:
|
||||
image: "asosnovsky/shortumation-${ARCH:-amd64}:${BUILD_TAG:-edge}"
|
||||
image: "ghcr.io/asosnovsky/shortumation-${ARCH:-amd64}:${BUILD_TAG:-edge}"
|
||||
build:
|
||||
context: "./"
|
||||
dockerfile: "./Dockerfile"
|
||||
|
||||
@@ -3,5 +3,8 @@ services:
|
||||
addon:
|
||||
volumes:
|
||||
- "./sample_config:/config"
|
||||
- "./api/tests:/app/tests"
|
||||
ports:
|
||||
- "8000:8000"
|
||||
env_file:
|
||||
- api/.env
|
||||
@@ -5,8 +5,6 @@ api_location=${1}
|
||||
pushd $api_location
|
||||
echo "--------------------"
|
||||
echo "BUILD_VERSION = "$BUILD_VERSION
|
||||
echo "HASSIO_TOKEN = "$HASSIO_TOKEN
|
||||
echo "SUPERVISOR_TOKEN = "$SUPERVISOR_TOKEN
|
||||
echo $(python --version)
|
||||
echo "--------------------"
|
||||
python -m uvicorn src.app:app --host "0.0.0.0" --port 8000
|
||||
+3
-1
@@ -12,6 +12,7 @@
|
||||
"@types/node": "^17.0.36",
|
||||
"@types/react": "^18.0.9",
|
||||
"@types/react-dom": "^18.0.5",
|
||||
"@types/ws": "^8.5.3",
|
||||
"@uiw/react-codemirror": "^4.7.0",
|
||||
"autosuggest-highlight": "^3.2.1",
|
||||
"home-assistant-js-websocket": "^7.1.0",
|
||||
@@ -27,6 +28,7 @@
|
||||
"theming": "^3.3.0",
|
||||
"typescript": "^4.1.2",
|
||||
"web-vitals": "^2.1.4",
|
||||
"ws": "^8.7.0",
|
||||
"yarn": "^1.22.17"
|
||||
},
|
||||
"scripts": {
|
||||
@@ -94,4 +96,4 @@
|
||||
"node_modules",
|
||||
"src"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,16 @@ export const makeRemoteAPI = (baseURL: string): API => ({
|
||||
data = {}
|
||||
}) {
|
||||
try {
|
||||
const reply = await fetch(baseURL + path, {
|
||||
const payload: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
if (method !== 'GET') {
|
||||
payload["body"] = JSON.stringify(data);
|
||||
}
|
||||
const reply = await fetch(baseURL + path, payload);
|
||||
let response: APIResponse<any>;
|
||||
if (reply.status === 200) {
|
||||
response = {
|
||||
|
||||
@@ -5,13 +5,18 @@ import { useAPIService } from './core';
|
||||
import { useMockAPI } from "./mock";
|
||||
|
||||
const locationPrefixWeb = window.location.pathname.match(/(.+)\/web/i);
|
||||
const baseURL = process.env.NODE_ENV === 'development' ?
|
||||
"http://localhost:8000" :
|
||||
(!locationPrefixWeb ? '' : locationPrefixWeb[1]);
|
||||
|
||||
export const remoteDetailsAPI = makeRemoteAPI(
|
||||
baseURL + "/details"
|
||||
);
|
||||
export const remoteAutoAPI = makeRemoteAPI(
|
||||
baseURL + "/automations"
|
||||
);
|
||||
export const useConnectedApiService = () => useAPIService(
|
||||
makeAutomationAPI(makeRemoteAPI(
|
||||
process.env.NODE_ENV === 'development' ?
|
||||
"http://localhost:8000/automations" :
|
||||
(!locationPrefixWeb ? '' : locationPrefixWeb[1]) + "/automations"
|
||||
))
|
||||
makeAutomationAPI(remoteAutoAPI)
|
||||
);
|
||||
export const useMockApiService = (initialAutos: AutomationData[]) => useAPIService(
|
||||
makeAutomationAPI(useMockAPI(initialAutos))
|
||||
|
||||
@@ -7,7 +7,7 @@ export interface ListParams {
|
||||
}
|
||||
export type APIRequest<D> = {
|
||||
path: string;
|
||||
method?: "POST" | "DELETE",
|
||||
method?: "POST" | "DELETE" | "GET",
|
||||
data?: D;
|
||||
};
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ export const AutoInfoBox: FC<Props> = ({
|
||||
<div className="automation-editor--info-box--tags">
|
||||
<h1>Tags</h1>
|
||||
<div className="automation-editor--info-box--tag-list">
|
||||
{tags.map(([tagName, tagValue], tagIndex) => <div className="automation-editor--info-box--tag">
|
||||
{tags.map(([tagName, tagValue], tagIndex) => <div key={tagIndex} className="automation-editor--info-box--tag">
|
||||
<InputAutoText
|
||||
value={tagName}
|
||||
label="Name"
|
||||
|
||||
@@ -40,8 +40,11 @@ export const InternalPage: FC<{
|
||||
if (snackbarKeyHAConKey.current) {
|
||||
snackbar.closeSnackbar(snackbarKeyHAConKey.current)
|
||||
}
|
||||
snackbarKeyHAConKey.current = snackbar.enqueueSnackbar(`Connected to HA!`, {
|
||||
"variant": "success",
|
||||
})
|
||||
}
|
||||
}, [conn.status])
|
||||
}, [conn, snackbar])
|
||||
|
||||
return <main className="page" style={{
|
||||
color: theme.palette.text.primary,
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import {
|
||||
getAuth,
|
||||
createConnection,
|
||||
ERR_HASS_HOST_REQUIRED,
|
||||
createLongLivedTokenAuth,
|
||||
Auth,
|
||||
Connection,
|
||||
ERR_CANNOT_CONNECT,
|
||||
ERR_CONNECTION_LOST,
|
||||
ERR_INVALID_AUTH,
|
||||
ERR_INVALID_HTTPS_TO_HTTP,
|
||||
createConnection as _createConnection,
|
||||
} from "home-assistant-js-websocket";
|
||||
import { remoteDetailsAPI } from 'apiService';
|
||||
|
||||
const HASS_URL = process.env.REACT_APP_HASS_URL as string;
|
||||
const HASS_TOKEN = process.env.REACT_APP_HASS_TOKEN;
|
||||
const HASS_SUPERVISOR_TOKEN = process.env.REACT_APP_SUPERVISOR_TOKEN as string;
|
||||
export type HAConnection = {
|
||||
status: 'loading'
|
||||
} | {
|
||||
@@ -47,52 +43,81 @@ const translateErrorCodes = (errorCode: number) => {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
const createAuth = async () => {
|
||||
let auth: Auth
|
||||
if (HASS_TOKEN) {
|
||||
return createLongLivedTokenAuth(HASS_URL, HASS_TOKEN);
|
||||
}
|
||||
if (HASS_SUPERVISOR_TOKEN) {
|
||||
return await getAuth({ hassUrl: HASS_URL, authCode: HASS_SUPERVISOR_TOKEN });
|
||||
}
|
||||
try {
|
||||
auth = await getAuth();
|
||||
return auth
|
||||
} catch (err) {
|
||||
if (err !== ERR_HASS_HOST_REQUIRED) {
|
||||
throw err
|
||||
type ReturnedToken = {
|
||||
hassURL: string;
|
||||
longToken: string
|
||||
} | {
|
||||
hassURL: string;
|
||||
hassioToken: string
|
||||
} | {
|
||||
error: any;
|
||||
}
|
||||
const getToken = async (): Promise<ReturnedToken> => {
|
||||
const data = await remoteDetailsAPI.makeCall({
|
||||
"path": "/",
|
||||
"method": "GET",
|
||||
});
|
||||
if (!data.ok) {
|
||||
return {
|
||||
error: {
|
||||
message: "Failed to get token from api.",
|
||||
originalError: data.error,
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
auth = await getAuth({ hassUrl: HASS_URL });
|
||||
return auth
|
||||
} catch (err) {
|
||||
throw err
|
||||
const hassURL = data.data.hass_url;
|
||||
const hassioToken = data.data.hassio_token;
|
||||
if (!hassioToken) {
|
||||
return {
|
||||
error: {
|
||||
message: [
|
||||
"Missing HASSIO Token, Please make sure the token is correctly passing to the container!",
|
||||
"Make sure the token `HASSIO_TOKEN` and `HASS_URL` are set properly."
|
||||
].join('\n'),
|
||||
}
|
||||
}
|
||||
}
|
||||
return { hassioToken, hassURL }
|
||||
}
|
||||
|
||||
const createConnection = async () => {
|
||||
const tokens = await getToken();
|
||||
try {
|
||||
if ('hassioToken' in tokens) {
|
||||
return _createConnection({
|
||||
auth: createLongLivedTokenAuth(tokens.hassURL, tokens.hassioToken),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line
|
||||
throw {
|
||||
originalError: JSON.stringify(error),
|
||||
message: translateErrorCodes(error as any),
|
||||
tokens,
|
||||
}
|
||||
}
|
||||
throw tokens
|
||||
}
|
||||
|
||||
async function startHAConnection() {
|
||||
let connection: Connection;
|
||||
try {
|
||||
const auth = await createAuth();
|
||||
const connection = await createConnection({ auth });
|
||||
haConnection = {
|
||||
status: 'loaded',
|
||||
connection,
|
||||
}
|
||||
return haConnection;
|
||||
connection = await createConnection();
|
||||
} catch (error) {
|
||||
haConnection = {
|
||||
status: 'error',
|
||||
error: {
|
||||
originalError: JSON.stringify(error),
|
||||
message: translateErrorCodes(error as any),
|
||||
hassToken: !HASS_TOKEN ? "no" : "yes",
|
||||
hasSuperToken: !HASS_SUPERVISOR_TOKEN ? "no" : "yes",
|
||||
url: HASS_URL,
|
||||
},
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
haConnection = {
|
||||
status: 'loaded',
|
||||
connection,
|
||||
}
|
||||
return haConnection;
|
||||
}
|
||||
|
||||
startHAConnection().then(console.log).catch(console.error);
|
||||
|
||||
@@ -15,3 +15,11 @@ ReactDOM.render(
|
||||
// to log results (for example: reportWebVitals(console.log))
|
||||
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
||||
reportWebVitals();
|
||||
|
||||
|
||||
// import { createRoot } from 'react-dom/client';
|
||||
// const container = document.getElementById('root');
|
||||
// const root = createRoot(container!);
|
||||
// root.render(<React.StrictMode>
|
||||
// <App />
|
||||
// </React.StrictMode>);
|
||||
+2
-2
@@ -3995,7 +3995,7 @@
|
||||
anymatch "^3.0.0"
|
||||
source-map "^0.6.0"
|
||||
|
||||
"@types/ws@^8.5.1":
|
||||
"@types/ws@^8.5.1", "@types/ws@^8.5.3":
|
||||
version "8.5.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d"
|
||||
integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==
|
||||
@@ -15767,7 +15767,7 @@ ws@^7.4.6:
|
||||
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.8.tgz#ac2729881ab9e7cbaf8787fe3469a48c5c7f636a"
|
||||
integrity sha512-ri1Id1WinAX5Jqn9HejiGb8crfRio0Qgu8+MtL36rlTA6RLsMdWt1Az/19A2Qij6uSHUMphEFaTKa4WG+UNHNw==
|
||||
|
||||
ws@^8.2.3, ws@^8.4.2:
|
||||
ws@^8.2.3, ws@^8.4.2, ws@^8.7.0:
|
||||
version "8.7.0"
|
||||
resolved "https://registry.yarnpkg.com/ws/-/ws-8.7.0.tgz#eaf9d874b433aa00c0e0d8752532444875db3957"
|
||||
integrity sha512-c2gsP0PRwcLFzUiA8Mkr37/MI7ilIlHQxaEAtd0uNMbVMoy8puJyafRlm0bV9MbGSabUPeLrRRaqIBcFcA2Pqg==
|
||||
|
||||
Reference in new issue
Block a user