Fix time_date sensor to update at predictable intervals#7644
Conversation
|
@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. |
There was a problem hiding this comment.
You can just do this:
from datetime import timedelta
return now + timedelta(seconds=interval)There was a problem hiding this comment.
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.
- 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.
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ah. That does make the code more readable. Thanks!
|
Thanks! 🐬 🍺 🎉 🌮 |
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:
toxrun successfully. Your PR cannot be merged unless tests pass