Skip to content

Commit

Permalink
Improve privacy mode checking
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG committed Jan 18, 2025
1 parent 120813c commit 0c3b3d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 14 additions & 10 deletions reolink_aio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,18 +1206,19 @@ def refresh_base_url(self) -> None:
self._url = f"http://{self._host}:{self._port}/cgi-bin/api.cgi"

async def _check_privacy_mode(self) -> None:
if self.baichuan.privacy_mode(0) is None:
if self.baichuan.privacy_mode() is None:
return

_LOGGER.debug("Checking privacy mode before login into host %s", self._host)

try:
await self.baichuan.get_privacy_mode(0)
except ReolinkError as exc:
_LOGGER.debug("Failed to retrieve privacy mode during login of host %s: %s", self._host, exc)
return
now = time_now()
if now - self.baichuan.last_privacy_check > 2:
_LOGGER.debug("Checking privacy mode before login into host %s", self._host)
try:
await self.baichuan.get_privacy_mode()
except ReolinkError as exc:
_LOGGER.debug("Failed to retrieve privacy mode during login of host %s: %s", self._host, exc)
return

if self.baichuan.privacy_mode(0):
if self.baichuan.privacy_mode():
raise LoginPrivacyModeError(f"Could not login because privacy mode is turned on for host {self._host}, Baichuan login successfull, HTTP(s) login failed")

async def login(self) -> None:
Expand All @@ -1229,7 +1230,7 @@ async def login(self) -> None:
return # succes

# check privacy mode
if self.baichuan.privacy_mode(0):
if self.baichuan.privacy_mode():
await self._check_privacy_mode()

await self._login_mutex.acquire()
Expand Down Expand Up @@ -6021,6 +6022,9 @@ async def get_digest(self) -> dict:

async def subscription_send(self, headers, data, timeout: aiohttp.ClientTimeout | None = None, mutex: asyncio.Lock | None = None) -> str:
"""Send subscription data to the camera."""
if self.baichuan.privacy_mode():
await self._check_privacy_mode()

if self._subscribe_url is None:
await self.get_state("GetNetPort")

Expand Down
2 changes: 2 additions & 0 deletions reolink_aio/baichuan/baichuan.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
self._password_hash: str | None = None
self._aes_key: bytes | None = None
self._log_once: list[str] = []
self.last_privacy_check: float = 0

# TCP connection
self._mutex = asyncio.Lock()
Expand Down Expand Up @@ -738,6 +739,7 @@ async def get_privacy_mode(self, channel: int = 0) -> bool:
mess = await self.send(cmd_id=574, channel=channel)
value = self._get_value_from_xml(mess, "sleep") == "1"
self._privacy_mode[channel] = value
self.last_privacy_check = time_now()
return value

async def set_privacy_mode(self, channel: int, enable: bool) -> None:
Expand Down

0 comments on commit 0c3b3d5

Please sign in to comment.