Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Expose running tasks to scripts #130

Closed
valsr opened this issue Dec 28, 2020 · 2 comments
Closed

Feature Request: Expose running tasks to scripts #130

valsr opened this issue Dec 28, 2020 · 2 comments

Comments

@valsr
Copy link

valsr commented Dec 28, 2020

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
@dlashua
Copy link
Contributor

dlashua commented Dec 28, 2020

I like the idea of having some helper methods to dig deeper into the running state of pyscript.

In the mean time, for the scenario you describe above, task.unique can handle the job.

@state_trigger('binary_sensor.door == "off"')
@task_unique('light_dimming')
def dim_lights():
  light.living.turn_on(brightness_pct=50)
  task.sleep(60 * 15)
  light.living.turn_on(brightness_pct=25)

@state_trigger('binary_sensor.door == "on"')
@task_unique('light_dimming')
def off_lights():
  light.living.turn_off()

@craigbarratt
Copy link
Member

In issue #112 we recently added some task functions for creating, cancelling or waiting on tasks. See the latest docs.

I like your suggestions of adding a few more helper functions. I was thinking of something like this:

  • task.current returns the task's own id
  • task.name2id returns the task's id given a name it passed to task.unique. With no arguments it returns a dict mapping names to task ids

I don't think it makes sense to return tasks that haven't named themselves via task.unique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants