mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-15 03:44:31 +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:
|
if not self._port:
|
||||||
self._port = 8729 if self._use_ssl else 8728
|
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
|
# disconnect
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -170,12 +184,9 @@ class MikrotikAPI:
|
||||||
def path(self, path, return_list=True) -> Optional(list):
|
def path(self, path, return_list=True) -> Optional(list):
|
||||||
"""Retrieve data from Mikrotik API."""
|
"""Retrieve data from Mikrotik API."""
|
||||||
"""Returns generator object, unless return_list passed as True"""
|
"""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():
|
if not self.connection_check():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
try:
|
try:
|
||||||
|
@ -244,12 +255,9 @@ class MikrotikAPI:
|
||||||
def update(self, path, param, value, mod_param, mod_value) -> bool:
|
def update(self, path, param, value, mod_param, mod_value) -> bool:
|
||||||
"""Modify a parameter"""
|
"""Modify a parameter"""
|
||||||
entry_found = False
|
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():
|
if not self.connection_check():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
response = self.path(path, return_list=False)
|
response = self.path(path, return_list=False)
|
||||||
if response is None:
|
if response is None:
|
||||||
|
@ -321,12 +329,8 @@ class MikrotikAPI:
|
||||||
def run_script(self, name) -> bool:
|
def run_script(self, name) -> bool:
|
||||||
"""Run script"""
|
"""Run script"""
|
||||||
entry_found = False
|
entry_found = False
|
||||||
if not self._connected or not self._connection:
|
if not self.connection_check():
|
||||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
return False
|
||||||
return False
|
|
||||||
|
|
||||||
if not self.connect():
|
|
||||||
return False
|
|
||||||
|
|
||||||
response = self.path("/system/script", return_list=False)
|
response = self.path("/system/script", return_list=False)
|
||||||
if response is None:
|
if response is None:
|
||||||
|
@ -397,12 +401,8 @@ class MikrotikAPI:
|
||||||
def get_traffic(self, interfaces) -> Optional(list):
|
def get_traffic(self, interfaces) -> Optional(list):
|
||||||
"""Get traffic stats"""
|
"""Get traffic stats"""
|
||||||
traffic = None
|
traffic = None
|
||||||
if not self._connected or not self._connection:
|
if not self.connection_check():
|
||||||
if self._connection_epoch > time.time() - self._connection_retry_sec:
|
return None
|
||||||
return None
|
|
||||||
|
|
||||||
if not self.connect():
|
|
||||||
return None
|
|
||||||
|
|
||||||
response = self.path("/interface", return_list=False)
|
response = self.path("/interface", return_list=False)
|
||||||
if response is None:
|
if response is None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue