mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-09 17:04:29 +02:00
moved connection check to separated function
This commit is contained in:
parent
592dbbe3dd
commit
4c10d0ebc7
1 changed files with 22 additions and 22 deletions
|
@ -65,6 +65,20 @@ class MikrotikAPI:
|
|||
if not self._port:
|
||||
self._port = 8729 if self._use_ssl else 8728
|
||||
|
||||
# ---------------------------
|
||||
# connection_check
|
||||
# ---------------------------
|
||||
def connection_check(self) -> bool:
|
||||
"""Check if mikrotik is connected"""
|
||||
if not self._connected or not self._connection:
|
||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
||||
return False
|
||||
|
||||
if not self.connect():
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# ---------------------------
|
||||
# disconnect
|
||||
# ---------------------------
|
||||
|
@ -170,12 +184,9 @@ class MikrotikAPI:
|
|||
def path(self, path, return_list=True) -> Optional(list):
|
||||
"""Retrieve data from Mikrotik API."""
|
||||
"""Returns generator object, unless return_list passed as True"""
|
||||
if not self._connected or not self._connection:
|
||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
||||
return None
|
||||
|
||||
if not self.connect():
|
||||
return None
|
||||
if not self.connection_check():
|
||||
return None
|
||||
|
||||
self.lock.acquire()
|
||||
try:
|
||||
|
@ -244,12 +255,9 @@ class MikrotikAPI:
|
|||
def update(self, path, param, value, mod_param, mod_value) -> bool:
|
||||
"""Modify a parameter"""
|
||||
entry_found = False
|
||||
if not self._connected or not self._connection:
|
||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
||||
return False
|
||||
|
||||
if not self.connect():
|
||||
return False
|
||||
if not self.connection_check():
|
||||
return False
|
||||
|
||||
response = self.path(path, return_list=False)
|
||||
if response is None:
|
||||
|
@ -321,12 +329,8 @@ class MikrotikAPI:
|
|||
def run_script(self, name) -> bool:
|
||||
"""Run script"""
|
||||
entry_found = False
|
||||
if not self._connected or not self._connection:
|
||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
||||
return False
|
||||
|
||||
if not self.connect():
|
||||
return False
|
||||
if not self.connection_check():
|
||||
return False
|
||||
|
||||
response = self.path("/system/script", return_list=False)
|
||||
if response is None:
|
||||
|
@ -397,12 +401,8 @@ class MikrotikAPI:
|
|||
def get_traffic(self, interfaces) -> Optional(list):
|
||||
"""Get traffic stats"""
|
||||
traffic = None
|
||||
if not self._connected or not self._connection:
|
||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
||||
return None
|
||||
|
||||
if not self.connect():
|
||||
return None
|
||||
if not self.connection_check():
|
||||
return None
|
||||
|
||||
response = self.path("/interface", return_list=False)
|
||||
if response is None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue