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

Write state if schedule update state from async context #31758

Merged
merged 4 commits into from
Feb 13, 2020

Conversation

balloob
Copy link
Member

@balloob balloob commented Feb 12, 2020

Breaking change

Proposed change

If we're in async land scheduling a state update and we know we're not going to trigger an update, write the state directly.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@balloob balloob requested a review from a team as a code owner February 12, 2020 18:09
@probot-home-assistant probot-home-assistant bot added core small-pr PRs with less than 30 lines. labels Feb 12, 2020
@elupus
Copy link
Contributor

elupus commented Feb 12, 2020

Seems we should drop the force feature from the function and replace all references of force=false with just a write call?

The force naming is a bit confusing anyway.

@balloob
Copy link
Member Author

balloob commented Feb 12, 2020

That would be a too big of a breaking change for custom components I think.

@elupus
Copy link
Contributor

elupus commented Feb 12, 2020

Suppose it would have to be done by marking it deprecated, and just having it call two new functions. But sure, probably not worth the hassle as a cleanup action.

@balloob
Copy link
Member Author

balloob commented Feb 12, 2020

@SukramJ I am running into a failed test with Homematic IP cloud: tests/components/homematicip_cloud/test_alarm_control_panel.py. I've looked at it but I can't seem to figure out what's wrong. Log at https://dev.azure.com/home-assistant/Core/_build/results?buildId=27085&view=logs&j=67510a0b-4400-5e74-cb95-9572caf5ca54&t=f8fe93fb-5ebb-59cc-455e-c0f2d9ad3a42&l=292

This PR makes calls to Entity.async_schedule_update_ha_state() faster if they don't need an update.

@balloob
Copy link
Member Author

balloob commented Feb 12, 2020

The reason the other tests were failing is that we call 1 async function less. That 1 async function was waited for when we call hass.async_block_till_done(), allowing for some other work to finish. That no longer happens as it's now callbacks all the way down.

@probot-home-assistant
Copy link

Hey there @ktnrg45, mind taking a look at this pull request as its been labeled with a integration (ps4) you are listed as a codeowner for? Thanks!

if force_refresh:
self.hass.async_create_task(self.async_update_ha_state(force_refresh))
else:
self.async_write_ha_state()
Copy link
Member

Choose a reason for hiding this comment

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

The name of async_schedule_update_ha_state doesn't match what happens here now.

Copy link
Member Author

Choose a reason for hiding this comment

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

True. But the intention of the method is still true: write the state to HA without a guarantee that it's available after the function call is done. I'll update the doc string

Copy link
Member

Choose a reason for hiding this comment

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

We're now removing the choice from the developer if the state should be updated in this iteration or a later iteration of the event loop. That might be ok. 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that's not something for the developer to decide. The developer can only indicate that it has new data and wants to be updated. I can't think of a reason for a developer wanting to let us know, but not write it to the state machine as soon as possible.


@callback
Copy link
Member

Choose a reason for hiding this comment

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

update_list is not async safe. We're loading games which loads a json file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed.

@SukramJ
Copy link
Contributor

SukramJ commented Feb 13, 2020

@balloob
Copy link
Member Author

balloob commented Feb 13, 2020

I'm a bit confused why it needs to be forced now . So by writing a state faster to the state machine than before we can end up requiring to do a full update? Was there a race condition and the full update was always needed?

@SukramJ
Copy link
Contributor

SukramJ commented Feb 13, 2020

Yes you are right force is not needed but it currently requires self.hass.async_create_task(self.async_update_ha_state())
I'am in in the office now, so i can have a further look at it this evening (CET).

@balloob balloob merged commit 9e7185c into dev Feb 13, 2020
@delete-merged-branch delete-merged-branch bot deleted the write-state-little-faster branch February 13, 2020 18:22
@SukramJ
Copy link
Contributor

SukramJ commented Feb 13, 2020

10 Minutes too fast. I create a new PR to avoid the force.

@lock lock bot locked and limited conversation to collaborators Feb 14, 2020
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.

5 participants