Skip to content

Allow wait template to run the remainder of the script#15836

Merged
balloob merged 5 commits intohome-assistant:devfrom
lhovo:dev
Aug 13, 2018
Merged

Allow wait template to run the remainder of the script#15836
balloob merged 5 commits intohome-assistant:devfrom
lhovo:dev

Conversation

@lhovo
Copy link
Copy Markdown
Contributor

@lhovo lhovo commented Aug 5, 2018

New feature: allow wait template to run the remainder of the script

Breaking changes
The wait template will now continue to run the remainder of the script on timeout, the original functionality can be gained by setting continue_on_timeout: false.

Description:

This allows the Wait Template script to use a timer to continue running the script if timeout is reached vs the current behaviour of exiting.
This is achieved by setting a new flag 'proceed' to 'true' in the config.
By default this flag is set to 'false' and is optional to ensure this is not a breaking change.
For this flag to work the optional 'timeout' is required to be set also.

Pull request in home-assistant.github.io with documentation (if applicable):
home-assistant/home-assistant.io#5954

Example entry for configuration.yaml (if applicable):

# if the front door is open and it's night time turn on the light
# when the occupant reaches the first sensor, turn the light off
# if the timeout triggers, turn the light off anyway
automation:
  - id: '15'
    alias: Door Open Light On
    trigger:
    - entity_id: binary_sensor.front
      platform: state
      to: 'on'
    condition:
    - condition: state
      entity_id: light.entrance
      state: 'off'
    - after: '17:00'
      before: '6:00'
      condition: time
    action:
    - data:
        entity_id:
        - light.entrance
      service: homeassistant.turn_on
    - wait_template: '{{ is_state("binary_sensor.entrance", "on") }}'
      timeout: 00:01:00
      continue_on_timeout: 'true'
    - alias: ''
      data:
        entity_id:
        - light.entrance
      service: homeassistant.turn_off

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @lhovo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@lhovo lhovo requested a review from a team as a code owner August 5, 2018 02:45
@homeassistant homeassistant added cla-needed core small-pr PRs with less than 30 lines. labels Aug 5, 2018
@ghost ghost added the in progress label Aug 5, 2018

