Skip to content

Commit

Permalink
changed add_sequences to take a SequenceConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jsl12 committed Feb 1, 2025
1 parent c623d72 commit d0f53fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
12 changes: 11 additions & 1 deletion appdaemon/app_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from appdaemon.dependency import DependencyResolutionFail, get_full_module_name
from appdaemon.dependency_manager import DependencyManager
from appdaemon.models.config import AllAppConfig, AppConfig, GlobalModule
from appdaemon.models.config.app import SequenceConfig
from appdaemon.models.config.sequence import Sequence
from appdaemon.models.internal.file_check import FileCheck

from . import exceptions as ade
Expand Down Expand Up @@ -151,6 +153,10 @@ def loaded_globals(self) -> set[str]:
if isinstance(cfg, GlobalModule) and cfg.module_name in sys.modules
)

@property
def sequence_config(self) -> SequenceConfig | None:
return self.app_config.root.get('sequence')

@property
def valid_apps(self) -> set[str]:
return self.running_apps | self.loaded_globals
Expand Down Expand Up @@ -502,7 +508,7 @@ def add_plugin_object(self, name: str, object: "PluginBase", use_dictionary_unpa
use_dictionary_unpacking=use_dictionary_unpacking,
)

def init_sequence_object(self, name: str, object):
def init_sequence_object(self, name: str, object: Sequence):
"""Add the sequence object to the internal dictionary of ``ManagedObjects``"""
self.objects[name] = ManagedObject(
type="sequence",
Expand Down Expand Up @@ -758,6 +764,7 @@ async def check_app_updates(self, plugin: str = None, mode: UpdateMode = UpdateM
if mode == UpdateMode.INIT:
await self._process_import_paths()
await self._init_dep_manager()
await self._init_sequences()

update_actions = UpdateActions()

Expand Down Expand Up @@ -1031,6 +1038,9 @@ async def safe_import(self: "AppManagement"):

await safe_import(self)

async def _init_sequences(self):
await self.AD.sequences.add_sequences(self.sequence_config)

def apps_per_module(self, module_name: str) -> Set[str]:
"""Finds which apps came from a given module name.
Expand Down
19 changes: 11 additions & 8 deletions appdaemon/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from appdaemon.entity import Entity
from appdaemon.exceptions import TimeOutException
from appdaemon.models.config.sequence import SequenceConfig

if TYPE_CHECKING:
from .appdaemon import AppDaemon
Expand Down Expand Up @@ -65,16 +66,18 @@ async def run_sequence_service(self, namespace, domain, service, kwargs):
elif service == "cancel" and isinstance(entity_id, str):
return await self.cancel_sequence(entity_id)

async def add_sequences(self, sequences: dict[str, dict]):
for seq_name, seq_cfg in sequences.items():
async def add_sequences(self, sequences: SequenceConfig):
self.logger.debug(f'Adding sequences: {set(sequences.root.keys())}')

for seq_name, seq_cfg in sequences.root.items():
attributes = {
"friendly_name": seq_cfg.get("name", seq_name),
"loop": seq_cfg.get("loop", False),
"steps": seq_cfg["steps"],
"friendly_name": seq_cfg.name or seq_name,
"loop": seq_cfg.loop,
"steps": seq_cfg.steps
}

if sequence_namespace := seq_cfg.get("namespace") is not None:
attributes["namespace"] = sequence_namespace
if seq_cfg.namespace is not None:
attributes["namespace"] = seq_cfg.namespace

entity = f"sequence.{seq_name}"
if self.sequence_exists(entity):
Expand All @@ -86,7 +89,7 @@ async def add_sequences(self, sequences: dict[str, dict]):
await self.add_entity(entity, state="idle", **attributes)

# create sequence objects
self.AD.app_management.init_sequence_object(f"sequence_{seq_name}", None)
self.AD.app_management.init_sequence_object(f"sequence_{seq_name}", seq_cfg)

async def remove_sequences(self, sequences):
if not isinstance(sequences, list):
Expand Down

0 comments on commit d0f53fe

Please sign in to comment.