Skip to content

Commit

Permalink
api code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Mar 13, 2020
1 parent d68ffbd commit b20d140
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions custom_components/mikrotik_router/mikrotikapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ def connect(self) -> bool:
BrokenPipeError,
OSError
) as api_error:
self.lock.release()
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, api_error)
self.error_to_strings("%s" % api_error)
self._connection = None
self.lock.release()
return False
except:
self.lock.release()
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, "Unknown")
self._connection = None
self.lock.release()
return False
else:
self.lock.release()
_LOGGER.info("Mikrotik Connected to %s", self._host)
self._connected = True
self.lock.release()

return self._connected

Expand Down Expand Up @@ -148,10 +148,9 @@ def path(self, path) -> list:
response = self._connection.path(path)
_LOGGER.debug("API response (%s): %s", path, response)
except librouteros_custom.exceptions.ConnectionClosed:
self.lock.release()
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
self.disconnect()
self.lock.release()
return None
except (
librouteros_custom.exceptions.TrapError,
Expand All @@ -163,14 +162,14 @@ def path(self, path) -> list:
OSError,
ValueError
) as api_error:
self.lock.release()
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
self.disconnect()
self.lock.release()
return None
except:
self.lock.release()
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
self.disconnect()
self.lock.release()
return None

self.lock.release()
Expand Down Expand Up @@ -210,10 +209,9 @@ def update(self, path, param, value, mod_param, mod_value) -> bool:
try:
response.update(**params)
except librouteros_custom.exceptions.ConnectionClosed:
self.lock.release()
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
self.disconnect()
self.lock.release()
return False
except (
librouteros_custom.exceptions.TrapError,
Expand All @@ -225,18 +223,17 @@ def update(self, path, param, value, mod_param, mod_value) -> bool:
OSError,
ValueError
) as api_error:
self.lock.release()
_LOGGER.error("Mikrotik %s error while update %s", self._host, api_error)
self.disconnect()
self.lock.release()
return False
except:
self.lock.release()
_LOGGER.error("Mikrotik %s error while update %s", self._host, "unknown")
self.disconnect()
return False
else:
self.lock.release()
return False

self.lock.release()
if not entry_found:
error = "Parameter \"{}\" with value \"{}\" not found".format(param, value)
raise ApiEntryNotFound(error)
Expand Down Expand Up @@ -273,10 +270,9 @@ def run_script(self, name) -> bool:
run = response('run', **{'.id': tmp['.id']})
tuple(run)
except librouteros_custom.exceptions.ConnectionClosed:
self.lock.release()
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
self.disconnect()
self.lock.release()
return False
except (
librouteros_custom.exceptions.TrapError,
Expand All @@ -288,18 +284,17 @@ def run_script(self, name) -> bool:
OSError,
ValueError
) as api_error:
self.lock.release()
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, api_error)
self.disconnect()
self.lock.release()
return False
except:
self.lock.release()
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, "unknown")
self.disconnect()
return False
else:
self.lock.release()
return False

self.lock.release()
if not entry_found:
error = "Script \"{}\" not found".format(name)
raise ApiEntryNotFound(error)
Expand Down Expand Up @@ -329,10 +324,9 @@ def get_traffic(self, interfaces) -> list:
traffic = response('monitor-traffic', **args)
_LOGGER.debug("API response (%s): %s", "/interface/monitor-traffic", traffic)
except librouteros_custom.exceptions.ConnectionClosed:
self.lock.release()
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
self.disconnect()
self.lock.release()
return None
except (
librouteros_custom.exceptions.TrapError,
Expand All @@ -344,16 +338,15 @@ def get_traffic(self, interfaces) -> list:
OSError,
ValueError
) as api_error:
self.lock.release()
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, api_error)
self.disconnect()
self.lock.release()
return None
except:
self.lock.release()
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, "unknown")
self.disconnect()
return None
else:
self.lock.release()
return None

self.lock.release()
return traffic if traffic else None

0 comments on commit b20d140

Please sign in to comment.