From d5714808edd841a0649630c3a8271505c84f0e37 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:11:45 -0500 Subject: [PATCH] added food.meal --- .gitignore | 3 ++- conf/apps/food-repo/src/food/meal.py | 19 ++++++++++++++++++ conf/apps/restaurant/restaurant.py | 7 +++++-- test_startup.py | 29 ++++++++++++++-------------- 4 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 conf/apps/food-repo/src/food/meal.py diff --git a/.gitignore b/.gitignore index f071240..9ab9cc9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -.pytest_cache \ No newline at end of file +.pytest_cache +*.egg-info \ No newline at end of file diff --git a/conf/apps/food-repo/src/food/meal.py b/conf/apps/food-repo/src/food/meal.py new file mode 100644 index 0000000..510f683 --- /dev/null +++ b/conf/apps/food-repo/src/food/meal.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass, field +from typing import List + +from appdaemon.adapi import ADAPI + +from .eggs import Eggs +from .ham import Ham + + +class GreenEggs(Eggs): + pass + + +@dataclass +class Meal: + dishes: List[ADAPI] = field(init=False) + + def __post_init__(self): + self.dishes = [GreenEggs, Ham] diff --git a/conf/apps/restaurant/restaurant.py b/conf/apps/restaurant/restaurant.py index ed7afad..eb65c3e 100644 --- a/conf/apps/restaurant/restaurant.py +++ b/conf/apps/restaurant/restaurant.py @@ -1,7 +1,10 @@ from appdaemon.adapi import ADAPI - from food import Eggs +from food.meal import Meal + class Restaurant(ADAPI): def initialize(self): - self.log(f'{self.__class__.__name__} initialized with {Eggs}') + meal = Meal() + eggs: Eggs = meal.dishes[0] + self.log(f'{self.__class__.__name__} initialized with {eggs}') diff --git a/test_startup.py b/test_startup.py index 4b738e2..88aaffa 100644 --- a/test_startup.py +++ b/test_startup.py @@ -31,21 +31,6 @@ def ad_system(base_config) -> AppDaemon: return ad -# def test_dependency_graph(): -# app_dir = Path('/home/john/conf/apps') -# files = app_dir.rglob('*.yaml') -# files = (f for f in files if f.stem != 'secrets' and f.stem != 'appdaemon') - -# cfg = AllAppConfig.from_config_files(files) -# graph = cfg.depedency_graph() -# rev = cfg.reversed_dependency_graph() - -# assert 'App1Foo' in graph['App2'], 'App2 is not dependent on App1Foo' -# assert 'App2' in rev['App1Foo'], 'App1 is not a predicate of App1Foo' - -# print(graph) - - async def delayed_stop(ad: AppDaemon, delay: float): await asyncio.sleep(delay) ad.stop() @@ -58,6 +43,17 @@ def get_load_order(caplog: pytest.LogCaptureFixture) -> List[str]: return record.args[0] +def validate_app_dependencies(ad: AppDaemon): + graph = ad.app_management.app_config.depedency_graph() + assert 'hello_world' in graph['food_app'] + + +def validate_module_dependencies(ad: AppDaemon): + graph = ad.app_management.module_dependencies + assert 'food' in graph['restaurant'] + assert 'food.meal' in graph['restaurant'] + + def validate_module_load_order(ad: AppDaemon, module_load_order: List[str]): dependency_graph = ad.app_management.module_dependencies for node, deps in dependency_graph.items(): @@ -85,6 +81,9 @@ def test_startup(ad_system: AppDaemon, caplog: pytest.LogCaptureFixture): assert "Started 'hello_world'" in caplog.text assert 'App initialization complete' in caplog.text + validate_app_dependencies(ad_system) + validate_module_dependencies(ad_system) + module_load_order = get_load_order(caplog) validate_module_load_order(ad_system, module_load_order)