diff --git a/.gitignore b/.gitignore index cc9f649..5427d4f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ web/node_modules web/.parcel-cache web/dist api/venv +**__pycache__** +*.db* +.coverage diff --git a/api/.vscode/settings.json b/api/.vscode/settings.json index de288e1..1065d2b 100644 --- a/api/.vscode/settings.json +++ b/api/.vscode/settings.json @@ -1,3 +1,21 @@ { - "python.formatting.provider": "black" + "python.formatting.provider": "black", + "sqltools.connections": [ + { + "previewLimit": 50, + "driver": "SQLite", + "name": "ha", + "sqltools.useNodeRuntime": true, + "database": "./tests/samples/home-assistant_v2.db" + } + ], + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "*test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true } \ No newline at end of file diff --git a/api/Makefile b/api/Makefile index 127e6f1..cd61ffd 100644 --- a/api/Makefile +++ b/api/Makefile @@ -41,3 +41,7 @@ clean: find . -type d -name "__pycache__" -exec rm -r {} + find . -type d -name "*tmp" -exec rm -r {} + +run-local: + rm -f web + ln -s ../web/dist web + uvicorn src.api:app \ No newline at end of file diff --git a/api/src/__init__.py b/api/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/src/api/__init__.py b/api/src/api/__init__.py new file mode 100644 index 0000000..c07c459 --- /dev/null +++ b/api/src/api/__init__.py @@ -0,0 +1 @@ +from .app import app diff --git a/api/src/api/app.py b/api/src/api/app.py new file mode 100644 index 0000000..ef5829b --- /dev/null +++ b/api/src/api/app.py @@ -0,0 +1,16 @@ +from fastapi import FastAPI +from fastapi.staticfiles import StaticFiles +from ..env import ROOT_FOLDER +from . import ping, search + +app = FastAPI() +app.mount( + "/web", + StaticFiles( + directory=ROOT_FOLDER / "web", + html=True, + ), + name="web", +) +app.include_router(ping.router, prefix="/ping") +app.include_router(search.router, prefix="/search") diff --git a/api/src/api/ping.py b/api/src/api/ping.py new file mode 100644 index 0000000..eb28461 --- /dev/null +++ b/api/src/api/ping.py @@ -0,0 +1,14 @@ +from fastapi import APIRouter +from datetime import datetime + +from ..env import BUILD_VERSION + +router = APIRouter() + + +@router.get("/ping") +def read_ping(): + return { + "version": BUILD_VERSION, + "current_time": datetime.now(), + } diff --git a/api/src/api/search/__init__.py b/api/src/api/search/__init__.py new file mode 100644 index 0000000..e589d2b --- /dev/null +++ b/api/src/api/search/__init__.py @@ -0,0 +1,33 @@ +from typing import Dict, Optional +from fastapi import APIRouter +from pydantic.main import BaseModel +from src.db import entities, connection + +router = APIRouter() + +db_connection = connection.HassDatabase( + "sqlite:////home/asosnovsky/LinuxProjects/random/Shortumation/api/tests/samples/home-assistant_v2.db" +) + + +class EntityRequest(BaseModel): + domain: Optional[str] = None + attributes: Optional[dict] = None + + +@router.post("/entity_id") +def search( + search_text: str, + offset: int = 0, + limit: int = 30, + filter_objects: Optional[EntityRequest] = EntityRequest(), +): + + return entities.search( + db_connection, + search_text=search_text, + domain_filter=filter_objects.domain, + attributes_filter=filter_objects.attributes, + offset=offset, + limit=limit, + ) diff --git a/api/src/api/search/types.py b/api/src/api/search/types.py new file mode 100644 index 0000000..7ca19ce --- /dev/null +++ b/api/src/api/search/types.py @@ -0,0 +1,9 @@ +from typing import Literal + +SearchObjects = Literal[ + "entity_id", + "device_id", + "domain", + "event_type", + "statistic_id", +] diff --git a/api/src/config.py b/api/src/config.py deleted file mode 100644 index c20b571..0000000 --- a/api/src/config.py +++ /dev/null @@ -1,154 +0,0 @@ -""" -Helper functions for config. -""" -import os -from pathlib import Path -from typing import Optional - -from ruamel.yaml import YAML -from ruamel.yaml.constructor import SafeConstructor - - -_CONFIGURATION_PATH: Optional[Path] = None - - -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." - ) - - -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/db/__init__.py b/api/src/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/src/db.py b/api/src/db/connection.py similarity index 70% rename from api/src/db.py rename to api/src/db/connection.py index 3b2a6e7..75eaf32 100644 --- a/api/src/db.py +++ b/api/src/db/connection.py @@ -8,6 +8,7 @@ class HassDatabase: self.entities = None try: self.engine = create_engine(db_url) + self.connection = self.engine.connect() except Exception as exc: if isinstance(exc, ImportError): raise RuntimeError( @@ -20,3 +21,13 @@ class HassDatabase: @property def db_type(self): return self.url.scheme.split("+")[0] + + def execute_sql(self, sql: str, **args): + resp = self.engine.execute(text(sql), **args) + return resp.fetchall() + + def close(self): + self.connection.close() + + def __del__(self): + self.close() diff --git a/api/src/db/entities.py b/api/src/db/entities.py new file mode 100644 index 0000000..acbe784 --- /dev/null +++ b/api/src/db/entities.py @@ -0,0 +1,59 @@ +import json +from pydantic import BaseModel +from typing import Dict, Iterable, List, Optional, Union +from .connection import HassDatabase + + +class Entity(BaseModel): + entity_id: str + domain: str + attributes: dict + + +def search( + db_connection: HassDatabase, + search_text: str, + offset: int = 0, + limit: int = 100, + domain_filter: Optional[str] = None, + attributes_filter: Optional[Dict[str, Union[str, bool, int, float]]] = None, +) -> Iterable[Entity]: + filter_sql = f"entity_id like '%%{search_text}%%' AND old_state_id is NULL" + if domain_filter: + filter_sql += f" AND `domain` like '%%{domain_filter}%%'" + for entity_id, domain, attributes_str in db_connection.execute_sql( + f""" + SELECT + entity_id, + `domain` , + `attributes` + FROM + states + WHERE + {filter_sql} + LIMIT {offset}, {limit} +""" + ): + attributes: dict = json.loads(attributes_str) + if attributes_filter: + if not _compare_attr(attributes, attributes_filter): + continue + yield Entity( + entity_id=entity_id, + domain=domain, + attributes=attributes, + ) + + +def _compare_attr(attributes: dict, attributes_filter: dict) -> bool: + for attr_name, attr_value in attributes_filter.items(): + if attr_name not in attributes: + return False + if not type(attributes[attr_name]) == type(attr_value): + return False + if isinstance(attr_value, str): + if attributes[attr_name].find(attr_value) < 0: + return False + if attributes[attr_name] != attr_value: + return False + return True diff --git a/api/src/env.py b/api/src/env.py index 3e2722c..496fcdc 100644 --- a/api/src/env.py +++ b/api/src/env.py @@ -1,4 +1,6 @@ from decouple import config +from pathlib import Path +ROOT_FOLDER = Path(__file__).parent.parent BUILD_VERSION = config("BUILD_VERSION", "0.0.0") diff --git a/api/src/main.py b/api/src/main.py deleted file mode 100644 index c4095e5..0000000 --- a/api/src/main.py +++ /dev/null @@ -1,16 +0,0 @@ -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/tests/unittests/__init__.py b/api/tests/unittests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/tests/unittests/test_db.py b/api/tests/unittests/test_db.py new file mode 100644 index 0000000..20db840 --- /dev/null +++ b/api/tests/unittests/test_db.py @@ -0,0 +1,80 @@ +from pathlib import Path +from unittest import TestCase +from src.db.connection import HassDatabase +from src.db import entities + +THIS_FOLDER = Path(__file__).parent +SAMPLE_DB_PATH = THIS_FOLDER.parent / "samples" / "home-assistant_v2.db" +db = HassDatabase(f"sqlite:///{SAMPLE_DB_PATH.absolute()}") + + +class hass_db_tests(TestCase): + def test_it_loads_local_db(self): + self.assertEqual(db.execute_sql("SELECT count(*) FROM states")[0][0], 333_878) + self.assertEqual(db.db_type, "sqlite") + + def test_search_entity_find_by_text(self): + expected = [ + "sensor.temperature_entrance_closet", + "sensor.temperature_office", + "sensor.temperature_kitchen", + "sensor.temperature_mbathroom", + "sensor.temperature_baby_room", + "sensor.temperature_storage_unit", + "sensor.temperature_powder_room", + "sensor.temperature_laundry", + "sensor.temperature_living_room", + "sensor.temperature_ewelink", + "sensor.temperature_main_bedroom", + ] + for entity in entities.search( + db, + search_text="sensor.temperature", + ): + self.assertTrue(expected.count(entity.entity_id) > 0) + self.assertEqual(entity.domain, "sensor") + self.assertEqual(entity.attributes["state_class"], "measurement") + + def test_search_entity_find_by_text_and_domain(self): + expected = [ + "automation.kitchen_island_buttom_press", + "automation.kitchen_button_long_press", + ] + for entity in entities.search( + db, search_text="kitchen", domain_filter="automation" + ): + self.assertTrue(expected.count(entity.entity_id) > 0) + self.assertEqual(entity.domain, "automation") + self.assertEqual(entity.attributes["mode"], "single") + + def test_search_entity_find_by_text_and_attr(self): + expected = [ + "sensor.humidity_kitchen", + "sensor.pressure_kitchen", + "sensor.temperature_kitchen", + "sensor.battery_kitchen_btn", + "sensor.battery_weather_kitchen", + "sensor.battery_wleak_kitchen", + ] + for entity in entities.search( + db, search_text="kitchen", attributes_filter={"state_class": "measurement"} + ): + self.assertTrue(expected.count(entity.entity_id) > 0) + self.assertEqual(entity.domain, "sensor") + self.assertEqual(entity.attributes["state_class"], "measurement") + + def test_search_entity_find_by_text_and_partial_attr(self): + expected = [ + "sensor.humidity_kitchen", + "sensor.pressure_kitchen", + "sensor.temperature_kitchen", + "sensor.battery_kitchen_btn", + "sensor.battery_weather_kitchen", + "sensor.battery_wleak_kitchen", + ] + for entity in entities.search( + db, search_text="kitchen", attributes_filter={"state_class": "measurement"} + ): + self.assertTrue(expected.count(entity.entity_id) > 0) + self.assertEqual(entity.domain, "sensor") + self.assertEqual(entity.attributes["state_class"], "sureme") diff --git a/web/src/tooltip/context.ts b/web/src/tooltip/context.ts index eff2e5e..e1c41d2 100644 --- a/web/src/tooltip/context.ts +++ b/web/src/tooltip/context.ts @@ -2,7 +2,7 @@ import { createContext, useContext } from "react" export interface TooltipSearchOption { searchObject: string; - textInput: string; + searchText: string; filterObjects: Record; } export interface TooltipState {