initial api work
This commit is contained in:
1 parent
00505c0faa
commit
fbbfee8873
15 files changed
+206
-222
No files matched your search
@@ -23,6 +23,7 @@ setuptools.setup(
|
|||||||
"pre-commit==2.13.0",
|
"pre-commit==2.13.0",
|
||||||
"vulture==2.3",
|
"vulture==2.3",
|
||||||
"types-requests==2.25.6",
|
"types-requests==2.25.6",
|
||||||
|
"requests==2.27.0",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -1 +0,0 @@
|
|||||||
from .app import app
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from src.automations.manager import AutomationManager
|
||||||
|
from src.hass_config import find_hass_config
|
||||||
|
from .app import make_app
|
||||||
|
|
||||||
|
hass_config = find_hass_config()
|
||||||
|
app = make_app(AutomationManager(hass_config))
|
||||||
+17
-13
@@ -1,17 +1,21 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from src.automations.manager import AutomationManager
|
||||||
from src.env import ROOT_FOLDER
|
from src.env import ROOT_FOLDER
|
||||||
from .routes import ping, search, automations
|
from .routes import ping, automations
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.mount(
|
def make_app(automation_mgr: AutomationManager) -> FastAPI:
|
||||||
"/web",
|
app = FastAPI()
|
||||||
StaticFiles(
|
# app.mount(
|
||||||
directory=ROOT_FOLDER / "web",
|
# "/web",
|
||||||
html=True,
|
# StaticFiles(
|
||||||
),
|
# directory=ROOT_FOLDER / "web",
|
||||||
name="web",
|
# html=True,
|
||||||
)
|
# ),
|
||||||
app.include_router(ping.router, prefix="/ping")
|
# name="web",
|
||||||
app.include_router(search.router, prefix="/search")
|
# )
|
||||||
app.include_router(automations.router, prefix="/automations")
|
app.include_router(ping.router, prefix="/ping")
|
||||||
|
app.include_router(automations.make_automation_router(automation_mgr), prefix="/automations")
|
||||||
|
|
||||||
|
return app
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
from src.env import ROOT_FOLDER
|
from src.automations.manager import AutomationManager
|
||||||
from src.automations.AutomationLoader import AutomationLoader
|
from src.hass_config import find_hass_config
|
||||||
|
|
||||||
# hass_config = load_hass_config(ROOT_FOLDER / "tests/samples")
|
hass_config = find_hass_config()
|
||||||
|
automations = AutomationManager(hass_config)
|
||||||
# db_connection = HassDatabase(
|
|
||||||
# f"sqlite:///{ROOT_FOLDER / 'tests/samples'}/home-assistant_v2.db"
|
|
||||||
# )
|
|
||||||
|
|
||||||
# automation_loader = AutomationLoader(hass_config)
|
|
||||||
@@ -1,53 +1,41 @@
|
|||||||
from typing import Optional
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
|
from src.automations.manager import AutomationManager
|
||||||
# from src.automations.AutomationLoader import AutomationData, AutomationLoaderException
|
from src.api.types import (
|
||||||
|
DeleteAutoRequest,
|
||||||
# from ..config import automation_loader
|
ListData,
|
||||||
|
ListParams,
|
||||||
router = APIRouter()
|
UpdateAutoRequest,
|
||||||
|
)
|
||||||
|
from src.automations.errors import FailedDeletion
|
||||||
|
from src.automations.types import AutomationData
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list")
|
def make_automation_router(automations: AutomationManager) -> APIRouter:
|
||||||
def list_autos(
|
router = APIRouter()
|
||||||
offset: int = 0,
|
|
||||||
limit: int = 10,
|
|
||||||
alias: Optional[str] = None,
|
|
||||||
description: Optional[str] = None,
|
|
||||||
mode: Optional[str] = None,
|
|
||||||
):
|
|
||||||
return list(
|
|
||||||
automation_loader.find(
|
|
||||||
offset=offset,
|
|
||||||
limit=limit,
|
|
||||||
alias=alias,
|
|
||||||
description=description,
|
|
||||||
mode=mode,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@router.post("/list")
|
||||||
@router.get("/{index}")
|
def list_autos(body: ListParams) -> ListData[AutomationData]:
|
||||||
def get_auto(index: int):
|
return ListData(
|
||||||
if auto := automation_loader.get(index):
|
params=body,
|
||||||
return auto
|
totalItems=automations.get_total_items(),
|
||||||
else:
|
data=automations.find(
|
||||||
raise HTTPException(
|
offset=body.offset,
|
||||||
status_code=400,
|
limit=body.limit,
|
||||||
detail={
|
),
|
||||||
"index": "out of bounds",
|
|
||||||
"must_be": f">= 0 or < {len(automation_loader)}",
|
|
||||||
"specified": index,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@router.post("/")
|
||||||
|
def upsert_auto(body: UpdateAutoRequest):
|
||||||
|
automations.update(body.index, body.data)
|
||||||
|
|
||||||
@router.post("/{index}")
|
@router.delete("/")
|
||||||
def upsert_auto(index: int, auto: AutomationData):
|
def delete_auto(body: DeleteAutoRequest):
|
||||||
try:
|
try:
|
||||||
automation_loader.save(index, auto)
|
automations.delete(body.index)
|
||||||
except AutomationLoaderException as err:
|
except FailedDeletion as err:
|
||||||
return HTTPException(
|
return HTTPException(
|
||||||
status_code=500,
|
status_code=400,
|
||||||
detail=err.args,
|
detail=err.args,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return router
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
from typing import Optional
|
|
||||||
from fastapi import APIRouter
|
|
||||||
from pydantic.main import BaseModel
|
|
||||||
from src.db import entities
|
|
||||||
from ..config import db_connection
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
from typing import Generic, List, TypeVar
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from pydantic.fields import Field
|
||||||
|
from pydantic.generics import GenericModel
|
||||||
|
from src.automations.types import AutomationData
|
||||||
|
|
||||||
|
DataT = TypeVar("DataT")
|
||||||
|
|
||||||
|
|
||||||
|
class ListParams(BaseModel):
|
||||||
|
offset: int
|
||||||
|
limit: int
|
||||||
|
|
||||||
|
|
||||||
|
class ListData(GenericModel, Generic[DataT]):
|
||||||
|
params: ListParams
|
||||||
|
total_items: int = Field(alias="totalItems")
|
||||||
|
data: List[DataT]
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateAutoRequest(BaseModel):
|
||||||
|
index: int = Field(description="Index of the automation to update")
|
||||||
|
data: AutomationData
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteAutoRequest(BaseModel):
|
||||||
|
index: int = Field(description="Index of the automation to update")
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
from typing import List, Optional
|
|
||||||
from src.yaml_serializer.types import IncludedYaml
|
|
||||||
from src.yaml_serializer import load_yaml, dump_yaml
|
|
||||||
from .types import AutomationData, AutomationMetdata
|
|
||||||
from .parsers import _parse_actions
|
|
||||||
from .errors import AutomationLoaderException
|
|
||||||
|
|
||||||
# Global Loader
|
|
||||||
class AutomationLoader:
|
|
||||||
def __init__(self, hass_config: HassConfig) -> None:
|
|
||||||
self.hass_config = hass_config
|
|
||||||
self.automation_ref = hass_config.config.get("automation", None)
|
|
||||||
|
|
||||||
if isinstance(self.automation_ref, IncludedYaml):
|
|
||||||
self.automation_raw_data: list = self.automation_ref.data
|
|
||||||
else:
|
|
||||||
self.automation_raw_data: list = self.automation_ref
|
|
||||||
self.automation_ref = None
|
|
||||||
|
|
||||||
if not isinstance(self.automation_raw_data, list):
|
|
||||||
raise AutomationLoaderException(
|
|
||||||
f"Invalid automation file, expected it to be a 'list' instead it's a '{type(self.automation_raw_data)}' --> `automation: {self.automation_raw_data}`"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.automations_data = list(load_automation(self.automation_raw_data))
|
|
||||||
|
|
||||||
def find(
|
|
||||||
self,
|
|
||||||
offset: int = 0,
|
|
||||||
limit: int = 10,
|
|
||||||
alias: Optional[str] = None,
|
|
||||||
description: Optional[str] = None,
|
|
||||||
mode: Optional[str] = None,
|
|
||||||
):
|
|
||||||
for auto in self.automations_data[offset : (offset + limit)]:
|
|
||||||
if alias is not None:
|
|
||||||
if auto.metadata.alias.lower().count(alias.lower()) <= 0:
|
|
||||||
continue
|
|
||||||
if description is not None:
|
|
||||||
if auto.metadata.description.lower().count(description.lower()) <= 0:
|
|
||||||
continue
|
|
||||||
if mode is not None:
|
|
||||||
if auto.metadata.mode.lower().count(mode.lower()) <= 0:
|
|
||||||
continue
|
|
||||||
yield auto
|
|
||||||
|
|
||||||
def get(self, index: int):
|
|
||||||
if (index < 0) or (len(self.automations_data) <= index):
|
|
||||||
return None
|
|
||||||
return self.automations_data[index]
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.automations_data)
|
|
||||||
|
|
||||||
def save(self, index: int, auto: AutomationData):
|
|
||||||
"""Save or create automation on disk and in-memory
|
|
||||||
|
|
||||||
Args:
|
|
||||||
index (int): index of the automation
|
|
||||||
auto (AutomationData): automation data
|
|
||||||
|
|
||||||
"""
|
|
||||||
# Update in-memory store
|
|
||||||
if len(self) <= index:
|
|
||||||
self.automations_data.append(auto)
|
|
||||||
else:
|
|
||||||
self.automations_data[index] = auto
|
|
||||||
# Load a simple version of the latest data on disk
|
|
||||||
if not isinstance(self.automation_ref, IncludedYaml):
|
|
||||||
with self.hass_config.root_config_path.open("r") as f:
|
|
||||||
hass_config_yaml = load_yaml(f)
|
|
||||||
original_yaml_path = f"{self.hass_config.root_config_path}>automation"
|
|
||||||
if "automation" not in hass_config_yaml:
|
|
||||||
raise AutomationLoaderException(
|
|
||||||
f'missing "automation" key in {self.hass_config.root_config_path}'
|
|
||||||
)
|
|
||||||
original_yaml = hass_config_yaml["automation"]
|
|
||||||
else:
|
|
||||||
with self.automation_ref.original_path.open("r") as f:
|
|
||||||
original_yaml = load_yaml(f)
|
|
||||||
original_yaml_path = f"{self.automation_ref.original_path}"
|
|
||||||
|
|
||||||
if not isinstance(original_yaml, list):
|
|
||||||
raise AutomationLoaderException(
|
|
||||||
f"Invalid data received from {original_yaml_path} expected a list"
|
|
||||||
)
|
|
||||||
# Upsert automation
|
|
||||||
if len(original_yaml) <= index:
|
|
||||||
original_yaml.append(auto.to_primitive())
|
|
||||||
else:
|
|
||||||
original_yaml[index] = auto.to_primitive()
|
|
||||||
|
|
||||||
# Save back to disk
|
|
||||||
if isinstance(self.automation_ref, IncludedYaml):
|
|
||||||
with self.automation_ref.original_path.open("w") as f:
|
|
||||||
f.write(dump_yaml(original_yaml))
|
|
||||||
else:
|
|
||||||
hass_config_yaml["automation"] = original_yaml
|
|
||||||
with self.hass_config.root_config_path.open("w") as f:
|
|
||||||
f.write(dump_yaml(hass_config_yaml))
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import os
|
|
||||||
from decouple import config
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
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(config("APPDATA", None) 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."
|
|
||||||
)
|
|
||||||
@@ -17,7 +17,7 @@ class AutomationManager:
|
|||||||
for i, auto in enumerate(self.hass_config.automations):
|
for i, auto in enumerate(self.hass_config.automations):
|
||||||
if i < offset:
|
if i < offset:
|
||||||
continue
|
continue
|
||||||
if i > offset + limit:
|
if i >= offset + limit:
|
||||||
break
|
break
|
||||||
yield from load_automation([auto])
|
yield from load_automation([auto])
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import os
|
||||||
|
from decouple import config
|
||||||
|
from pathlib import Path
|
||||||
|
from .loader import HassConfig
|
||||||
|
|
||||||
|
|
||||||
|
def find_hass_config() -> HassConfig:
|
||||||
|
"""Try to find HASS config."""
|
||||||
|
if "HASSIO_TOKEN" in os.environ:
|
||||||
|
return HassConfig(Path("/config"))
|
||||||
|
|
||||||
|
"""Put together the default configuration directory based on the OS."""
|
||||||
|
data_dir = Path(config("APPDATA", None) if os.name == "nt" else os.path.expanduser("~"))
|
||||||
|
config_dir = data_dir / ".homeassistant"
|
||||||
|
|
||||||
|
if config_dir.is_dir():
|
||||||
|
return HassConfig(config_dir)
|
||||||
|
|
||||||
|
raise ValueError(
|
||||||
|
"Unable to automatically find the location of Home Assistant " "config. Please pass it in."
|
||||||
|
)
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
from pathlib import Path
|
from typing import NamedTuple
|
||||||
from typing import Any, NamedTuple
|
|
||||||
from ruamel.yaml.nodes import Node
|
from ruamel.yaml.nodes import Node
|
||||||
from ruamel.yaml.representer import BaseRepresenter
|
from ruamel.yaml.representer import BaseRepresenter
|
||||||
|
|
||||||
|
|||||||
Whitespace-only changes.
@@ -0,0 +1,94 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from src.api.app import make_app
|
||||||
|
from src.automations.types import AutomationData, AutomationMetdata
|
||||||
|
from tests.utils import get_example_automation_loader
|
||||||
|
|
||||||
|
|
||||||
|
class BaseTestCase(TestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
(
|
||||||
|
self.config_folder,
|
||||||
|
self.hass_config,
|
||||||
|
self.automation_loader,
|
||||||
|
) = get_example_automation_loader()
|
||||||
|
self.client = TestClient(make_app(self.automation_loader))
|
||||||
|
|
||||||
|
|
||||||
|
class automation_list_tests(BaseTestCase):
|
||||||
|
def test_get_list_of_everything(self):
|
||||||
|
data = self.client.post(
|
||||||
|
"/automations/list",
|
||||||
|
json={
|
||||||
|
"offset": 0,
|
||||||
|
"limit": 50,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(data.status_code, 200)
|
||||||
|
d = data.json()
|
||||||
|
self.assertEqual(
|
||||||
|
d["params"],
|
||||||
|
{
|
||||||
|
"offset": 0,
|
||||||
|
"limit": 50,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(d["totalItems"], 31)
|
||||||
|
self.assertEqual(len(d["data"]), 31)
|
||||||
|
|
||||||
|
def test_subset1(self):
|
||||||
|
data = self.client.post(
|
||||||
|
"/automations/list",
|
||||||
|
json={
|
||||||
|
"offset": 0,
|
||||||
|
"limit": 10,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(data.status_code, 200)
|
||||||
|
d = data.json()
|
||||||
|
self.assertEqual(
|
||||||
|
d["params"],
|
||||||
|
{
|
||||||
|
"offset": 0,
|
||||||
|
"limit": 10,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(d["totalItems"], 31)
|
||||||
|
self.assertEqual(len(d["data"]), 10)
|
||||||
|
|
||||||
|
def test_subset2(self):
|
||||||
|
data = self.client.post(
|
||||||
|
"/automations/list",
|
||||||
|
json={
|
||||||
|
"offset": 25,
|
||||||
|
"limit": 10,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(data.status_code, 200)
|
||||||
|
d = data.json()
|
||||||
|
self.assertEqual(
|
||||||
|
d["params"],
|
||||||
|
{
|
||||||
|
"offset": 25,
|
||||||
|
"limit": 10,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(d["totalItems"], 31)
|
||||||
|
self.assertEqual(len(d["data"]), 6)
|
||||||
|
|
||||||
|
|
||||||
|
class automation_update_tests(BaseTestCase):
|
||||||
|
def test_insert_new(self):
|
||||||
|
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0})
|
||||||
|
self.assertEqual(resp.json()["totalItems"], 31)
|
||||||
|
resp = self.client.post(
|
||||||
|
"/automations",
|
||||||
|
json={
|
||||||
|
"index": 32,
|
||||||
|
"data": AutomationData(metadata=AutomationMetdata(id="testme")).dict(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 32})
|
||||||
|
data = resp.json()
|
||||||
|
self.assertEqual(data["totalItems"], 32)
|
||||||
|
self.assertEqual(data["data"][0]["metadata"]["id"], "testme")
|
||||||
Reference in new issue
Block a user