manager code
This commit is contained in:
1 parent
8adb86b2ec
commit
ef5715ea6c
9 files changed
+66
-35
No files matched your search
@@ -1,5 +1,5 @@
|
||||
from itertools import chain
|
||||
from typing import Iterable
|
||||
from typing import Iterable, Iterator
|
||||
from src.json_serializer import normalize_obj
|
||||
from .types import AutomationData, AutomationMetdata
|
||||
from .parsers import _parse_actions
|
||||
@@ -7,7 +7,7 @@ from .parsers import _parse_actions
|
||||
# Single loader
|
||||
def load_automation(
|
||||
automations_raw_data: Iterable[dict],
|
||||
) -> Iterable[AutomationData]:
|
||||
) -> Iterator[AutomationData]:
|
||||
"""Converts an automation dictionary loaded directly from the config into
|
||||
several python typed objects
|
||||
|
||||
@@ -15,7 +15,7 @@ def load_automation(
|
||||
automations_raw_data (Iterable[dict])
|
||||
|
||||
Returns:
|
||||
Iterable[AutomationData]
|
||||
Iterator[AutomationData]
|
||||
|
||||
"""
|
||||
for auto_raw in automations_raw_data:
|
||||
|
||||
@@ -1,3 +1,28 @@
|
||||
from typing import Iterator, Optional
|
||||
from src.automations.types import AutomationData
|
||||
from src.hass_config.loader import HassConfig
|
||||
from .loader import load_automation
|
||||
|
||||
|
||||
class AutomationManager:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
def __init__(self, hass_config: HassConfig) -> None:
|
||||
self.hass_config = hass_config
|
||||
|
||||
def find(
|
||||
self,
|
||||
offset: int = 0,
|
||||
limit: int = 10,
|
||||
) -> Iterator[AutomationData]:
|
||||
for i, auto in enumerate(self.hass_config.automations):
|
||||
if i < offset:
|
||||
continue
|
||||
if i > offset + limit:
|
||||
break
|
||||
yield from load_automation([auto])
|
||||
|
||||
def get(self, index: int) -> Optional[AutomationData]:
|
||||
if index >= 0:
|
||||
for i, auto in enumerate(self.hass_config.automations):
|
||||
if i == index:
|
||||
return next(load_automation(([auto])))
|
||||
return None
|
||||
@@ -21,8 +21,8 @@ class HassConfig:
|
||||
if "automation" in config:
|
||||
automation_ref = config["automation"]
|
||||
if isinstance(automation_ref, IncludedYaml):
|
||||
automation_path = automation_ref.path
|
||||
automation_path = self.root_path / automation_ref.path_str
|
||||
else:
|
||||
return list(config["automation"])
|
||||
return config["automation"]
|
||||
with automation_path.open("r") as f:
|
||||
return list(load_yaml(f)) # type: ignore
|
||||
return load_yaml(f) # type: ignore
|
||||
@@ -9,15 +9,15 @@ class IncludedYaml(NamedTuple):
|
||||
this data type will represent it.
|
||||
"""
|
||||
|
||||
path: Path # original node path
|
||||
path_str: str # original node path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
||||
return representer.represent_scalar("!include", str(node.path.absolute()))
|
||||
return representer.represent_scalar("!include", node.path_str)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, _constructor, node: Node):
|
||||
return cls(Path(node.value).absolute())
|
||||
return cls(node.value)
|
||||
|
||||
|
||||
class SecretValue(NamedTuple):
|
||||
|
||||
Reference in new issue
Block a user