Repository: sabeechen/hassio-google-drive-backup
Issue: Snapshot time of day not at specified time

I have snapshot_time_of_day: '12:00' but snapshots are never taken at that time. Snapshots are taken up to 55 minutes after that time. Last days the time was:
12:05
12:18
12:20
12:54
12:41

I'm on the latest version of the add-on v0.100.0
My config:
max_snapshots_in_hassio: 7
max_snapshots_in_google_drive: 14
days_between_snapshots: 1
use_ssl: false
generational_day_of_week: sun
snapshot_time_of_day: '12:00'
snapshot_name: '{year}-{month}-{day} {hr24}:{min} {version_ha} {type}'
send_error_reports: false
confirm_multiple_deletes: false
generational_days: 7
generational_weeks: 4
generational_months: 12
generational_years: 5
background_color: '#ffffff'

[hassio-google-drive-backup.log](https://github.com/sabeechen/hassio-google-drive-backup/files/4345285/hassio-google-drive-backup.log)


Changed files:
hassio-google-drive-backup/tests/test_model.py, hassio-google-drive-backup/tests/test_coordinator.py, hassio-google-drive-backup/backup/model/model.py

diff --git a/hassio-google-drive-backup/backup/model/model.py b/hassio-google-drive-backup/backup/model/model.py
index c5c545e..02543ae 100644
--- a/hassio-google-drive-backup/backup/model/model.py
+++ b/hassio-google-drive-backup/backup/model/model.py
@@ -131,10 +131,7 @@ class Model():
             # return the next snapshot after the delta
             next = self.time.toUtc(
                 time_that_day_local + timedelta(days=self.config.get(Setting.DAYS_BETWEEN_SNAPSHOTS)))
-        if next < now:
-            return now
-        else:
-            return next
+        return next
 
     def nextSnapshot(self, now: datetime):
         latest = max(self.snapshots.values(),
diff --git a/hassio-google-drive-backup/tests/test_coordinator.py b/hassio-google-drive-backup/tests/test_coordinator.py
index 0817867..c2633cb 100644
--- a/hassio-google-drive-backup/tests/test_coordinator.py
+++ b/hassio-google-drive-backup/tests/test_coordinator.py
@@ -416,3 +416,48 @@ async def test_only_source_configured(coord: Coordinator, dest: HelperTestSource
     dest.setNeedsConfiguration(False)
     await coord.sync()
     assert len(coord.snapshots()) == 1
+
+
+@pytest.mark.asyncio
+async def test_schedule_snapshot_next_sync_attempt(coord: Coordinator, model, source: HelperTestSource, dest: HelperTestSource, snapshot, time: FakeTime, simple_config: Config):
+    """
+    Next snapshot is before max sync interval is reached
+    """
+    simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1)
+    simple_config.override(Setting.MAX_SYNC_INTERVAL_SECONDS, 60 * 60)
+
+    time.setTimeZone("Europe/Stockholm")
+    simple_config.override(Setting.SNAPSHOT_TIME_OF_DAY, "03:23")
+    simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1)
+
+    source.setMax(10)
+    source.insert("Fri", time.toUtc(time.local(2020, 3, 16, 3, 33)))
+
+    time.setNow(time.local(2020, 3, 17, 2, 29))
+    model.reinitialize()
+    coord.reset()
+    await coord.sync()
+    assert coord.nextSnapshotTime() == time.local(2020, 3, 17, 3, 23)
+    assert coord.nextSnapshotTime() == coord.nextSyncAttempt()
+
+
+@pytest.mark.asyncio
+async def test_max_sync_interval_next_sync_attempt(coord: Coordinator, model, source: HelperTestSource, dest: HelperTestSource, snapshot, time: FakeTime, simple_config: Config):
+    """
+    Next snapshot is after max sync interval is reached
+    """
+    simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1)
+    simple_config.override(Setting.MAX_SYNC_INTERVAL_SECONDS, 60 * 60)
+
+    time.setTimeZone("Europe/Stockholm")
+    simple_config.override(Setting.SNAPSHOT_TIME_OF_DAY, "03:23")
+    simple_config.override(Setting.DAYS_BETWEEN_SNAPSHOTS, 1)
+
+    source.setMax(10)
+    source.insert("Fri", time.toUtc(time.local(2020, 3, 16, 3, 33)))
+    time.setNow(time.local(2020, 3, 17, 1, 29))
+    model.reinitialize()
+    coord.reset()
+    await coord.sync()
+    assert coord.nextSyncAttempt() == time.local(2020, 3, 17, 2, 29)
+    assert coord.nextSnapshotTime() > coord.nextSyncAttempt()
diff --git a/hassio-google-drive-backup/tests/test_model.py b/hassio-google-drive-backup/tests/test_model.py
index 4b691b2..0669623 100644
--- a/hassio-google-drive-backup/tests/test_model.py
+++ b/hassio-google-drive-backup/tests/test_model.py
@@ -133,7 +133,8 @@ def test_next_time_of_day(estimator):
     assert model._nextSnapshot(
         now=now, last_snapshot=None) == now - timedelta(minutes=1)
     assert model._nextSnapshot(
-        now=now, last_snapshot=now - timedelta(days=1)) == now
+        now=now, last_snapshot=now - timedelta(days=1)) == datetime(
+        1985, 12, 5, 8, 0, tzinfo=test_tz)
     assert model._nextSnapshot(now=now, last_snapshot=now) == datetime(
         1985, 12, 6, 8, 0, tzinfo=test_tz)
     assert model._nextSnapshot(now=now, last_snapshot=datetime(
@@ -155,7 +156,7 @@ def test_next_time_of_day_drift(estimator):
     assert model._nextSnapshot(
         now=now, last_snapshot=None) == now - timedelta(minutes=1)
     assert model._nextSnapshot(
-        now=now, last_snapshot=now - timedelta(days=1) + timedelta(minutes=1)) == now
+        now=now, last_snapshot=now - timedelta(days=1) + timedelta(minutes=1)) == datetime(1985, 12, 5, 8, 0, tzinfo=test_tz)
 
 
 def test_next_time_of_day_dest_disabled(model, time, source, dest):