@callback
def async_script_wait(entity_id, from_s, to_s):
def async_script_wait(entity_id, from_s = None, to_s = None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected spaces around keyword / parameter equals


from homeassistant.const import (
CONF_PLATFORM, CONF_SCAN_INTERVAL, TEMP_CELSIUS, TEMP_FAHRENHEIT,
CONF_PLATFORM, CONF_PROCEED, CONF_SCAN_INTERVAL, TEMP_CELSIUS, TEMP_FAHRENHEIT,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (83 > 79 characters)

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @lhovo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@awarecan
Copy link
Copy Markdown
Contributor

awarecan commented Aug 5, 2018

Thanks for your contribution. There are several outstanding issues in your pull request you have to resolve before review.

  1. Please do not change file mod 100644 → 100755
  2. Please fill in PR template's "Example entry for configuration.yaml" section, in your case, maybe template and script
  3. Please add unit test, home-assistant/tests/helpers/test_script.py
  4. Please fix the link to your document PR (remove the space between # and issue number)

@fabaff fabaff changed the title New feature: Allow wait template to run the remainder of the script Allow wait template to run the remainder of the script Aug 5, 2018
@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 5, 2018

  1. Please do not change file mod 100644 → 100755
    Whoops this was a mistake

  2. Please fill in PR template's "Example entry for configuration.yaml" section, in your case, maybe template and script
    I think I have done this now?

  3. Please add unit test, home-assistant/tests/helpers/test_script.py
    Added

  4. Please fix the link to your document PR (remove the space between # and issue number)
    Done

Thanks for your patience walking me through my first home-assistant PR

@awarecan
Copy link
Copy Markdown
Contributor

awarecan commented Aug 6, 2018

Regarding Example entry for configuration.yaml

new flag 'proceed' to 'true' in the config.

I want to see the "example config" to demo how to set proceed

Wait Template script to use a timer to continue running the script

I want to see a example to demo "Wait Template script to use a timer"

It should be something similar with your unit test case, but write it in yaml format.

CONF_PENDING_TIME = 'pending_time'
CONF_PIN = 'pin'
CONF_PLATFORM = 'platform'
CONF_PROCEED = 'proceed'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proceed is not self-explain to me. How about continue_on_timeout

And it should resident in helpers/script.py together with CONF_WAIT_TEMPLATE.

self.hass.async_add_job(self._change_listener)

# Allow proceed to be false by default
proceed = False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need declare here, should put it inside if CONF_TIMEOUT

# Check if we want to proceed to execute
# the script after the timeout
# or exit as the default behaviour
if proceed:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if action.get(CONF_PROCEED, False):

# Check if we want to proceed to execute
# the script after the timeout
# or exit as the default behaviour
if proceed:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if I were you, I will extend self._async_set_timeout method to accept the 3rd parameter as the callback method when timeout occurred. If the 3rd parameter is None, then keep current logic. If we want to continue run after timeout, we can simply pass in async_script_wait.

@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 6, 2018

Thanks, I was battling with the correct word to use.
I was originally trying to stay away from 'continue' as it's a key word in programming.

I have moved the "if CONF_CONTINUE" inside the _async_set_timeout.
It's passing the tests presently, but I would prefer to integrate it with my HA to test it some more to ensure the logic is 100% correct (assuming the test is correct, it should be fine)

If your happy with the change I will update the supporting docs with the new variable name

I will endeavour to do this tomorrow

Copy link
Copy Markdown
Contributor

@awarecan awarecan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, doc need to change reflect latest change

@lhovo lhovo changed the title Allow wait template to run the remainder of the script WIP Allow wait template to run the remainder of the script Aug 7, 2018
@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 7, 2018

Not convinced the code is working as expected, when running on my home system.
Will do more testing soon, in the meantime I'm going to mark this as WIP

@amelchio
Copy link
Copy Markdown
Contributor

amelchio commented Aug 9, 2018

I think we should consider for this to be the default.

@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 9, 2018

I would love for this to be the default, though that would make it a breaking change.

@balloob
Copy link
Copy Markdown
Member

balloob commented Aug 9, 2018

I agree with @amelchio that this should be the default behavior.

@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 12, 2018

I have spent the last week testing the present code and it is working as expected on my system

@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 12, 2018

As I have only just started to use Home Assistant and contribute back,
I am unsure on the steps required to change the default behaviour.

I can change the code and update the docs, is there anything else that needs to be done?
Like a vote between core developers?

@amelchio
Copy link
Copy Markdown
Contributor

Having @balloob on board is enough :)

You tag the PR with "breaking change" and add a small note at the top describing the incompatibility. This note is copied to the release note and that's that.

@lhovo lhovo changed the title WIP Allow wait template to run the remainder of the script Allow wait template to run the remainder of the script Aug 12, 2018
@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 12, 2018

Either I am missing how to add a label, or I don't have permission.
Could someone add the breaking-change label for me?

Thanks

@balloob balloob merged commit 6aee535 into home-assistant:dev Aug 13, 2018
@ghost ghost removed the in progress label Aug 13, 2018
@balloob
Copy link
Copy Markdown
Member

balloob commented Aug 13, 2018

🎉 thanks for the great contribution!

@lhovo
Copy link
Copy Markdown
Contributor Author

lhovo commented Aug 13, 2018

Thanks for being so inviting to your project!

@balloob balloob mentioned this pull request Aug 29, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
…t#15836)

* Adding new feature to allow a wait template to run the remainer of the script on timeout

* Styling changes

* Fixing file permissions, adding test for new code

* changed variable name, refactored script to pass information into async_set_timeout

* Changing the default behaviour to continue to run the script after timeout
vrih pushed a commit to vrih/home-assistant that referenced this pull request Sep 29, 2018
…t#15836)

* Adding new feature to allow a wait template to run the remainer of the script on timeout

* Styling changes

* Fixing file permissions, adding test for new code

* changed variable name, refactored script to pass information into async_set_timeout

* Changing the default behaviour to continue to run the script after timeout
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants