Skip to content

Restore automation last_triggered with initial_state override#24400

Merged
balloob merged 4 commits into
devfrom
frenck-2019-0063
Jun 8, 2019
Merged

Restore automation last_triggered with initial_state override#24400
balloob merged 4 commits into
devfrom
frenck-2019-0063

Conversation

@frenck
Copy link
Copy Markdown
Member

@frenck frenck commented Jun 8, 2019

Breaking Change:

When setting the startup state of automation using the initial_state setting, it would also mean that the last_triggered attribute of the automation was lost. This behavior has been solved. It can potentially break automations if you rely on it being reset on startup.

Description:

Automations are restored from the state storage only when initial_state isn't set.
However, the last_triggered is also restored from the state storage.

The current logic, makes the last_triggered vanish when the user has set an initial_state.

This PR changes this behavior, by first trying to load the previous state from the state storage and later apply the initial_state override. This preserves the last_triggered attribute.

Related issue (if applicable): fixes #12672 (old closed, never resolved).

Pull request with documentation for home-assistant.io (if applicable): n/a

Example entry for configuration.yaml (if applicable):

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

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

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@ghost
Copy link
Copy Markdown

ghost commented Jun 8, 2019

Hey there @home-assistant/core, mind taking a look at this pull request as its been labeled with a integration (automation) you are listed as a codeowner for? Thanks!

This is a automatic comment generated by codeowners-mention to help ensure issues and pull requests are seen by the right people.

Comment thread tests/components/automation/test_init.py Outdated
Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

ok to merge when comment addressed

@frenck
Copy link
Copy Markdown
Member Author

frenck commented Jun 8, 2019

❌ ci/circleci: static-check, because of a styling issue in an unrelated file:

homeassistant/components/zha/sensor.py:24:1: E302 expected 2 blank lines, found 1

Introduced in #24373

@frenck frenck force-pushed the frenck-2019-0063 branch from 2680fe6 to fdf4161 Compare June 8, 2019 14:33
@frenck
Copy link
Copy Markdown
Member Author

frenck commented Jun 8, 2019

Rebased onto dev to fix build error.

state = hass.states.get('automation.hello')
assert state
assert state.state == STATE_OFF
assert state.attributes.get('last_triggered') is None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you asserting that last_triggered is not present in the dictionary or that it's present but with a value of None? This should be made tighter to verify the intended behavior... i.e. assert 'last_triggered' not in state.attributes or assert state.attributes['last_triggered'] is None.

Copy link
Copy Markdown
Member Author

@frenck frenck Jun 8, 2019

Choose a reason for hiding this comment

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

The last_triggered attribute is hardcoded to be returned always in the state attributes of AutomationEntity. It will always be there, by default initialized as None.

It really won't matter, secondly, this is consistent with all other tests.
Third, the test_automation_restore_state test should cover that case IMHO, which it doesn't either.

@balloob balloob merged commit 9235b52 into dev Jun 8, 2019
@delete-merged-branch delete-merged-branch Bot deleted the frenck-2019-0063 branch June 8, 2019 19:48
@balloob balloob mentioned this pull request Jun 26, 2019
alandtse pushed a commit to alandtse/home-assistant that referenced this pull request Oct 12, 2019
…ssistant#24400)

* Restore automation last_triggered with initial_state override

* Made test async/await

* Fixes linter warning

* Update test_init.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automation last_triggered not being updated

4 participants