Skip to content

Fix time_date sensor to update at predictable intervals#7644

Merged
balloob merged 2 commits into
home-assistant:devfrom
stu-gott:time_date
May 23, 2017
Merged

Fix time_date sensor to update at predictable intervals#7644
balloob merged 2 commits into
home-assistant:devfrom
stu-gott:time_date

Conversation

@stu-gott
Copy link
Copy Markdown
Contributor

Description:

Change the time_date sensor to update when the time or date actually changes. Previous behavior asserts no control over when updates occur so they happen randomly/sporadically.

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): No docs impact

Checklist:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@mention-bot
Copy link
Copy Markdown

@stu-gott, thanks for your PR! By analyzing the history of the files in this pull request, we identified @molobrakos, @balloob and @fabaff to be potential reviewers.

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.

You can just do this:

from datetime import timedelta

return now + timedelta(seconds=interval)

Copy link
Copy Markdown
Contributor Author

@stu-gott stu-gott May 18, 2017

Choose a reason for hiding this comment

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

The intent of this code is to have the sensor update approximately at the time that the minute/date actually changes.
I had two specific cases in mind that a straightforward timedelta fails to address.

  1. Home Assistant restart: we have no way of knowing how many seconds into a minute a sensor will be instantiated. an extreme example is Home Assistant being restarted at 2 PM local time for a date sensor. waiting a full 86400 seconds to update state means the sensor will read the previous day's setting until 2 PM.
  2. Time skew as Home Assistant runs. because it takes a finite amount of time for this code to run (and the system is cooperative multitasking), blindly asking for a 60-second sleep interval forever means this function will be called late. by re-computing how much time is left in the interval, we will stay on track.

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.

You're right, I didn't fully copy your algorithm. I was merely trying to point out that Python has a built-in class to work with time periods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah. That does make the code more readable. Thanks!

@balloob balloob merged commit 54c45f8 into home-assistant:dev May 23, 2017
@balloob
Copy link
Copy Markdown
Member

balloob commented May 23, 2017

Thanks! 🐬 🍺 🎉 🌮

@balloob balloob mentioned this pull request Jun 2, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Sep 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants