Skip to content

Feature Request: Expose running tasks to scripts #130

Closed
@valsr

Description

@valsr

As I am adding long running tasks (in the like dim light wait 5 minutes dim some more), I find it that other tasks may need to know about what is currently running and act accordingly (even more so with more complex automations). For a sample scenario:

  • task 1: runs at 21:45 with door closed -> dims light to 50%, waits 15 minutes and dims lights to 25%
  • task 2: runs at 21:45 if door is opened -> turn off all lights

Currently this means that task 1 may dim the lights. After door opens and the lights turn off the task 1 will resume and turn on the lights. In this case it will be useful to expose running tasks (preferably named ones) and be able to cancel them.

I am currently simply augmenting the functions.py to add the following snippet

cls.functions.update(
   {
        ...
       "task.named_tasks": cls.list_named_tasks,
       "task.tasks": cls.list_tasks,
       "task.current": cls.list_current,
    }
...
    @classmethod
    async def list_current(cls):
        """Return the current running task name."""
        return asyncio.current_task()

    @classmethod
    async def list_tasks(cls):
        """Return currently running tasks."""

        return cls.our_tasks

    @classmethod
    async def list_named_tasks(cls):
        """Return named running tasks."""

        return cls.unique_name2task

This rough-in seems to do the job and I am able to list currently running task with some caveats:

  • service calls are non-blocking so for anything that runs over x minutes I have to add a task.sleep for x minutes
  • tasks by themselves don't know anything about their names

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions