From e87c41f496977455a2ab6398205c9cf97af539b0 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 9 Nov 2021 21:38:17 -0500 Subject: [PATCH] initial api --- api/.vscode/settings.json | 3 + api/Makefile | 43 +++++ api/setup.py | 27 +++ api/src/__pycache__/env.cpython-39.pyc | Bin 0 -> 234 bytes api/src/__pycache__/main.cpython-39.pyc | Bin 0 -> 585 bytes api/src/config.py | 154 ++++++++++++++++++ api/src/config/__init__.py | 48 ++++++ api/src/config/configuration_parser.py | 105 ++++++++++++ api/src/config/globals.py | 6 + api/src/db.py | 22 +++ api/src/env.py | 4 + api/src/main.py | 16 ++ api/web | 1 + samples/01/config/automations.yaml | 27 +++ web/package.json | 2 +- .../automation/StateManager/ScriptDevice.tsx | 2 +- 16 files changed, 458 insertions(+), 2 deletions(-) create mode 100644 api/.vscode/settings.json create mode 100644 api/Makefile create mode 100644 api/setup.py create mode 100644 api/src/__pycache__/env.cpython-39.pyc create mode 100644 api/src/__pycache__/main.cpython-39.pyc create mode 100644 api/src/config.py create mode 100644 api/src/config/__init__.py create mode 100644 api/src/config/configuration_parser.py create mode 100644 api/src/config/globals.py create mode 100644 api/src/db.py create mode 100644 api/src/env.py create mode 100644 api/src/main.py create mode 120000 api/web create mode 100644 samples/01/config/automations.yaml diff --git a/api/.vscode/settings.json b/api/.vscode/settings.json new file mode 100644 index 0000000..de288e1 --- /dev/null +++ b/api/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "black" +} \ No newline at end of file diff --git a/api/Makefile b/api/Makefile new file mode 100644 index 0000000..127e6f1 --- /dev/null +++ b/api/Makefile @@ -0,0 +1,43 @@ +VENV_FOLDER=./venv +VENV_BIN=$(VENV_FOLDER)/bin +PIP=$(VENV_BIN)/pip +PYTHON=$(VENV_BIN)/python + +setup: + rm -rf $(VENV_FOLDER) + python3 -m venv $(VENV_FOLDER) + $(PIP) install wheel==0.35.1 + $(PIP) install .[dev,boto] + $(VENV_BIN)/pre-commit install + +lint: + $(VENV_BIN)/vulture --exclude src + $(MAKE) lint-project + $(MAKE) lint-tests + +lint-project: + $(VENV_BIN)/black src + $(VENV_BIN)/isort --trailing-comma src + $(VENV_BIN)/mypy src + + +lint-tests: + $(VENV_BIN)/black tests + $(VENV_BIN)/isort --trailing-comma tests + $(VENV_BIN)/mypy tests + +security-checks: + $(VENV_BIN)/bandit -r src + +test: + $(PYTHON) -m unittest discover tests + +coverage: + $(VENV_BIN)/coverage run -m unittest discover tests + $(VENV_BIN)/coverage report -m --fail-under=90 + +clean: + rm -rf dist *.sqlite *.egg-info build .mypy_cache .coverage + find . -type d -name "__pycache__" -exec rm -r {} + + find . -type d -name "*tmp" -exec rm -r {} + + diff --git a/api/setup.py b/api/setup.py new file mode 100644 index 0000000..7556f28 --- /dev/null +++ b/api/setup.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +import setuptools # type: ignore + +setuptools.setup( + python_requires=">=3.7", + install_requires=[ + "ruamel.yaml==0.17.17", + "SQLAlchemy==1.4.26", + "fastapi==0.70.0", + "pydantic==1.8.2", + "uvicorn[standard]==0.15.0", + "python-decouple==3.5", + ], + extras_require={ + "dev": [ + "mypy==0.910", + "mypy-extensions==0.4.3", + "black==21.7b0", + "bandit==1.7.0", + "isort==5.9.3", + "coverage==5.5", + "pre-commit==2.13.0", + "vulture==2.3", + "types-requests==2.25.6", + ], + }, +) diff --git a/api/src/__pycache__/env.cpython-39.pyc b/api/src/__pycache__/env.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..87f7cd5db0fa836edebac96d3e332c7d19994c2c GIT binary patch literal 234 zcmYe~<>g`kf@uBjM1LUt7{oya%s`F<5En}Ui4=wu#vF!R#wbQchE&EBrWEF8rYNQq zmS6@=)|Wte22I9WY{~g~X_@J_c%4E$eO%(hT!Vr={r##~4fKG}Pm?){BPBICzqBAH zwTKC*qX=ZdN`@j95F14Na@5br&rQ`&EY2^^%P%X=uGII*%qy)3D9X=DO)e?cFG|cy z$fPt7aSE2zB1VUwGmQks)$#|SjA7-S6(GXSnE BIYIyc literal 0 HcmV?d00001 diff --git a/api/src/__pycache__/main.cpython-39.pyc b/api/src/__pycache__/main.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9c636fc840eb83bd12f6855719cb393466b86631 GIT binary patch literal 585 zcmYjNJ8s-C5ama|-Id$~@Ez1FFAxOzI0hClHij2Y!Dc~=v@J&@1(I54-P&E?8>Fzy z+#qLBo61*65oEYA5Rbrzc|#57%@b8MMG!yPH*ty)`W4B`NP)bCKQ!h<2*zO&i=Tlby?4NehzLy zqa#q@ub$Z)YGB-XG|t8So>eNd*@+&h=zE@OJD4!i3l( Path: + """Try to find HASS config.""" + if "HASSIO_TOKEN" in os.environ: + return Path("/config") + + """Put together the default configuration directory based on the OS.""" + data_dir = Path( + os.getenv("APPDATA") if os.name == "nt" else os.path.expanduser("~") + ) + config_dir = data_dir / ".homeassistant" + + if config_dir.is_dir(): + return config_dir + + raise ValueError( + "Unable to automatically find the location of Home Assistant " + "config. Please pass it in." + ) + + +class HassSafeConstructor(SafeConstructor): + """Hass specific SafeConstructor.""" + + +def _secret_yaml(loader, node): + """Load secrets and embed it into the configuration YAML.""" + dirpath = Path(os.path.dirname(loader.name)) + + # Recursively search for the secrets.yaml file + # this might be needed when a yaml file is included like + # `!included folder/file.yaml`. Same rules as + # https://www.home-assistant.io/docs/configuration/secrets/#debugging-secrets + fname = dirpath / "secrets.yaml" + for _ in range(len(dirpath.parts)): + if fname.exists() or ( + _CONFIGURATION_PATH is not None + and _CONFIGURATION_PATH.exists() + and dirpath.samefile(_CONFIGURATION_PATH) + ): + break + dirpath = dirpath.parent + fname = dirpath / "secrets.yaml" + + try: + with fname.open() as secret_file: + secrets = YAML(typ="safe").load(secret_file) + except FileNotFoundError: + raise ValueError("Secrets file {} not found".format(fname)) from None + + try: + return secrets[node.value] + except KeyError: + raise ValueError("Secret {} not found".format(node.value)) from None + + +def _include_yaml(loader, node): + """Load another YAML file and embeds it using the !include tag. + + Example: + device_tracker: !include device_tracker.yaml + """ + dirpath = Path(os.path.dirname(loader.name)) + fname = dirpath / node.value + for _ in range(len(dirpath.parts)): + if fname.exists() or ( + _CONFIGURATION_PATH is not None + and _CONFIGURATION_PATH.exists() + and dirpath.samefile(_CONFIGURATION_PATH) + ): + break + dirpath = dirpath.parent + fname = dirpath / node.value + + return load_yaml(os.path.join(dirpath, node.value)) + + +def _stub_tag(constructor, node): + """Stub a constructor with a dictionary.""" + seen = getattr(constructor, "_stub_seen", None) + + if seen is None: + seen = constructor._stub_seen = set() + + if node.tag not in seen: + print("YAML tag {} is not supported".format(node.tag)) + seen.add(node.tag) + + return {} + + +HassSafeConstructor.add_constructor("!include", _include_yaml) +HassSafeConstructor.add_constructor("!env_var", _stub_tag) +HassSafeConstructor.add_constructor("!secret", _secret_yaml) +HassSafeConstructor.add_constructor("!include_dir_list", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_merge_list", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_named", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_merge_named", _stub_tag) + + +def load_hass_config(path): + """Load the HASS config.""" + return load_yaml(os.path.join(path, "configuration.yaml")) + + +def load_yaml(fname): + """Load a YAML file.""" + yaml = YAML(typ="safe") + # Compat with HASS + yaml.allow_duplicate_keys = True + # Stub HASS constructors + HassSafeConstructor.name = fname + yaml.Constructor = HassSafeConstructor + + with open(fname, encoding="utf-8") as conf_file: + # If configuration file is empty YAML returns None + # We convert that to an empty dict + return yaml.load(conf_file) or {} + + +def db_url_from_hass_config(path): + """Find the recorder database url from a HASS config dir.""" + global _CONFIGURATION_PATH + _CONFIGURATION_PATH = Path(path).resolve() + config = load_hass_config(path) + default_path = os.path.join(path, "home-assistant_v2.db") + default_url = "sqlite:///{}".format(default_path) + + recorder = config.get("recorder") + + if recorder: + db_url = recorder.get("db_url") + if db_url is not None: + return db_url + + if not os.path.isfile(default_path): + raise ValueError( + "Unable to determine DB url from hass config at {}".format(path) + ) + + return default_url diff --git a/api/src/config/__init__.py b/api/src/config/__init__.py new file mode 100644 index 0000000..614a88f --- /dev/null +++ b/api/src/config/__init__.py @@ -0,0 +1,48 @@ +import os +from pathlib import Path +from .globals import CONFIGURATION_PATH +from .configuration_parser import load_hass_config + + +def find_hass_config() -> Path: + """Try to find HASS config.""" + if "HASSIO_TOKEN" in os.environ: + return Path("/config") + + """Put together the default configuration directory based on the OS.""" + data_dir = Path( + os.getenv("APPDATA") if os.name == "nt" else os.path.expanduser("~") + ) + config_dir = data_dir / ".homeassistant" + + if config_dir.is_dir(): + return config_dir + + raise ValueError( + "Unable to automatically find the location of Home Assistant " + "config. Please pass it in." + ) + + +def db_url_from_hass_config(path: Path): + """Find the recorder database url from a HASS config dir.""" + global CONFIGURATION_PATH + CONFIGURATION_PATH = path.resolve() + config = load_hass_config(path) + + recorder = config.get("recorder") + + if recorder: + db_url = recorder.get("db_url") + if db_url is not None: + return db_url + + default_path = path / "home-assistant_v2.db" + default_url = f"sqlite:///{default_path.absolute()}" + + if not os.path.isfile(default_path): + raise ValueError( + "Unable to determine DB url from hass config at {}".format(path) + ) + + return default_url diff --git a/api/src/config/configuration_parser.py b/api/src/config/configuration_parser.py new file mode 100644 index 0000000..074e3a6 --- /dev/null +++ b/api/src/config/configuration_parser.py @@ -0,0 +1,105 @@ +import os + +from pathlib import Path +from ruamel.yaml import YAML +from ruamel.yaml.constructor import SafeConstructor +from .globals import CONFIGURATION_PATH + + +class HassSafeConstructor(SafeConstructor): + """Hass specific SafeConstructor.""" + + +def _secret_yaml(loader, node): + """Load secrets and embed it into the configuration YAML.""" + dirpath = Path(os.path.dirname(loader.name)) + + # Recursively search for the secrets.yaml file + # this might be needed when a yaml file is included like + # `!included folder/file.yaml`. Same rules as + # https://www.home-assistant.io/docs/configuration/secrets/#debugging-secrets + fname = dirpath / "secrets.yaml" + for _ in range(len(dirpath.parts)): + if fname.exists() or ( + CONFIGURATION_PATH is not None + and CONFIGURATION_PATH.exists() + and dirpath.samefile(CONFIGURATION_PATH) + ): + break + dirpath = dirpath.parent + fname = dirpath / "secrets.yaml" + + try: + with fname.open() as secret_file: + secrets = YAML(typ="safe").load(secret_file) + except FileNotFoundError: + raise ValueError("Secrets file {} not found".format(fname)) from None + + try: + return secrets[node.value] + except KeyError: + raise ValueError("Secret {} not found".format(node.value)) from None + + +def _include_yaml(loader, node): + """Load another YAML file and embeds it using the !include tag. + + Example: + device_tracker: !include device_tracker.yaml + """ + dirpath = Path(os.path.dirname(loader.name)) + fname = dirpath / node.value + for _ in range(len(dirpath.parts)): + if fname.exists() or ( + CONFIGURATION_PATH is not None + and CONFIGURATION_PATH.exists() + and dirpath.samefile(CONFIGURATION_PATH) + ): + break + dirpath = dirpath.parent + fname = dirpath / node.value + + return load_yaml(os.path.join(dirpath, node.value)) + + +def _stub_tag(constructor, node): + """Stub a constructor with a dictionary.""" + seen = getattr(constructor, "_stub_seen", None) + + if seen is None: + seen = constructor._stub_seen = set() + + if node.tag not in seen: + print("YAML tag {} is not supported".format(node.tag)) + seen.add(node.tag) + + return {} + + +HassSafeConstructor.add_constructor("!include", _include_yaml) +HassSafeConstructor.add_constructor("!env_var", _stub_tag) +HassSafeConstructor.add_constructor("!secret", _secret_yaml) +HassSafeConstructor.add_constructor("!include_dir_list", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_merge_list", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_named", _stub_tag) +HassSafeConstructor.add_constructor("!include_dir_merge_named", _stub_tag) + + +def load_hass_config(dir_path: Path): + """Load the HASS config.""" + return load_yaml(dir_path / "configuration.yaml") + + +def load_yaml(file_path: Path): + """Load a YAML file.""" + yaml = YAML(typ="safe") + # Compat with HASS + yaml.allow_duplicate_keys = True + # Stub HASS constructors + HassSafeConstructor.name = str(file_path.absolute()) + yaml.Constructor = HassSafeConstructor + + with file_path.open(encoding="utf-8") as conf_file: + # If configuration file is empty YAML returns None + # We convert that to an empty dict + return yaml.load(conf_file) or {} diff --git a/api/src/config/globals.py b/api/src/config/globals.py new file mode 100644 index 0000000..c820f2e --- /dev/null +++ b/api/src/config/globals.py @@ -0,0 +1,6 @@ +from pathlib import Path +from typing import Optional + + +# TODO: get rid of this +CONFIGURATION_PATH: Optional[Path] = None diff --git a/api/src/db.py b/api/src/db.py new file mode 100644 index 0000000..3b2a6e7 --- /dev/null +++ b/api/src/db.py @@ -0,0 +1,22 @@ +from sqlalchemy import create_engine, text +from urllib.parse import urlparse + + +class HassDatabase: + def __init__(self, db_url: str) -> None: + self.url = urlparse(db_url) + self.entities = None + try: + self.engine = create_engine(db_url) + except Exception as exc: + if isinstance(exc, ImportError): + raise RuntimeError( + "The right dependency to connect to your database is " + "missing. Please make sure that it is installed." + ) from exc + + raise + + @property + def db_type(self): + return self.url.scheme.split("+")[0] diff --git a/api/src/env.py b/api/src/env.py new file mode 100644 index 0000000..3e2722c --- /dev/null +++ b/api/src/env.py @@ -0,0 +1,4 @@ +from decouple import config + + +BUILD_VERSION = config("BUILD_VERSION", "0.0.0") diff --git a/api/src/main.py b/api/src/main.py new file mode 100644 index 0000000..c4095e5 --- /dev/null +++ b/api/src/main.py @@ -0,0 +1,16 @@ +from fastapi import FastAPI +from fastapi.staticfiles import StaticFiles +from datetime import datetime + +from .env import BUILD_VERSION + +app = FastAPI() +app.mount("/web", StaticFiles(directory="web"), name="web") + + +@app.get("/ping") +def read_ping(): + return { + "version": BUILD_VERSION, + "current_time": datetime.now(), + } diff --git a/api/web b/api/web new file mode 120000 index 0000000..4315e28 --- /dev/null +++ b/api/web @@ -0,0 +1 @@ +../web/dist \ No newline at end of file diff --git a/samples/01/config/automations.yaml b/samples/01/config/automations.yaml new file mode 100644 index 0000000..7939ba3 --- /dev/null +++ b/samples/01/config/automations.yaml @@ -0,0 +1,27 @@ +- id: "1616887975473" + alias: Home Assistant Off + description: "" + trigger: + - platform: homeassistant + event: shutdown + condition: [] + action: + - device_id: 2a98db84af7526c7ec3cc7aebd1a9e1c + domain: mobile_app + type: notify + title: Hassio Status + message: Hassio Is turning off... + mode: single +- id: "1616888026264" + alias: Home Assistant On + description: "" + trigger: + - platform: homeassistant + event: start + condition: [] + action: + - device_id: 2a98db84af7526c7ec3cc7aebd1a9e1c + domain: mobile_app + type: notify + title: Hassio Status + message: Home assistant is up! diff --git a/web/package.json b/web/package.json index 01442dd..aed1dca 100644 --- a/web/package.json +++ b/web/package.json @@ -32,7 +32,7 @@ "scripts": { "develop": "concurrently -r \"npm:type-check:watch\" \"npm:start\"", "start": "parcel ./src/index.html", - "build": "npm run type-check && parcel build ./src/index.html --public-url '/'", + "build": "npm run type-check && parcel build ./src/index.html --public-url '/web'", "prettify": "prettier --write './src/**/*.{ts,tsx}'", "test": "jest", "test:watch": "jest --watch", diff --git a/web/src/automation/StateManager/ScriptDevice.tsx b/web/src/automation/StateManager/ScriptDevice.tsx index dd6f1b1..6015ca8 100644 --- a/web/src/automation/StateManager/ScriptDevice.tsx +++ b/web/src/automation/StateManager/ScriptDevice.tsx @@ -43,7 +43,7 @@ export default class ScriptDeviceState extends BaseState { this.setState({...state, entity_id, domain: entity_id.split('.')[0]})} />