mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-08-31 15:09:32 +02:00
moved disconnect error messages to api.disconnect
This commit is contained in:
parent
4c10d0ebc7
commit
12514b4af8
1 changed files with 21 additions and 104 deletions
|
@ -82,8 +82,16 @@ class MikrotikAPI:
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# disconnect
|
# disconnect
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def disconnect(self):
|
def disconnect(self, location="unknown", error="unknown"):
|
||||||
"""Disconnect from Mikrotik device."""
|
"""Disconnect from Mikrotik device."""
|
||||||
|
if not self.connection_error_reported:
|
||||||
|
if location == "unknown":
|
||||||
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||||
|
else:
|
||||||
|
_LOGGER.error("Mikrotik %s error while %s : %s", self._host, location, error)
|
||||||
|
|
||||||
|
self.connection_error_reported = True
|
||||||
|
|
||||||
self._connected = False
|
self._connected = False
|
||||||
self._connection = None
|
self._connection = None
|
||||||
self._connection_epoch = 0
|
self._connection_epoch = 0
|
||||||
|
@ -193,10 +201,6 @@ class MikrotikAPI:
|
||||||
_LOGGER.debug("API query: %s", path)
|
_LOGGER.debug("API query: %s", path)
|
||||||
response = self._connection.path(path)
|
response = self._connection.path(path)
|
||||||
except librouteros_custom.exceptions.ConnectionClosed:
|
except librouteros_custom.exceptions.ConnectionClosed:
|
||||||
if not self.connection_error_reported:
|
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
@ -210,19 +214,11 @@ class MikrotikAPI:
|
||||||
OSError,
|
OSError,
|
||||||
ValueError,
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("path", api_error)
|
||||||
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("path")
|
||||||
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -230,19 +226,11 @@ class MikrotikAPI:
|
||||||
try:
|
try:
|
||||||
response = list(response)
|
response = list(response)
|
||||||
except librouteros_custom.exceptions.ConnectionClosed as api_error:
|
except librouteros_custom.exceptions.ConnectionClosed as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("building list for path", api_error)
|
||||||
_LOGGER.error("Mikrotik %s error while building list for path %s", self._host, api_error)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("building list for path")
|
||||||
_LOGGER.error("Mikrotik %s error while building list for path %s", self._host, "unknown")
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -277,10 +265,6 @@ class MikrotikAPI:
|
||||||
try:
|
try:
|
||||||
response.update(**params)
|
response.update(**params)
|
||||||
except librouteros_custom.exceptions.ConnectionClosed:
|
except librouteros_custom.exceptions.ConnectionClosed:
|
||||||
if not self.connection_error_reported:
|
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
@ -294,25 +278,11 @@ class MikrotikAPI:
|
||||||
OSError,
|
OSError,
|
||||||
ValueError,
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("update", api_error)
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while update %s", self._host,
|
|
||||||
api_error
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("update")
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while update %s", self._host,
|
|
||||||
"unknown"
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -349,10 +319,6 @@ class MikrotikAPI:
|
||||||
run = response("run", **{".id": tmp[".id"]})
|
run = response("run", **{".id": tmp[".id"]})
|
||||||
tuple(run)
|
tuple(run)
|
||||||
except librouteros_custom.exceptions.ConnectionClosed:
|
except librouteros_custom.exceptions.ConnectionClosed:
|
||||||
if not self.connection_error_reported:
|
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
@ -366,25 +332,11 @@ class MikrotikAPI:
|
||||||
OSError,
|
OSError,
|
||||||
ValueError,
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("run_script", api_error)
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while run_script %s", self._host,
|
|
||||||
api_error
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("run_script")
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while run_script %s", self._host,
|
|
||||||
"unknown"
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -412,14 +364,7 @@ class MikrotikAPI:
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
try:
|
try:
|
||||||
traffic = response("monitor-traffic", **args)
|
traffic = response("monitor-traffic", **args)
|
||||||
_LOGGER.debug(
|
|
||||||
"API response (%s): %s", "/interface/monitor-traffic", traffic
|
|
||||||
)
|
|
||||||
except librouteros_custom.exceptions.ConnectionClosed:
|
except librouteros_custom.exceptions.ConnectionClosed:
|
||||||
if not self.connection_error_reported:
|
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
@ -433,50 +378,22 @@ class MikrotikAPI:
|
||||||
OSError,
|
OSError,
|
||||||
ValueError,
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("get_traffic", api_error)
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while get_traffic %s", self._host,
|
|
||||||
api_error
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("get_traffic")
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while get_traffic %s", self._host,
|
|
||||||
"unknown"
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tuple(response)
|
tuple(response)
|
||||||
except librouteros_custom.exceptions.ConnectionClosed as api_error:
|
except librouteros_custom.exceptions.ConnectionClosed as api_error:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("get_traffic", api_error)
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while get_traffic %s", self._host,
|
|
||||||
api_error
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
except:
|
except:
|
||||||
if not self.connection_error_reported:
|
self.disconnect("get_traffic")
|
||||||
_LOGGER.error(
|
|
||||||
"Mikrotik %s error while get_traffic %s", self._host,
|
|
||||||
"unknown"
|
|
||||||
)
|
|
||||||
self.connection_error_reported = True
|
|
||||||
|
|
||||||
self.disconnect()
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue