Skip to content

Commit 7efaa3e

Browse files
authored
Fix Excessive Writes (#258)
1 parent d685266 commit 7efaa3e

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

emulated_hue/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async def async_start(self) -> None:
5252
"""Start running the Hue emulation."""
5353
self._loop = asyncio.get_running_loop()
5454
self._hass = HomeAssistantClient(url=self._hass_url, token=self._hass_token)
55+
await self.config.async_start(self._loop)
5556
await self._hass.connect()
5657
await self._api.async_setup()
5758
self.loop.create_task(async_setup_discovery(self.config))
@@ -63,5 +64,6 @@ async def async_start(self) -> None:
6364
async def async_stop(self) -> None:
6465
"""Stop running the Hue emulation."""
6566
LOGGER.info("Application shutdown")
67+
await self.config.async_stop()
6668
await self._hass.disconnect()
6769
await self._api.async_stop()

emulated_hue/config.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Hold configuration variables for the emulated hue bridge."""
2+
import asyncio
23
import datetime
34
import hashlib
45
import logging
@@ -7,6 +8,7 @@
78

89
from getmac import get_mac_address
910

11+
from .const import CONFIG_WRITE_INTERVAL_SECONDS
1012
from .utils import async_save_json, create_secure_string, get_local_ip, load_json
1113

1214
if TYPE_CHECKING:
@@ -76,6 +78,29 @@ def __init__(
7678
self._bridge_serial = mac_str.lower()
7779
self._bridge_uid = f"2f402f80-da50-11e1-9b23-{mac_str}"
7880

81+
# Flag to initiate shutdown of background saving
82+
self._interrupted = False
83+
self._need_save = False
84+
self._saver_task = None # type: asyncio.Task | None
85+
86+
async def _background_saver(self) -> None:
87+
last_save = 0
88+
while not self._interrupted:
89+
now = datetime.datetime.now().timestamp()
90+
if self._need_save and now - last_save > CONFIG_WRITE_INTERVAL_SECONDS:
91+
await async_save_json(self.get_path(CONFIG_FILE), self._config)
92+
last_save = now
93+
await asyncio.sleep(1)
94+
95+
async def async_start(self, loop: asyncio.AbstractEventLoop) -> None:
96+
"""Start background saving task."""
97+
self._saver_task = loop.create_task(self._background_saver())
98+
99+
async def async_stop(self) -> None:
100+
"""Save the config."""
101+
self._interrupted = True
102+
await self._saver_task
103+
79104
@property
80105
def ip_addr(self) -> str:
81106
"""Return ip address of the emulated bridge."""
@@ -252,7 +277,7 @@ async def async_set_storage_value(
252277
needs_save = True
253278
# save config to file if changed
254279
if needs_save:
255-
await async_save_json(self.get_path(CONFIG_FILE), self._config)
280+
self._need_save = True
256281

257282
async def async_delete_storage_value(self, key: str, subkey: str = None) -> None:
258283
"""Delete a value in persistent storage."""

emulated_hue/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
DEFAULT_THROTTLE_MS = 0
44

5+
CONFIG_WRITE_INTERVAL_SECONDS = 60
6+
57
HASS_ATTR_BRIGHTNESS = "brightness"
68
HASS_ATTR_COLOR_TEMP = "color_temp"
79
HASS_ATTR_XY_COLOR = "xy_color"

0 commit comments

Comments
 (0)