Skip to content

Commit

Permalink
Fix Excessive Writes (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyao2015 authored Jan 19, 2022
1 parent d685266 commit 7efaa3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions emulated_hue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def async_start(self) -> None:
"""Start running the Hue emulation."""
self._loop = asyncio.get_running_loop()
self._hass = HomeAssistantClient(url=self._hass_url, token=self._hass_token)
await self.config.async_start(self._loop)
await self._hass.connect()
await self._api.async_setup()
self.loop.create_task(async_setup_discovery(self.config))
Expand All @@ -63,5 +64,6 @@ async def async_start(self) -> None:
async def async_stop(self) -> None:
"""Stop running the Hue emulation."""
LOGGER.info("Application shutdown")
await self.config.async_stop()
await self._hass.disconnect()
await self._api.async_stop()
27 changes: 26 additions & 1 deletion emulated_hue/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Hold configuration variables for the emulated hue bridge."""
import asyncio
import datetime
import hashlib
import logging
Expand All @@ -7,6 +8,7 @@

from getmac import get_mac_address

from .const import CONFIG_WRITE_INTERVAL_SECONDS
from .utils import async_save_json, create_secure_string, get_local_ip, load_json

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

# Flag to initiate shutdown of background saving
self._interrupted = False
self._need_save = False
self._saver_task = None # type: asyncio.Task | None

async def _background_saver(self) -> None:
last_save = 0
while not self._interrupted:
now = datetime.datetime.now().timestamp()
if self._need_save and now - last_save > CONFIG_WRITE_INTERVAL_SECONDS:
await async_save_json(self.get_path(CONFIG_FILE), self._config)
last_save = now
await asyncio.sleep(1)

async def async_start(self, loop: asyncio.AbstractEventLoop) -> None:
"""Start background saving task."""
self._saver_task = loop.create_task(self._background_saver())

async def async_stop(self) -> None:
"""Save the config."""
self._interrupted = True
await self._saver_task

@property
def ip_addr(self) -> str:
"""Return ip address of the emulated bridge."""
Expand Down Expand Up @@ -252,7 +277,7 @@ async def async_set_storage_value(
needs_save = True
# save config to file if changed
if needs_save:
await async_save_json(self.get_path(CONFIG_FILE), self._config)
self._need_save = True

async def async_delete_storage_value(self, key: str, subkey: str = None) -> None:
"""Delete a value in persistent storage."""
Expand Down
2 changes: 2 additions & 0 deletions emulated_hue/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

DEFAULT_THROTTLE_MS = 0

CONFIG_WRITE_INTERVAL_SECONDS = 60

HASS_ATTR_BRIGHTNESS = "brightness"
HASS_ATTR_COLOR_TEMP = "color_temp"
HASS_ATTR_XY_COLOR = "xy_color"
Expand Down

0 comments on commit 7efaa3e

Please sign in to comment.