Skip to content

Commit

Permalink
Add force_clients possibility (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaskivskyi authored Nov 20, 2023
1 parent 43a2f5f commit 4fc7694
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 44 additions & 1 deletion asusrouter/asusrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import aiohttp

from asusrouter.connection import Connection
from asusrouter.const import DEFAULT_CACHE_TIME, DEFAULT_TIMEOUT
from asusrouter.const import (
DEFAULT_CACHE_TIME,
DEFAULT_CLIENTS_WAITTIME,
DEFAULT_TIMEOUT,
)
from asusrouter.error import (
AsusRouter404Error,
AsusRouterAccessError,
Expand Down Expand Up @@ -54,6 +58,7 @@
save_state,
set_state,
)
from asusrouter.modules.system import AsusSystem
from asusrouter.tools import legacy
from asusrouter.tools.converters import safe_list
from asusrouter.tools.readers import merge_dicts
Expand All @@ -77,6 +82,10 @@ class AsusRouter:
# ID from the last called service
_last_id: Optional[int] = None

# Force clients
_force_clients: bool = False
_force_clients_waittime: float = DEFAULT_CLIENTS_WAITTIME

def __init__(
self,
hostname: str,
Expand Down Expand Up @@ -449,6 +458,20 @@ def _drop_data(self, datatype: AsusData, endpoint: Endpoint) -> bool:

return False

async def _check_prerequisites(self, datatype: AsusData) -> None:
"""Check prerequisites before fetching data."""

# Allow forcing clients update if set by the user
if datatype == AsusData.CLIENTS:
if self._force_clients:
_LOGGER.debug("Forcing clients update")
await self.async_set_state(AsusSystem.UPDATE_CLIENTS)
_LOGGER.debug(
"Waiting for clients to be updated: %s s",
self._force_clients_waittime,
)
await asyncio.sleep(self._force_clients_waittime)

def _check_state(self, datatype: Optional[AsusData]) -> None:
"""Make sure the state object is available."""

Expand Down Expand Up @@ -499,6 +522,9 @@ async def async_get_data(self, datatype: AsusData, force: bool = False) -> Any:
# Mark the data as active
self._state[datatype].start()

# Check prerequisites
await self._check_prerequisites(datatype)

# Get the data finder
data_finder = self._where_to_get_data(datatype)

Expand Down Expand Up @@ -848,6 +874,23 @@ def connected(self) -> bool:
# <-- Properties
# ---------------------------

# ---------------------------
# Additional settings -->
# ---------------------------

def set_force_clients(
self, value: bool = True, waittime: Optional[float] = None
) -> None:
"""Set force clients."""

self._force_clients = value
if waittime:
self._force_clients_waittime = waittime

# ---------------------------
# <-- Additional settings
# ---------------------------

# ---------------------------
# General management -->
# ---------------------------
Expand Down
1 change: 1 addition & 0 deletions asusrouter/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContentType(str, Enum):

# Library defaults
DEFAULT_CACHE_TIME = 5.0
DEFAULT_CLIENTS_WAITTIME = 5.0
DEFAULT_SLEEP_TIME = 0.1
DEFAULT_TIMEOUT = 10.0
DEFAULT_TIMEOUT_CONNECTION = 180.0
Expand Down

0 comments on commit 4fc7694

Please sign in to comment.