Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c27f2d13f0 | ||
|
|
26c81effd6 | ||
|
|
e72c83b980 | ||
|
|
e222c42150 | ||
|
|
e7b88fc6f6 | ||
|
|
079f353dbf | ||
|
|
e6e51be5a5 | ||
|
|
3a6b21a117 | ||
|
|
f42df5a70f | ||
|
|
0c3e9b5ace |
No files matched your search
@@ -100,5 +100,6 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
args: |
|
args: |
|
||||||
--${{ matrix.arch }} \
|
--${{ matrix.arch }} \
|
||||||
|
--no-latest \
|
||||||
--version ${{ needs.metadata.outputs.version }} \
|
--version ${{ needs.metadata.outputs.version }} \
|
||||||
--target .
|
--target .
|
||||||
@@ -9,9 +9,7 @@ WORKDIR /app
|
|||||||
ARG BUILD_VERSION
|
ARG BUILD_VERSION
|
||||||
|
|
||||||
# <--- Environment Variables -->
|
# <--- Environment Variables -->
|
||||||
ENV HASSIO_TOKEN ""
|
|
||||||
ENV BUILD_VERSION $BUILD_VERSION
|
ENV BUILD_VERSION $BUILD_VERSION
|
||||||
ENV HASS_URL "http://supervisor/"
|
|
||||||
|
|
||||||
# <--- System Wide Dependencies -->
|
# <--- System Wide Dependencies -->
|
||||||
ENV LANG C.UTF-8
|
ENV LANG C.UTF-8
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ push:
|
|||||||
$(DC) push
|
$(DC) push
|
||||||
|
|
||||||
start:
|
start:
|
||||||
$(MAKE) build
|
|
||||||
$(DC) up -d
|
$(DC) up -d
|
||||||
sleep 1
|
sleep 1
|
||||||
$(DC) ps
|
$(DC) ps
|
||||||
|
|||||||
+13
-16
@@ -1,17 +1,14 @@
|
|||||||
list_autos # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:16)
|
list_autos # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:17)
|
||||||
upsert_auto # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:27)
|
upsert_auto # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:28)
|
||||||
delete_auto # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:31)
|
delete_auto # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/automations.py:32)
|
||||||
read_ping # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/details.py:12)
|
read_details # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/details.py:10)
|
||||||
read_ping # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/ping.py:9)
|
read_ping # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/ping.py:10)
|
||||||
params # unused variable (/home/ari/Projects/Shortumation/api/src/api/types.py:16)
|
websocket_endpoint # unused function (/home/ari/Projects/Shortumation/api/src/api/routes/socket.py:9)
|
||||||
total_items # unused variable (/home/ari/Projects/Shortumation/api/src/api/types.py:17)
|
params # unused variable (/home/ari/Projects/Shortumation/api/src/api/types.py:18)
|
||||||
id # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:6)
|
total_items # unused variable (/home/ari/Projects/Shortumation/api/src/api/types.py:19)
|
||||||
alias # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:7)
|
id # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:8)
|
||||||
description # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:8)
|
alias # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:9)
|
||||||
trigger_variables # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:9)
|
description # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:10)
|
||||||
mode # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:10)
|
trigger_variables # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:11)
|
||||||
|
mode # unused variable (/home/ari/Projects/Shortumation/api/src/automations/types.py:12)
|
||||||
json_dumps # unused function (/home/ari/Projects/Shortumation/api/src/json_serializer.py:5)
|
json_dumps # unused function (/home/ari/Projects/Shortumation/api/src/json_serializer.py:5)
|
||||||
logger # unused variable (/home/ari/Projects/Shortumation/api/src/logger.py:16)
|
|
||||||
_.to_yaml # unused method (/home/ari/Projects/Shortumation/api/src/yaml_serializer/types.py:13)
|
|
||||||
_.to_yaml # unused method (/home/ari/Projects/Shortumation/api/src/yaml_serializer/types.py:30)
|
|
||||||
_.to_yaml # unused method (/home/ari/Projects/Shortumation/api/src/yaml_serializer/types.py:54)
|
|
||||||
+1
-1
@@ -33,7 +33,7 @@ test:
|
|||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
$(VENV_BIN)/coverage run -m unittest discover tests
|
$(VENV_BIN)/coverage run -m unittest discover tests
|
||||||
$(VENV_BIN)/coverage report -m --fail-under=90
|
$(VENV_BIN)/coverage report -m --fail-under=85
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf dist *.sqlite *.egg-info build .mypy_cache .coverage
|
rm -rf dist *.sqlite *.egg-info build .mypy_cache .coverage
|
||||||
|
|||||||
+10
-5
@@ -3,16 +3,16 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from src.automations.manager import AutomationManager
|
from src.automations.manager import AutomationManager
|
||||||
from src.env import ORIGIN, ROOT_FOLDER
|
from src.env import API_PREFIX, ORIGIN, ROOT_FOLDER
|
||||||
|
|
||||||
from .routes import automations, details, ping
|
from .routes import automations, details, ping, socket
|
||||||
|
|
||||||
|
|
||||||
def make_app(automation_mgr: AutomationManager) -> FastAPI:
|
def make_app(automation_mgr: AutomationManager) -> FastAPI:
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
if (ROOT_FOLDER / "web").exists():
|
if (ROOT_FOLDER / "web").exists():
|
||||||
app.mount(
|
app.mount(
|
||||||
"/web",
|
f"{API_PREFIX}/web",
|
||||||
StaticFiles(
|
StaticFiles(
|
||||||
directory=ROOT_FOLDER / "web",
|
directory=ROOT_FOLDER / "web",
|
||||||
html=True,
|
html=True,
|
||||||
@@ -29,6 +29,11 @@ def make_app(automation_mgr: AutomationManager) -> FastAPI:
|
|||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
app.include_router(ping.router, prefix="/ping")
|
app.include_router(ping.router, prefix="/ping")
|
||||||
app.include_router(automations.make_automation_router(automation_mgr), prefix="/automations")
|
app.include_router(
|
||||||
app.include_router(details.make_details_router(automation_mgr.hass_config), prefix="/details")
|
automations.make_automation_router(automation_mgr), prefix=f"{API_PREFIX}/automations"
|
||||||
|
)
|
||||||
|
app.include_router(
|
||||||
|
details.make_details_router(automation_mgr.hass_config), prefix=f"{API_PREFIX}/details"
|
||||||
|
)
|
||||||
|
app.include_router(socket.router, prefix=f"{API_PREFIX}/socket")
|
||||||
return app
|
return app
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from src.env import BUILD_VERSION, HASS_URL, HASSIO_TOKEN
|
from src.env import BUILD_VERSION, HASSIO_TOKEN, HASSIO_WS
|
||||||
from src.hass_config import HassConfig
|
from src.hass_config import HassConfig
|
||||||
|
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ def make_details_router(hass_config: HassConfig) -> APIRouter:
|
|||||||
return {
|
return {
|
||||||
"version": BUILD_VERSION,
|
"version": BUILD_VERSION,
|
||||||
"hassio_token": HASSIO_TOKEN,
|
"hassio_token": HASSIO_TOKEN,
|
||||||
"hass_url": HASS_URL,
|
"hassio_url": HASSIO_WS,
|
||||||
"config": hass_config.get_configuration_path(),
|
"config": hass_config.get_configuration_path(),
|
||||||
"tags": hass_config.get_automation_tags_path(),
|
"tags": hass_config.get_automation_tags_path(),
|
||||||
"autos": hass_config.get_automation_path(),
|
"autos": hass_config.get_automation_path(),
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
|
from fastapi import APIRouter, WebSocket
|
||||||
|
|
||||||
|
from src.ws_redirect import WSRedirector
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.websocket("/api/websocket")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
|
async def on_message(msg: str):
|
||||||
|
await websocket.send_text(msg)
|
||||||
|
|
||||||
|
conn = WSRedirector(
|
||||||
|
on_message=on_message,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def wait_for_data():
|
||||||
|
while True:
|
||||||
|
await conn.send(await websocket.receive_text())
|
||||||
|
|
||||||
|
await websocket.accept()
|
||||||
|
|
||||||
|
await asyncio.gather(
|
||||||
|
conn.start(),
|
||||||
|
wait_for_data(),
|
||||||
|
)
|
||||||
+3
-2
@@ -4,7 +4,8 @@ from decouple import Csv, config
|
|||||||
|
|
||||||
ROOT_FOLDER = Path(__file__).parent.parent.absolute()
|
ROOT_FOLDER = Path(__file__).parent.parent.absolute()
|
||||||
BUILD_VERSION = config("BUILD_VERSION", "0.0.0")
|
BUILD_VERSION = config("BUILD_VERSION", "0.0.0")
|
||||||
HASSIO_TOKEN = config("HASSIO_TOKEN", "")
|
HASSIO_TOKEN = config("SUPERVISOR_TOKEN", "")
|
||||||
HASS_URL = config("HASS_URL", "/")
|
HASSIO_WS = config("HASSIO_WS", "ws://supervisor/core/websocket")
|
||||||
LOG_LEVEL = config("LOG_LEVEL", "INFO")
|
LOG_LEVEL = config("LOG_LEVEL", "INFO")
|
||||||
|
API_PREFIX = config("API_PREFIX", "")
|
||||||
ORIGIN = config("ORIGIN", cast=Csv(), default="*")
|
ORIGIN = config("ORIGIN", cast=Csv(), default="*")
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import json
|
||||||
|
from typing import Any, Callable, Coroutine, Optional
|
||||||
|
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
from src.env import HASSIO_TOKEN, HASSIO_WS
|
||||||
|
from src.logger import logger
|
||||||
|
|
||||||
|
OnMessage = Callable[[str], Coroutine]
|
||||||
|
|
||||||
|
|
||||||
|
class WSRedirector:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
on_message: OnMessage,
|
||||||
|
token: str = HASSIO_TOKEN,
|
||||||
|
host: str = HASSIO_WS,
|
||||||
|
) -> None:
|
||||||
|
self._host = host
|
||||||
|
self._token = token
|
||||||
|
self._on_message = on_message
|
||||||
|
self._client: Optional[Any] = None
|
||||||
|
self._close: bool = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_closed(self):
|
||||||
|
return self._close
|
||||||
|
|
||||||
|
async def send(self, msg: str):
|
||||||
|
if self._client:
|
||||||
|
return await self._client.send(msg)
|
||||||
|
|
||||||
|
async def start(self):
|
||||||
|
async with websockets.connect(self._host) as websocket:
|
||||||
|
self._client = websocket
|
||||||
|
await websocket.send(json.dumps({"type": "auth", "access_token": self._token}))
|
||||||
|
while not self.is_closed:
|
||||||
|
msg = await websocket.recv()
|
||||||
|
logger.debug(f"WS:MSG: {msg}")
|
||||||
|
await self.process_message(msg)
|
||||||
|
|
||||||
|
async def process_message(self, msg: str):
|
||||||
|
await self._on_message(msg)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._close = True
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.close()
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
name: Shortumation
|
name: Shortumation
|
||||||
version: v0.1.14
|
version: v0.1.14
|
||||||
slug: shortumation
|
slug: shortumation
|
||||||
image: ghcr.io/asosnovsky/shortumation-{arch}
|
image: asosnovsky/shortumation-{arch}
|
||||||
url: "https://github.com/asosnovsky/Shortumation"
|
url: "https://github.com/asosnovsky/Shortumation"
|
||||||
description: "A homeassitant addon which provides better UI for automations & scripts."
|
description: "A homeassitant addon which provides better UI for automations & scripts."
|
||||||
arch:
|
arch:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
version: '3'
|
version: '3'
|
||||||
services:
|
services:
|
||||||
addon:
|
addon:
|
||||||
image: "ghcr.io/asosnovsky/shortumation-${ARCH:-amd64}:${BUILD_TAG:-edge}"
|
image: "asosnovsky/shortumation-${ARCH:-amd64}:${BUILD_TAG:-edge}"
|
||||||
build:
|
build:
|
||||||
context: "./"
|
context: "./"
|
||||||
dockerfile: "./Dockerfile"
|
dockerfile: "./Dockerfile"
|
||||||
|
|||||||
@@ -4,16 +4,16 @@ import { makeRemoteAPI } from "./base";
|
|||||||
import { useAPIService } from './core';
|
import { useAPIService } from './core';
|
||||||
import { useMockAPI } from "./mock";
|
import { useMockAPI } from "./mock";
|
||||||
|
|
||||||
const locationPrefixWeb = window.location.pathname.match(/(.+)\/web/i);
|
const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i);
|
||||||
const baseURL = process.env.NODE_ENV === 'development' ?
|
const baseURL = new URL(
|
||||||
"http://localhost:8000" :
|
process.env.NODE_ENV === 'development' ?
|
||||||
(!locationPrefixWeb ? '' : locationPrefixWeb[1]);
|
"http://localhost:8000" :
|
||||||
|
window.location.origin + (!locationPrefixWeb ? '' : locationPrefixWeb[1])
|
||||||
export const remoteDetailsAPI = makeRemoteAPI(
|
|
||||||
baseURL + "/details"
|
|
||||||
);
|
);
|
||||||
|
export const wsURL = baseURL + "socket"
|
||||||
|
console.log({ baseURL, wsURL })
|
||||||
export const remoteAutoAPI = makeRemoteAPI(
|
export const remoteAutoAPI = makeRemoteAPI(
|
||||||
baseURL + "/automations"
|
baseURL + "automations"
|
||||||
);
|
);
|
||||||
export const useConnectedApiService = () => useAPIService(
|
export const useConnectedApiService = () => useAPIService(
|
||||||
makeAutomationAPI(remoteAutoAPI)
|
makeAutomationAPI(remoteAutoAPI)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
ERR_INVALID_HTTPS_TO_HTTP,
|
ERR_INVALID_HTTPS_TO_HTTP,
|
||||||
createConnection as _createConnection,
|
createConnection as _createConnection,
|
||||||
} from "home-assistant-js-websocket";
|
} from "home-assistant-js-websocket";
|
||||||
import { remoteDetailsAPI } from 'apiService';
|
import { wsURL } from 'apiService';
|
||||||
|
|
||||||
export type HAConnection = {
|
export type HAConnection = {
|
||||||
status: 'loading'
|
status: 'loading'
|
||||||
@@ -43,60 +43,18 @@ const translateErrorCodes = (errorCode: number) => {
|
|||||||
return errorCode;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 createConnection = async () => {
|
||||||
const tokens = await getToken();
|
|
||||||
try {
|
try {
|
||||||
if ('hassioToken' in tokens) {
|
return _createConnection({
|
||||||
return _createConnection({
|
auth: createLongLivedTokenAuth(wsURL, 'xxx'),
|
||||||
auth: createLongLivedTokenAuth(tokens.hassURL, tokens.hassioToken),
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
throw {
|
throw {
|
||||||
originalError: JSON.stringify(error),
|
originalError: JSON.stringify(error),
|
||||||
message: translateErrorCodes(error as any),
|
message: translateErrorCodes(error as any),
|
||||||
tokens,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw tokens
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startHAConnection() {
|
async function startHAConnection() {
|
||||||
|
|||||||
Reference in new issue
Block a user