refactor + cleanup

This commit is contained in:
ari committed 2021-12-30 22:30:21 -05:00
1 parent 4a08956e1a
commit 381835c642
34 files changed
+724 -933

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# End of https://www.toptal.com/developers/gitignore/api/python
+1 -10
View File
@@ -1,14 +1,5 @@
{
"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",
@@ -18,4 +9,4 @@
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
}
+6 -8
View File
@@ -1,12 +1,10 @@
from src.env import ROOT_FOLDER
from src.db.connection import HassDatabase
from src.config.AutomationLoader import AutomationLoader
from src.yaml_serializer import load_hass_config
from src.automations.AutomationLoader import AutomationLoader
hass_config = load_hass_config(ROOT_FOLDER / "tests/samples")
# hass_config = load_hass_config(ROOT_FOLDER / "tests/samples")
db_connection = HassDatabase(
f"sqlite:///{ROOT_FOLDER / 'tests/samples'}/home-assistant_v2.db"
)
# db_connection = HassDatabase(
# f"sqlite:///{ROOT_FOLDER / 'tests/samples'}/home-assistant_v2.db"
# )
automation_loader = AutomationLoader(hass_config)
# automation_loader = AutomationLoader(hass_config)
+2 -2
View File
@@ -1,9 +1,9 @@
from typing import Optional
from fastapi import APIRouter, HTTPException
from src.config.AutomationLoader import AutomationData, AutomationLoaderException
# from src.automations.AutomationLoader import AutomationData, AutomationLoaderException
from ..config import automation_loader
# from ..config import automation_loader
router = APIRouter()
+100
View File
@@ -0,0 +1,100 @@
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))
+20
View File
@@ -0,0 +1,20 @@
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."
)
+2
View File
@@ -0,0 +1,2 @@
class AutomationLoaderException(Exception):
pass
+33
View File
@@ -0,0 +1,33 @@
from itertools import chain
from typing import Iterable
from src.json_serializer import normalize_obj
from .types import AutomationData, AutomationMetdata
from .parsers import _parse_actions
# Single loader
def load_automation(
automations_raw_data: Iterable[dict],
) -> Iterable[AutomationData]:
"""Converts an automation dictionary loaded directly from the config into
several python typed objects
Args:
automations_raw_data (Iterable[dict])
Returns:
Iterable[AutomationData]
"""
for auto_raw in automations_raw_data:
yield AutomationData(
metadata=AutomationMetdata.parse_obj(auto_raw),
sequence=list(
_parse_actions(
chain(
auto_raw.get("condition", []),
auto_raw.get("action", []),
)
)
),
trigger=[normalize_obj(obj) for obj in auto_raw.get("trigger", [])],
)
+3
View File
@@ -0,0 +1,3 @@
class AutomationManager:
def __init__(self) -> None:
pass
+88
View File
@@ -0,0 +1,88 @@
from typing import Iterable, Union
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG, json_dumps, normalize_obj
from src.yaml_serializer.types import SecretValue
from .types import AutomationActionNode, AutomationConditionNode
def _parse_actions(
raw_actions: Iterable[Union[dict, str]]
) -> Iterable[Union[AutomationActionNode, AutomationConditionNode]]:
for raw_c in raw_actions:
action_type = "unknown"
if isinstance(raw_c, str):
yield from _parse_conditions([raw_c])
continue
elif isinstance(raw_c, dict):
if raw_c.get("condition"):
yield from _parse_conditions([raw_c])
continue
elif raw_c.get("service"):
action_type = "service"
action_data = normalize_obj(raw_c)
elif raw_c.get("repeat"):
action_type = "repeat"
repeat_data = raw_c.pop("repeat")
action_data = {
**raw_c,
"repeat": {
"count": repeat_data.get("count", 0),
"sequence": list(_parse_actions(repeat_data.get("sequence", []))),
},
}
elif raw_c.get("wait_template"):
action_type = "wait"
action_data = normalize_obj(raw_c)
elif raw_c.get("event"):
action_type = "event"
action_data = normalize_obj(raw_c)
elif raw_c.get("type") and raw_c.get("device_id"):
action_type = "device"
action_data = normalize_obj(raw_c)
elif raw_c.get("choose"):
action_type = "choose"
choose_data = raw_c.pop("choose")
default_data = raw_c.pop("default", [])
action_data = {
**raw_c,
"choose": [
{
"conditions": list(_parse_conditions(option.get("conditions", []))),
"sequence": list(_parse_actions(option.get("sequence", []))),
}
for option in choose_data
],
"default": list(_parse_actions(default_data)),
}
else:
action_data = normalize_obj(raw_c)
yield AutomationActionNode(
action=action_type,
action_data=action_data,
)
def _parse_conditions(
raw_conditions: Iterable[Union[dict, str]]
) -> Iterable[AutomationConditionNode]:
for raw_c in raw_conditions:
if isinstance(raw_c, str):
yield AutomationConditionNode(
condition="template",
condition_data={"value_template": raw_c},
)
elif isinstance(raw_c, SecretValue):
yield AutomationConditionNode(
condition="template",
condition_data={"value_template": NOT_IMPLEMENTED_SV_MSG},
)
elif isinstance(raw_c, dict):
condition = raw_c.pop("condition")
yield AutomationConditionNode(
condition=condition,
condition_data=normalize_obj(raw_c),
)
else:
yield AutomationConditionNode(
condition="template",
condition_data=json_dumps(raw_c),
)
+46
View File
@@ -0,0 +1,46 @@
from typing import Dict, List, Literal, Optional, Union
from pydantic.fields import Field
from pydantic import BaseModel
class AutomationConditionNode(BaseModel):
condition: str
condition_data: dict
node_type: Literal["condition"] = Field(alias="$smType", default="condition")
def to_primitive(self):
return {
**self.condition_data,
"condition": self.condition,
}
class AutomationActionNode(BaseModel):
action: str
action_data: dict
node_type: Literal["action"] = Field(alias="$smType", default="action")
def to_primitive(self):
return self.action_data
class AutomationMetdata(BaseModel):
id: str
alias: Optional[str] = None
description: Optional[str] = None
trigger_variables: Optional[Dict[str, Union[str, int, bool, float]]] = None
mode: Literal["single", "restart", "queued", "parallel"] = "single"
class AutomationData(BaseModel):
metadata: AutomationMetdata
trigger: List[dict] = []
sequence: List[Union[AutomationConditionNode, AutomationActionNode]] = []
def to_primitive(self):
return {
**self.metadata.dict(),
"trigger": self.trigger,
"condition": [],
"action": [action.to_primitive() for action in self.sequence],
}
-253
View File
@@ -1,253 +0,0 @@
from typing import Dict, Iterable, List, Literal, Optional, Union
from pydantic.fields import Field
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG, json_dumps, normalize_obj
from src.yaml_serializer import HassConfig, IncludedYaml, SecretValue, dump_yaml, simple_load_yaml
from pydantic import BaseModel
# errors
class AutomationLoaderException(Exception):
pass
# types
class AutomationConditionNode(BaseModel):
condition: str
condition_data: dict
node_type: Literal["condition"] = Field(alias="$smType", default="condition")
def to_primitive(self):
return {
**self.condition_data,
"condition": self.condition,
}
class AutomationActionNode(BaseModel):
action: str
action_data: dict
node_type: Literal["action"] = Field(alias="$smType", default="action")
def to_primitive(self):
return self.action_data
class AutomationMetdata(BaseModel):
id: str
alias: Optional[str] = None
description: Optional[str] = None
trigger_variables: Optional[Dict[str, Union[str, int, bool, float]]] = None
mode: Literal["single", "restart", "queued", "parallel"] = "single"
class AutomationData(BaseModel):
metadata: AutomationMetdata
trigger: List[dict] = []
condition: List[AutomationConditionNode] = []
action: List[Union[AutomationConditionNode, AutomationActionNode]] = []
def to_primitive(self):
return {
**self.metadata.dict(),
"trigger": self.trigger,
"condition": [condition.to_primitive() for condition in self.condition],
"action": [action.to_primitive() for action in self.action],
}
# Single loader
def load_automation(
automations_raw_data: List[dict],
) -> Iterable[AutomationData]:
"""Converts an automation dictionary loaded directly from the config into
several python typed objects
Args:
automations_raw_data (List[dict])
Returns:
Iterable[AutomationData]
"""
for auto_raw in automations_raw_data:
yield AutomationData(
metadata=AutomationMetdata.parse_obj(auto_raw),
condition=list(_parse_conditions(auto_raw.get("condition", []))),
action=list(_parse_actions(auto_raw.get("action", []))),
trigger=[normalize_obj(obj) for obj in auto_raw.get("trigger", [])],
)
def _parse_actions(
raw_actions: Iterable[Union[dict, str]]
) -> Iterable[Union[AutomationActionNode, AutomationConditionNode]]:
for raw_c in raw_actions:
action_type = "unknown"
if isinstance(raw_c, str):
yield from _parse_conditions([raw_c])
continue
elif isinstance(raw_c, dict):
if raw_c.get("condition"):
yield from _parse_conditions([raw_c])
continue
elif raw_c.get("service"):
action_type = "service"
action_data = normalize_obj(raw_c)
elif raw_c.get("repeat"):
action_type = "repeat"
repeat_data = raw_c.pop("repeat")
action_data = {
**raw_c,
"repeat": {
"count": repeat_data.get("count", 0),
"sequence": list(_parse_actions(repeat_data.get("sequence", []))),
},
}
elif raw_c.get("wait_template"):
action_type = "wait"
action_data = normalize_obj(raw_c)
elif raw_c.get("event"):
action_type = "event"
action_data = normalize_obj(raw_c)
elif raw_c.get("type") and raw_c.get("device_id"):
action_type = "device"
action_data = normalize_obj(raw_c)
elif raw_c.get("choose"):
action_type = "choose"
choose_data = raw_c.pop("choose")
default_data = raw_c.pop("default", [])
action_data = {
**raw_c,
"choose": [
{
"conditions": list(_parse_conditions(option.get("conditions", []))),
"sequence": list(_parse_actions(option.get("sequence", []))),
}
for option in choose_data
],
"default": list(_parse_actions(default_data)),
}
else:
action_data = normalize_obj(raw_c)
yield AutomationActionNode(action=action_type, action_data=action_data)
def _parse_conditions(
raw_conditions: Iterable[Union[dict, str]]
) -> Iterable[AutomationConditionNode]:
for raw_c in raw_conditions:
if isinstance(raw_c, str):
yield AutomationConditionNode(
condition="template",
condition_data={"value_template": raw_c},
)
elif isinstance(raw_c, SecretValue):
yield AutomationConditionNode(
condition="template",
condition_data={"value_template": NOT_IMPLEMENTED_SV_MSG},
)
elif isinstance(raw_c, dict):
condition = raw_c.pop("condition")
yield AutomationConditionNode(
condition=condition,
condition_data=normalize_obj(raw_c),
)
else:
yield AutomationConditionNode(
condition="template",
condition_data=json_dumps(raw_c),
)
# 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 = simple_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 = simple_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))
-46
View File
@@ -1,46 +0,0 @@
import os
from decouple import config
from pathlib import Path
from src.yaml_serializer 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(
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."
)
def db_url_from_hass_config(path: Path):
"""Find the recorder database url from a HASS config dir."""
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
-41
View File
@@ -1,41 +0,0 @@
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)
self.connection = None
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]
def execute_sql(self, sql: str, **args):
self.connect()
resp = self.engine.execute(text(sql), **args)
self.close()
return resp.fetchall()
def connect(self):
if self.connection is None:
self.connection = self.engine.connect()
def close(self):
if self.connection is not None:
self.connection.close()
self.connection = None
def __del__(self):
self.close()
-59
View File
@@ -1,59 +0,0 @@
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
File renamed without changes.
+6
View File
@@ -0,0 +1,6 @@
class HassConfigError(Exception):
pass
class MissingFile(HassConfigError):
pass
+28
View File
@@ -0,0 +1,28 @@
from pathlib import Path
from typing import List
from src.yaml_serializer import load_yaml
from src.yaml_serializer.types import IncludedYaml
class HassConfig:
def __init__(self, root_path: Path) -> None:
self.root_path = root_path
@property
def configurations(self) -> dict:
with (self.root_path / "configuration.yaml").open("r") as f:
return dict(load_yaml(f)) # type: ignore
@property
def automations(self) -> List[dict]:
automation_path = self.root_path / "automations.yaml"
config = self.configurations
if "automation" in config:
automation_ref = config["automation"]
if isinstance(automation_ref, IncludedYaml):
automation_path = automation_ref.path
else:
return list(config["automation"])
with automation_path.open("r") as f:
return list(load_yaml(f)) # type: ignore
+1
View File
@@ -0,0 +1 @@
+1 -1
View File
@@ -1,6 +1,6 @@
import orjson
from typing import Any, Union
from src.yaml_serializer import SecretValue
from src.yaml_serializer.types import SecretValue
# TODO: add support for these
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
-176
View File
@@ -1,176 +0,0 @@
from pathlib import Path
from typing import Any, NamedTuple, Optional
from ruamel.yaml import YAML
from ruamel.yaml.constructor import SafeConstructor
from ruamel.yaml.nodes import Node
from src.logger import logger
from src.yaml_serializer.types import IncludedYamlDir, SecretValue, IncludedYaml
class HassYaml:
"""Load a YAML file based on the standard defined by HASS
Args:
file_path (Path): path to yaml file
Returns:
dict
"""
def __init__(self) -> None:
self.secrets: dict = {}
self.current_file_path: Optional[str] = None
self.yaml_loader = YAML(typ="safe")
# Compat with HASS
self.yaml_loader.allow_duplicate_keys = True
# Stub HASS constructors
class HassSafeConstructor(SafeConstructor):
"""Hass specific SafeConstructor."""
def _include_yaml(_loader, node: Node):
"""Load another YAML file and embeds it using the !include tag.
Example:
device_tracker: !include device_tracker.yaml
"""
root_path = (
self.current_file_path.parent if self.current_file_path is not None else Path("/")
)
file_path = _upward_recursive_search(root_path, node.value)
if file_path is None:
logger.warning(f"Could not find `!include {node.value}`!")
return None
else:
logger.info(f"Found `!include {node.value}` in {file_path.absolute()}!")
return IncludedYaml(node.value, file_path, self.__internal_constructor_load(file_path))
def _secret_yaml(loader, node: Node):
"""Load secrets and embed it into the configuration YAML."""
secret = self.secrets.get(node.value, None)
if secret is None:
logger.warning(
f"The secret {node.value} failed to load, please check your `secret.yaml` file."
)
return SecretValue(node.value, secret)
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)
self.yaml_loader.Constructor = HassSafeConstructor
def load(self, file_path: Path) -> dict:
"""Loads a given yaml file into a dictionary
Args:
file_path (Path): path to yaml file
Returns:
dict
"""
self.secrets = self.__load_secrets(file_path.parent)
return self.__load(file_path)
def __internal_constructor_load(self, file_path: Path):
"""This is similar to `self.load` function but it is only called internally by the yaml parsers
as it also manages the references to which file we are currently processing
Args:
file_path (Path): path to yaml file
Returns:
dict
"""
last_file_path = self.current_file_path
loaded = self.__load(file_path)
self.current_file_path = last_file_path
return loaded
def __load(self, file_path: Path) -> dict:
"""Generic load yaml function.
This function is called by either `self.load` or `self.__internal_constructor_load`
Args:
file_path (Path): path to yaml file
Returns:
dict
"""
self.current_file_path = file_path
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 self.yaml_loader.load(conf_file) or {}
def __load_secrets(self, root_path: Path) -> dict:
"""Preloads all of the secrets into memory
Args:
root_path (Path): root path where we search for the serects.yaml file
Returns:
dict: all secrets in dict format
"""
file_path = _upward_recursive_search(root_path, "secrets.yaml")
if file_path is None:
logger.warning(f"Secrets file could not be found!")
return {}
else:
logger.info(f"Found secrets file in `{file_path.absolute()}`!")
with file_path.open() as secret_file:
return YAML(typ="safe").load(secret_file)
def _upward_recursive_search(root_dir: Path, file_name: str) -> Optional[Path]:
"""Recursively search for a file or return None
i.e.
assume we are in local deployment where our /config is sitting in /home/awesomeuser/ha/config
you specified somewhere in the yaml `device_tracker: !include device_tracker.yaml`
then
root_dir = Path(/home/awesomeuser/ha/config)
file_name = device_tracker.yaml
we then first search /home/awesomeuser/ha/config
if we find nothin go up to /home/awesomeuser/ha
if we find nothin go up to /home/awesomeuser
if we find nothin go up to /home
if we find nothin go up to /
if we still find nothing return None
if we found something at any stage, we will return it's direct path
Args:
root_dir (Path)
file_name (str)
Returns:
Optional[Path]
"""
dir_path = root_dir
file_path = dir_path / file_name
for _ in root_dir.parts:
if file_path.exists():
return file_path
dir_path = root_dir
file_path = dir_path / file_name
return None
def _stub_tag(constructor, node: 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:
logger.warning(f"YAML tag {node.tag} is not supported")
seen.add(node.tag)
return IncludedYamlDir(node.tag, node.value)
+2 -21
View File
@@ -1,25 +1,6 @@
import io
from pathlib import Path
from typing import Any, Union
from typing import Any, Union, TextIO
from src.yaml_serializer.base_yaml import yaml
from src.yaml_serializer.HassYaml import HassYaml
from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml, IncludedYamlDir
## TODO: consider deprecating this and using 'simple_load_yaml' for consistency
def load_hass_config(file_path: Path) -> HassConfig:
"""Load all Hass configrations from the root config folder
**highly opinionated**
Args:
file_path (Path)
Returns:
HassConfig
"""
HAY = HassYaml()
data = HAY.load(file_path / "configuration.yaml")
return HassConfig(HAY.secrets, data, file_path / "configuration.yaml")
def dump_yaml(obj: Any) -> str:
@@ -37,7 +18,7 @@ def dump_yaml(obj: Any) -> str:
return stream.read()
def simple_load_yaml(yaml_stream: io.StringIO) -> Union[dict, list]:
def load_yaml(yaml_stream: TextIO) -> Union[dict, list]:
"""Load yaml from string using some hass-compliant yaml strings
Args:
+5 -15
View File
@@ -9,17 +9,15 @@ class IncludedYaml(NamedTuple):
this data type will represent it.
"""
name: str # original node name
original_path: Path # path to location of original file
data: Any # the parsed data
path: Path # original node path
@classmethod
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
return representer.represent_scalar("!include", node.name)
return representer.represent_scalar("!include", str(node.path.absolute()))
@classmethod
def from_yaml(cls, _constructor, node: Node):
return cls(node.value, Path("/"), "n/a")
return cls(Path(node.value).absolute())
class SecretValue(NamedTuple):
@@ -27,7 +25,7 @@ class SecretValue(NamedTuple):
value: str
@classmethod
def to_yaml(cls, representer: BaseRepresenter, node: Node):
def to_yaml(cls, representer: BaseRepresenter, node: "SecretValue"):
return representer.represent_scalar("!secret", node.name)
@classmethod
@@ -48,17 +46,9 @@ class IncludedYamlDir(NamedTuple):
name: str # original node name
@classmethod
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYamlDir"):
return representer.represent_scalar(f"!{node.original_tag}", node.name)
@classmethod
def from_yaml(cls, _constructor, node: Node):
return cls(node.tag[1:], node.value)
class HassConfig(NamedTuple):
"""The final output for load_hass_config"""
secrets: dict # the secrets file content
config: dict # the complete configration.yaml tree
root_config_path: Path # where the root 'configuration.yaml' lives
Submodule api/tests/samples/example_config_folder added at 2f2c69d6db.
Whitespace-only changes.
@@ -0,0 +1,67 @@
from unittest import TestCase
from src.automations.types import (
AutomationActionNode,
AutomationConditionNode,
AutomationData,
AutomationMetdata,
)
from src.automations.loader import load_automation
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
from src.yaml_serializer.types import (
SecretValue,
)
class loader_tests(TestCase):
def test_load_automation_runs(self):
auto = list(
load_automation(
[
{
"id": "1616887975473",
"alias": "Home Assistant Off",
"description": "",
"trigger": [{"platform": "homeassistant", "event": "shutdown"}],
"condition": ["states(time.time) >= '10:00:00'"],
"action": [
{
"device_id": SecretValue("my_mobile_phone", "..."),
"domain": "mobile_app",
"type": "notify",
"title": "Hassio Status",
"message": "Hassio Is turning off...",
}
],
"mode": "single",
}
]
)
)
self.assertEqual(
auto[0],
AutomationData(
metadata=AutomationMetdata(
id="1616887975473",
alias="Home Assistant Off",
description="",
mode="single",
),
trigger=[{"platform": "homeassistant", "event": "shutdown"}],
sequence=[
AutomationConditionNode(
condition="template",
condition_data={"value_template": "states(time.time) >= '10:00:00'"},
),
AutomationActionNode(
action="device",
action_data={
"device_id": NOT_IMPLEMENTED_SV_MSG,
"domain": "mobile_app",
"type": "notify",
"title": "Hassio Status",
"message": "Hassio Is turning off...",
},
),
],
),
)
@@ -1,46 +1,19 @@
from pathlib import Path
from tempfile import mkdtemp
from unittest import TestCase
from src.config.AutomationLoader import (
from src.automations.types import (
AutomationActionNode,
AutomationConditionNode,
AutomationData,
AutomationLoader,
AutomationLoaderException,
AutomationMetdata,
load_automation,
)
from src.automations.parsers import (
_parse_conditions,
_parse_actions,
)
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
from src.yaml_serializer import (
from src.yaml_serializer.types import (
SecretValue,
load_hass_config,
IncludedYaml,
)
from tests.utils import SAMPLE_HA_PATH, create_dummy_config_folder
from src.yaml_serializer.types import HassConfig
class config_finder_tests(TestCase):
def test_simple_made_up_config(self):
pass
def test_find_sample_config(self):
secrets, config, file_path = load_hass_config(SAMPLE_HA_PATH)
self.assertIsInstance(config["template"], IncludedYaml)
self.assertIsInstance(config["binary_sensor"], IncludedYaml)
self.assertIsInstance(config["group"], IncludedYaml)
self.assertIsInstance(config["switch"], IncludedYaml)
self.assertEqual(secrets["myamazingpassword"], "holymoly")
self.assertEqual(
config["automation"].data[32]["trigger"][0]["device_id"],
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
)
self.assertEqual(file_path, SAMPLE_HA_PATH / "configuration.yaml")
class AutomationLoader_tests(TestCase):
class parser_tests(TestCase):
def test_various_condition_types(self):
raw_conditions = [
{"condition": "template", "value_template": "silly logic here"},
@@ -248,170 +221,115 @@ class AutomationLoader_tests(TestCase):
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
self.assertEqual(parsed, expected)
def test_load_automation_runs(self):
auto = list(
load_automation(
[
{
"id": "1616887975473",
"alias": "Home Assistant Off",
"description": "",
"trigger": [{"platform": "homeassistant", "event": "shutdown"}],
"condition": ["states(time.time) >= '10:00:00'"],
"action": [
{
"device_id": SecretValue("my_mobile_phone", "..."),
"domain": "mobile_app",
"type": "notify",
"title": "Hassio Status",
"message": "Hassio Is turning off...",
}
],
"mode": "single",
}
]
)
)
self.assertEqual(
auto[0],
AutomationData(
metadata=AutomationMetdata(
id="1616887975473",
alias="Home Assistant Off",
description="",
mode="single",
),
trigger=[{"platform": "homeassistant", "event": "shutdown"}],
condition=[
AutomationConditionNode(
condition="template",
condition_data={"value_template": "states(time.time) >= '10:00:00'"},
)
],
action=[
AutomationActionNode(
action="device",
action_data={
"device_id": NOT_IMPLEMENTED_SV_MSG,
"domain": "mobile_app",
"type": "notify",
"title": "Hassio Status",
"message": "Hassio Is turning off...",
},
)
],
),
)
# def test_finding_with_AutomationLoader(self):
# automation_loader = AutomationLoader(load_hass_config(SAMPLE_HA_PATH))
def test_finding_with_AutomationLoader(self):
automation_loader = AutomationLoader(load_hass_config(SAMPLE_HA_PATH))
# first_auto_with_find = list(automation_loader.find(limit=1))
# first_auto_with_get = automation_loader.get(0)
# self.assertEqual(first_auto_with_find[0], first_auto_with_get)
first_auto_with_find = list(automation_loader.find(limit=1))
first_auto_with_get = automation_loader.get(0)
self.assertEqual(first_auto_with_find[0], first_auto_with_get)
# kitchen_autos = list(automation_loader.find(alias="kitchen"))
# self.assertTrue(len(kitchen_autos), 3)
kitchen_autos = list(automation_loader.find(alias="kitchen"))
self.assertTrue(len(kitchen_autos), 3)
# self.assertIsNone(automation_loader.get(-1))
# self.assertIsNone(automation_loader.get(1_000))
# self.assertIsNone(automation_loader.get(31))
# self.assertEqual(len(automation_loader), 31)
self.assertIsNone(automation_loader.get(-1))
self.assertIsNone(automation_loader.get(1_000))
self.assertIsNone(automation_loader.get(35))
self.assertEqual(len(automation_loader), 34)
# def test_invalid_automations(self):
# with self.assertRaises(AutomationLoaderException):
# AutomationLoader(
# HassConfig(
# secrets={},
# config={
# "automation": "bob",
# },
# root_config_path=Path("."),
# )
# )
def test_invalid_automations(self):
with self.assertRaises(AutomationLoaderException):
AutomationLoader(
HassConfig(
secrets={},
config={
"automation": "bob",
},
root_config_path=Path("."),
)
)
# with self.assertRaises(AutomationLoaderException):
# AutomationLoader(
# HassConfig(
# secrets={},
# config={},
# root_config_path=Path("."),
# )
# )
with self.assertRaises(AutomationLoaderException):
AutomationLoader(
HassConfig(
secrets={},
config={},
root_config_path=Path("."),
)
)
# def test_overwriting_one_auto_in_separate_file(self):
# # create dummy data
# config_path = create_dummy_config_folder(
# auto=[
# AutomationData(
# metadata=AutomationMetdata(
# id="01",
# )
# ),
# AutomationData(
# metadata=AutomationMetdata(
# id="02",
# )
# ),
# AutomationData(
# metadata=AutomationMetdata(
# id="03",
# )
# ),
# ]
# )
# # check the data is loaded well
# automation_loader = AutomationLoader(load_hass_config(config_path))
# self.assertEqual(automation_loader.get(0).metadata.id, "01")
# self.assertEqual(automation_loader.get(1).metadata.id, "02")
# self.assertEqual(automation_loader.get(2).metadata.id, "03")
# self.assertIsNone(automation_loader.get(3))
# self.assertEqual(len(automation_loader), 3)
# # update auto #1
# automation_loader.save(
# 1,
# AutomationData(
# metadata=AutomationMetdata(id="02", alias="flush toilet!"),
# trigger=[{"event": "toilet.flushed"}],
# ),
# )
# # check that in-memory data is correctly updated
# self.assertEqual(automation_loader.get(1).metadata.id, "02")
# self.assertEqual(automation_loader.get(1).metadata.alias, "flush toilet!")
# self.assertListEqual(automation_loader.get(1).trigger, [{"event": "toilet.flushed"}])
def test_overwriting_one_auto_in_separate_file(self):
# create dummy data
config_path = create_dummy_config_folder(
auto=[
AutomationData(
metadata=AutomationMetdata(
id="01",
)
),
AutomationData(
metadata=AutomationMetdata(
id="02",
)
),
AutomationData(
metadata=AutomationMetdata(
id="03",
)
),
]
)
# check the data is loaded well
automation_loader = AutomationLoader(load_hass_config(config_path))
self.assertEqual(automation_loader.get(0).metadata.id, "01")
self.assertEqual(automation_loader.get(1).metadata.id, "02")
self.assertEqual(automation_loader.get(2).metadata.id, "03")
self.assertIsNone(automation_loader.get(3))
self.assertEqual(len(automation_loader), 3)
# update auto #1
automation_loader.save(
1,
AutomationData(
metadata=AutomationMetdata(id="02", alias="flush toilet!"),
trigger=[{"event": "toilet.flushed"}],
),
)
# check that in-memory data is correctly updated
self.assertEqual(automation_loader.get(1).metadata.id, "02")
self.assertEqual(automation_loader.get(1).metadata.alias, "flush toilet!")
self.assertListEqual(automation_loader.get(1).trigger, [{"event": "toilet.flushed"}])
# check that disk data is correctly updated
reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
self.assertEqual(len(automation_loader), 3)
self.assertEqual(reloaded_automation_loader.get(1).metadata.id, "02")
self.assertEqual(reloaded_automation_loader.get(1).metadata.alias, "flush toilet!")
self.assertListEqual(
reloaded_automation_loader.get(1).trigger, [{"event": "toilet.flushed"}]
)
# add new auto
automation_loader.save(
3,
AutomationData(
metadata=AutomationMetdata(id="newest", alias="testing autos"),
condition=[
AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})
],
),
)
# check that in-memory data is correctly updated
self.assertEqual(len(automation_loader), 4)
self.assertEqual(automation_loader.get(3).metadata.id, "newest")
self.assertEqual(automation_loader.get(3).metadata.alias, "testing autos")
self.assertListEqual(
automation_loader.get(3).condition,
[AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
)
# check that disk data is correctly updated
reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
self.assertEqual(len(reloaded_automation_loader), 4)
self.assertEqual(reloaded_automation_loader.get(3).metadata.id, "newest")
self.assertEqual(reloaded_automation_loader.get(3).metadata.alias, "testing autos")
self.assertListEqual(
reloaded_automation_loader.get(3).condition,
[AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
)
# # check that disk data is correctly updated
# reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
# self.assertEqual(len(automation_loader), 3)
# self.assertEqual(reloaded_automation_loader.get(1).metadata.id, "02")
# self.assertEqual(reloaded_automation_loader.get(1).metadata.alias, "flush toilet!")
# self.assertListEqual(
# reloaded_automation_loader.get(1).trigger, [{"event": "toilet.flushed"}]
# )
# # add new auto
# automation_loader.save(
# 3,
# AutomationData(
# metadata=AutomationMetdata(id="newest", alias="testing autos"),
# sequence=[
# AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})
# ],
# ),
# )
# # check that in-memory data is correctly updated
# self.assertEqual(len(automation_loader), 4)
# self.assertEqual(automation_loader.get(3).metadata.id, "newest")
# self.assertEqual(automation_loader.get(3).metadata.alias, "testing autos")
# self.assertListEqual(
# automation_loader.get(3).sequence,
# [AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
# )
# # check that disk data is correctly updated
# reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
# self.assertEqual(len(reloaded_automation_loader), 4)
# self.assertEqual(reloaded_automation_loader.get(3).metadata.id, "newest")
# self.assertEqual(reloaded_automation_loader.get(3).metadata.alias, "testing autos")
# self.assertListEqual(
# reloaded_automation_loader.get(3).sequence,
# [AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
# )
Whitespace-only changes.
@@ -0,0 +1,38 @@
from unittest import TestCase
from src.automations.types import AutomationData, AutomationMetdata
from src.hass_config.loader import HassConfig
from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder
example_hass_config = HassConfig(HA_CONFIG_EXAMPLE)
class loader_tests(TestCase):
def test_load_automation_from_config_yaml(self):
folder = create_dummy_config_folder(
[AutomationData(metadata=AutomationMetdata(id="test"))],
automation_in_conifguration_mode="inline",
)
hass_config = HassConfig(folder)
self.assertIsInstance(hass_config.configurations, dict)
self.assertTrue("automation" in hass_config.configurations)
self.assertEqual(hass_config.automations[0]["id"], "test")
def test_load_automation_from_config_yaml_inclue(self):
folder = create_dummy_config_folder(
[AutomationData(metadata=AutomationMetdata(id="test"))],
automation_in_conifguration_mode="include",
)
hass_config = HassConfig(folder)
self.assertIsInstance(hass_config.configurations, dict)
self.assertTrue("automation" in hass_config.configurations)
self.assertEqual(hass_config.automations[0]["id"], "test")
def test_load_automation_from_infer(self):
folder = create_dummy_config_folder(
[AutomationData(metadata=AutomationMetdata(id="test"))],
automation_in_conifguration_mode="none",
)
hass_config = HassConfig(folder)
self.assertIsInstance(hass_config.configurations, dict)
self.assertFalse("automation" in hass_config.configurations)
self.assertEqual(hass_config.automations[0]["id"], "test")
-80
View File
@@ -1,80 +0,0 @@
from unittest import TestCase
from src.db.connection import HassDatabase
from src.db import entities
from tests.utils import SAMPLE_DB_PATH
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": "sureme"},
):
self.assertTrue(expected.count(entity.entity_id) > 0)
self.assertEqual(entity.domain, "sensor")
self.assertEqual(entity.attributes["state_class"], "measurement")
@@ -1,6 +1,6 @@
import json
from unittest import TestCase
from src.yaml_serializer import SecretValue
from src.yaml_serializer.types import SecretValue
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
@@ -3,10 +3,12 @@ from pathlib import Path
from unittest import TestCase
from src.yaml_serializer import (
dump_yaml,
load_yaml,
)
from src.yaml_serializer.types import (
IncludedYaml,
SecretValue,
IncludedYamlDir,
simple_load_yaml,
)
@@ -15,15 +17,11 @@ class dumping_yamls_tests(TestCase):
yaml = dump_yaml(
{
"automations": IncludedYaml(
"automations.yaml",
Path("/config/automations.yaml"),
[
{"id": "wow", "alias": "NO!"},
],
Path("automations.yaml"),
)
}
)
self.assertEqual(yaml, "automations: !include automations.yaml\n")
self.assertEqual(yaml, f"automations: !include {Path('automations.yaml').absolute()}\n")
def test_dumping_secrets(self):
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
@@ -34,7 +32,7 @@ class dumping_yamls_tests(TestCase):
self.assertEqual(yaml, "sensor: !include_dir_list sensors/*yaml\n")
def test_load_stub(self):
yaml_obj = simple_load_yaml(
yaml_obj = load_yaml(
io.StringIO(
"""
name: cool
@@ -50,7 +48,7 @@ class dumping_yamls_tests(TestCase):
"name": "cool",
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
"automations": IncludedYaml("automations.yaml", Path("/"), "n/a"),
"automations": IncludedYaml(Path("automations.yaml").absolute()),
},
)
self.assertEqual(
@@ -58,5 +56,5 @@ class dumping_yamls_tests(TestCase):
"name: cool\n"
"google_password: !secret gpass\n"
"sensor: !include_dir_list sensors/*yaml\n"
"automations: !include automations.yaml\n",
f"automations: !include {Path('automations.yaml').absolute()}\n",
)
+10 -9
View File
@@ -1,21 +1,21 @@
from tempfile import mkdtemp
from pathlib import Path
from typing import List, Optional
from src.config.AutomationLoader import AutomationData
from typing import List, Literal, Optional
from src.automations.types import AutomationData
from src.yaml_serializer import dump_yaml
from src.yaml_serializer.types import IncludedYaml
THIS_FOLDER = Path(__file__).parent
SAMPLE_HA_PATH = THIS_FOLDER / "samples"
SAMPLE_DB_PATH = SAMPLE_HA_PATH / "home-assistant_v2.db"
SAMPLES_FOLDER = THIS_FOLDER / "samples"
HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "example_config_folder"
def create_dummy_config_folder(
auto: List[AutomationData],
secrets: Optional[dict] = None,
other_config: Optional[dict] = None,
include_auto_in_configuration: bool = False,
automation_in_conifguration_mode: Literal["include", "inline", "none"] = "include",
) -> Path:
"""Creates a dummy /config structure for testing
@@ -23,7 +23,7 @@ def create_dummy_config_folder(
auto (List[AutomationData]): automations
secrets (Optional[dict], optional): dictionary of secrets
other_config (Optional[dict], optional): some config stuff to place into configuration.yaml. Defaults to None.
include_auto_in_configuration (bool, optional): whether to !include automation as a separete file or in the configuration yaml. Defaults to False.
automation_in_conifguration_mode (Literal['include', 'inline', 'none'], optional): whether to !include automation as a separete file or in the configuration yaml. Defaults to inline.
Returns:
Path: [description]
@@ -31,12 +31,13 @@ def create_dummy_config_folder(
root_folder = Path(mkdtemp())
auto_prims = [a.to_primitive() for a in auto]
configuration_yaml = {**other_config} if other_config is not None else {}
if include_auto_in_configuration:
if automation_in_conifguration_mode == "inline":
configuration_yaml["automation"] = auto_prims
else:
elif automation_in_conifguration_mode == "include":
configuration_yaml["automation"] = IncludedYaml(
"automations.yaml", root_folder / "automations.yaml", auto
Path(root_folder / "automations.yaml"),
)
if automation_in_conifguration_mode != "inline":
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
if secrets is not None:
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))