v2 initial work

This commit is contained in:
ari committed 2022-08-14 00:24:44 -04:00
1 parent 2bc7e11bb1
commit 3a89fe5efc
36 files changed
+830 -4

No files matched your search

View File
Whitespace-only changes.
+116
View File
@@ -0,0 +1,116 @@
import json
import sqlite3
import base64
from contextlib import contextmanager
from pathlib import Path
from typing import Iterable, Iterator, List, Optional
from src.logger import get_logger
from .types import ExtenededAutomationData
from .errors import DBDataError, DBError
logger = get_logger(__file__)
automations_tbl = "autos"
def encode_auto(auto: ExtenededAutomationData) -> str:
return base64.b64encode(
json.dumps(auto.to_primitive(include_tags=True)).encode("utf-8")
).decode("utf-8")
def decode_auto(auto_str: str, source_file: str, source_file_type: str) -> ExtenededAutomationData:
return ExtenededAutomationData(
source_file=source_file,
source_file_type=source_file_type,
**json.loads(base64.b64decode(auto_str.encode("utf-8")).decode("utf-8")),
)
class AutomationDBConnection:
def __init__(self, db_file: Path) -> None:
self.db_file = db_file
@contextmanager
def get_cur(self) -> Iterator[sqlite3.Cursor]:
conn = sqlite3.connect(self.db_file)
cur = conn.cursor()
try:
yield cur
conn.commit()
except sqlite3.IntegrityError as err:
raise DBDataError(*err.args) from err
except sqlite3.DatabaseError as err:
raise DBError(*err.args) from err
finally:
cur.close()
conn.close()
def create_db(self):
with self.get_cur() as cur:
cur.execute(
f"""
CREATE TABLE IF NOT EXISTS {automations_tbl} (
id TEXT NOT NULL PRIMARY KEY,
alias TEXT,
description TEXT,
mode TEXT NOT NULL,
source_file TEXT NOT NULL,
source_file_type TEXT NOT NULL,
rest BLOB NOT NULL
);
"""
)
def upsert_automations(self, automations: List[ExtenededAutomationData]):
with self.get_cur() as cur:
for automation in automations:
cur.execute(
f"""
INSERT INTO {automations_tbl} VALUES (
"{automation.id}",
"{automation.alias}",
"{automation.description}",
"{automation.mode}",
"{automation.source_file}",
"{automation.source_file_type}",
"{encode_auto(automation)}"
)
ON CONFLICT DO UPDATE SET
alias = "{automation.alias}",
description = "{automation.description}",
mode = "{automation.mode}",
source_file = "{automation.source_file}",
source_file_type = "{automation.source_file_type}",
rest = "{encode_auto(automation)}"
""",
)
def delete_automations(self, automations: List[ExtenededAutomationData]):
with self.get_cur() as cur:
cur.executemany(
f"DELETE FROM {automations_tbl} WHERE id = ?", [(a.id,) for a in automations]
)
def get_automation(self, automation_id: str) -> ExtenededAutomationData:
with self.get_cur() as cur:
cur.execute(
f'SELECT rest, source_file, source_file_type FROM {automations_tbl} WHERE id = "{automation_id}"'
)
objs = cur.fetchone()
[rest, source_file, source_file_type] = objs
return decode_auto(rest, source_file, source_file_type)
def list_automations(self, offset: int, limit: int) -> Iterable[ExtenededAutomationData]:
with self.get_cur() as cur:
for [rest, source_file, source_file_type] in cur.execute(
f"SELECT rest, source_file, source_file_type FROM {automations_tbl} LIMIT {limit} OFFSET {offset}"
):
yield decode_auto(rest, source_file, source_file_type)
def count_automations(self) -> int:
with self.get_cur() as cur:
return cur.execute(f"SELECT count(id) FROM {automations_tbl}").fetchone()[0]
+21
View File
@@ -0,0 +1,21 @@
class AutomationLoaderException(Exception):
def __init__(self, *args: object, **kwargs) -> None:
super().__init__(
"".join(map(str, args)) + ",".join((f"{k}={v}" for k, v in kwargs.items())),
)
class InvalidAutomationFile(AutomationLoaderException):
pass
class FailedDeletion(AutomationLoaderException):
pass
class DBError(Exception):
pass
class DBDataError(DBError):
pass
+93
View File
@@ -0,0 +1,93 @@
from pathlib import Path
from typing import Any, Iterator, Union
from src.json_serializer import normalize_obj
from src.yaml_serializer import load_yaml
from .errors import InvalidAutomationFile
from .types import ExtenededAutomationData
# Single loader
def load_automation_path(automation_path: Path) -> Iterator[ExtenededAutomationData]:
"""
Args:
automation_path (Path)
Returns:
Iterator[ExtenededAutomationData]
"""
try:
with automation_path.open("r") as fp:
automations = load_yaml(fp)
except Exception as err:
raise InvalidAutomationFile(
when="loading yaml",
automation_path=automation_path,
error=err,
) from err
if isinstance(automations, dict):
try:
yield ExtenededAutomationData(
**clean_automation(automations),
source_file=str(automation_path),
source_file_type="obj",
)
except Exception as err:
raise InvalidAutomationFile(
when="reading file contents",
automation_path=automation_path,
error=err,
) from err
else:
for automation in automations:
try:
yield ExtenededAutomationData(
**clean_automation(automation),
source_file=str(automation_path),
source_file_type="list",
)
except Exception as err:
raise InvalidAutomationFile(
when="reading file contents",
automation_path=automation_path,
error=err,
automation=automation,
) from err
def clean_automation(auto_raw: dict):
return {
**auto_raw,
"condition": ifempty_or_null_then_list(load_conditions(auto_raw.get("condition", []))),
"action": ifempty_or_null_then_list(load_conditions(auto_raw.get("action", []))),
"trigger": ifempty_or_null_then_list(normalize_obj(auto_raw.get("trigger", []))),
}
def ifempty_or_null_then_list(obj: Any):
if not isinstance(obj, list):
return []
else:
return obj
def load_conditions(conditions: Union[dict, list, str]):
if isinstance(conditions, list):
return list(map(ensure_condition_dictionary, conditions))
elif not conditions:
return []
else:
return [ensure_condition_dictionary(conditions)]
def ensure_condition_dictionary(c: Union[str, dict]):
c = normalize_obj(c)
if isinstance(c, str):
return {"condition": "template", "value_template": c}
elif isinstance(c, dict):
return c
raise InvalidAutomationFile(f"Invalid type for condition object {c}")
+32
View File
@@ -0,0 +1,32 @@
from typing import Dict, List, Literal, Optional, Union
from pydantic import BaseModel
from pydantic.fields import Field
class BaseAutomation(BaseModel):
id: str
alias: Optional[str] = None
description: Optional[str] = None
trigger_variables: Optional[Dict[str, Union[str, int, bool, float]]] = None
mode: str = "single"
trigger: List[dict] = []
condition: List[dict] = []
action: List[dict] = []
def to_primitive(self):
return self.dict(exclude_unset=True, exclude_none=True)
class ExtenededAutomationData(BaseAutomation):
source_file: str
source_file_type: Literal["list", "obj"]
tags: Dict[str, str] = Field(default_factory=dict)
def to_primitive(self, include_tags: bool = False):
out = super().to_primitive()
del out["source_file"]
del out["source_file_type"]
if include_tags and out.get("tags", None) is not None:
del out["tags"]
return out
+107
View File
@@ -0,0 +1,107 @@
from threading import Thread, Condition, Event
from pathlib import Path
from time import sleep
from typing import List, Optional
from watchdog.events import FileSystemEvent
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from src.logger import get_logger
from .db import AutomationDBConnection, DBError
from .loader import load_automation_path
from .types import ExtenededAutomationData
from .errors import FailedDeletion
logger = get_logger(__file__)
class AutomationFileEventHandler(FileSystemEventHandler):
def __init__(self, automation_file: Path, automation_db_path: Path, file_loaded: Event) -> None:
super().__init__()
self.__automation_file = automation_file.absolute()
self.__automation_db = AutomationDBConnection(automation_db_path)
self.file_loaded = file_loaded
self.__loaded_automations: Optional[List[ExtenededAutomationData]] = None
self.reload_file()
def reload_file(self):
try:
automations = list(load_automation_path(self.__automation_file))
self.__automation_db.upsert_automations(automations)
self.__loaded_automations = automations
except Exception as err:
logger.error(err)
if isinstance(err, DBError):
logger.warning(f"Failed to reload {self.__automation_file} due to {err}")
else:
logger.warning(f"Failed to load {self.__automation_file} due to {err}")
if self.__loaded_automations is not None:
try:
self.__automation_db.delete_automations(self.__loaded_automations)
except DBError as err:
raise FailedDeletion(
f"failed to flush automations from database for the invalid file {self.__automation_file} due to {err}"
) from err
self.file_loaded.set()
def on_modified(self, event: FileSystemEvent):
logger.info(f"on_modified: {type(event)} {event.event_type} path : {event.src_path}")
if Path(event.src_path).absolute() == self.__automation_file:
self.reload_file()
def on_created(self, event: FileSystemEvent):
logger.info(f"on_created: {type(event)} {event.event_type} path : {event.src_path}")
if Path(event.src_path).absolute() == self.__automation_file:
self.reload_file()
def on_deleted(self, event: FileSystemEvent):
logger.info(f"on_deleted: {type(event)} {event.event_type} path : {event.src_path}")
if Path(event.src_path).absolute() == self.__automation_file:
self.reload_file()
class AutomationFileWatcher(Thread):
def __init__(self, automation_file: Path, automation_db_path: Path):
self.__automation_file = automation_file
self.__automation_db_path = automation_db_path
self.__stop = False
self.__file_loaded = Event()
Thread.__init__(self)
def run(self) -> None:
self.__stop = False
logger.info(f"Watching {self.__automation_file} for changes...")
obs = Observer()
handler = AutomationFileEventHandler(
self.__automation_file,
self.__automation_db_path,
self.__file_loaded,
)
obs.schedule(handler, self.__automation_file)
obs.start()
while not self.__stop:
sleep(1)
obs.stop()
logger.warning(f"Watcher for {self.__automation_file} is stopped!")
def stop(self):
if not self.__stop:
logger.warning(f"Watcher for {self.__automation_file} is set to stop...")
self.__stop = True
def __del__(self):
logger.warning(f"Deleting watcher for {self.__automation_file}...")
self.join()
def join(self, timeout: float = 10) -> None:
logger.warning(f"Closing thread for watcher for {self.__automation_file}...")
self.stop()
return super().join(timeout)
def wait_until_next_reload(self, reset_after: bool = False, *, timeout: int = 5):
self.__file_loaded.wait(timeout)
if reset_after:
self.__file_loaded.clear()
+1 -1
View File
@@ -6,7 +6,7 @@ from websockets.client import WebSocketClientProtocol, connect
from websockets.exceptions import ConnectionClosedOK
from src.env import HASSIO_TOKEN, HASSIO_WS
from src.logger import get_logger, logger
from src.logger import get_logger
OnMessage = Callable[[str], Coroutine]
logger = get_logger(__name__)
+1 -1
View File
@@ -36,6 +36,6 @@ def load_yaml(yaml_stream: TextIO) -> Union[dict, list]:
yaml_stream (io.StringIO)
Returns:
List[dict, list]
Union[dict, list]
"""
return yaml.load(yaml_stream, Loader=YamlSafeLoader) # nosec