Skip to content

Commit c31b02a

Browse files
Don't refresh library every new device, limit to an hour (#3081)
1 parent dc0042b commit c31b02a

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

custom_components/battery_notes/config_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ async def async_step_device(
168168
library_updater: LibraryUpdater = self.hass.data[DOMAIN][
169169
DATA_LIBRARY_UPDATER
170170
]
171-
await library_updater.get_library_updates(dt_util.utcnow())
171+
if await library_updater.time_to_update_library(1):
172+
await library_updater.get_library_updates(dt_util.utcnow())
172173

173174
device_registry = dr.async_get(self.hass)
174175
device_entry = device_registry.async_get(device_id)
@@ -261,7 +262,8 @@ async def async_step_entity(
261262
library_updater: LibraryUpdater = self.hass.data[DOMAIN][
262263
DATA_LIBRARY_UPDATER
263264
]
264-
await library_updater.get_library_updates(dt_util.utcnow())
265+
if await library_updater.time_to_update_library(1):
266+
await library_updater.get_library_updates(dt_util.utcnow())
265267

266268
device_registry = dr.async_get(self.hass)
267269
device_entry = device_registry.async_get(entity_entry.device_id)

custom_components/battery_notes/library_updater.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self, hass: HomeAssistant):
6262
@callback
6363
async def timer_update(self, now: datetime):
6464
"""Need to update the library."""
65-
if await self.time_to_update_library() is False:
65+
if await self.time_to_update_library(23) is False:
6666
return
6767

6868
await self.get_library_updates(now)
@@ -114,7 +114,7 @@ def _update_library_json(library_file: str, content: str) -> None:
114114
"Unable to update library, will retry later."
115115
)
116116

117-
async def time_to_update_library(self) -> bool:
117+
async def time_to_update_library(self, hours: int) -> bool:
118118
"""Check when last updated and if OK to do a new library update."""
119119
try:
120120
if DATA_LIBRARY_LAST_UPDATE in self.hass.data[DOMAIN]:
@@ -124,7 +124,7 @@ async def time_to_update_library(self) -> bool:
124124

125125
time_difference_in_hours = time_since_last_update / timedelta(hours=1)
126126

127-
if time_difference_in_hours < 23:
127+
if time_difference_in_hours < hours:
128128
_LOGGER.debug("Skipping library update, too recent")
129129
return False
130130

0 commit comments

Comments
 (0)