mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-13 10:54:33 +02:00
reformatted code using black
This commit is contained in:
parent
3cf637d1ba
commit
bd82612cb8
2 changed files with 90 additions and 65 deletions
|
@ -272,7 +272,6 @@ class MikrotikControllerData:
|
||||||
traffic_type = "bps"
|
traffic_type = "bps"
|
||||||
traffic_div = 1
|
traffic_div = 1
|
||||||
|
|
||||||
|
|
||||||
for uid in self.data["interface"]:
|
for uid in self.data["interface"]:
|
||||||
self.data["interface"][uid]["rx-bits-per-second-attr"] = traffic_type
|
self.data["interface"][uid]["rx-bits-per-second-attr"] = traffic_type
|
||||||
self.data["interface"][uid]["tx-bits-per-second-attr"] = traffic_type
|
self.data["interface"][uid]["tx-bits-per-second-attr"] = traffic_type
|
||||||
|
|
|
@ -21,7 +21,16 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
class MikrotikAPI:
|
class MikrotikAPI:
|
||||||
"""Handle all communication with the Mikrotik API."""
|
"""Handle all communication with the Mikrotik API."""
|
||||||
|
|
||||||
def __init__(self, host, username, password, port=0, use_ssl=True, login_method=DEFAULT_LOGIN_METHOD, encoding=DEFAULT_ENCODING):
|
def __init__(
|
||||||
|
self,
|
||||||
|
host,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
port=0,
|
||||||
|
use_ssl=True,
|
||||||
|
login_method=DEFAULT_LOGIN_METHOD,
|
||||||
|
encoding=DEFAULT_ENCODING,
|
||||||
|
):
|
||||||
"""Initialize the Mikrotik Client."""
|
"""Initialize the Mikrotik Client."""
|
||||||
self._host = host
|
self._host = host
|
||||||
self._use_ssl = use_ssl
|
self._use_ssl = use_ssl
|
||||||
|
@ -76,7 +85,9 @@ class MikrotikAPI:
|
||||||
kwargs["ssl_wrapper"] = self._ssl_wrapper
|
kwargs["ssl_wrapper"] = self._ssl_wrapper
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
try:
|
try:
|
||||||
self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs)
|
self._connection = librouteros.connect(
|
||||||
|
self._host, self._username, self._password, **kwargs
|
||||||
|
)
|
||||||
except (
|
except (
|
||||||
librouteros.exceptions.TrapError,
|
librouteros.exceptions.TrapError,
|
||||||
librouteros.exceptions.MultiTrapError,
|
librouteros.exceptions.MultiTrapError,
|
||||||
|
@ -85,15 +96,19 @@ class MikrotikAPI:
|
||||||
librouteros.exceptions.FatalError,
|
librouteros.exceptions.FatalError,
|
||||||
ssl.SSLError,
|
ssl.SSLError,
|
||||||
BrokenPipeError,
|
BrokenPipeError,
|
||||||
OSError
|
OSError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, api_error)
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while connecting: %s", self._host, api_error
|
||||||
|
)
|
||||||
self.error_to_strings("%s" % api_error)
|
self.error_to_strings("%s" % api_error)
|
||||||
self._connection = None
|
self._connection = None
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, "Unknown")
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while connecting: %s", self._host, "Unknown"
|
||||||
|
)
|
||||||
self._connection = None
|
self._connection = None
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
@ -152,7 +167,7 @@ class MikrotikAPI:
|
||||||
ssl.SSLError,
|
ssl.SSLError,
|
||||||
BrokenPipeError,
|
BrokenPipeError,
|
||||||
OSError,
|
OSError,
|
||||||
ValueError
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
|
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
@ -205,10 +220,7 @@ class MikrotikAPI:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entry_found = True
|
entry_found = True
|
||||||
params = {
|
params = {".id": tmp[".id"], mod_param: mod_value}
|
||||||
'.id': tmp['.id'],
|
|
||||||
mod_param: mod_value
|
|
||||||
}
|
|
||||||
|
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
try:
|
try:
|
||||||
|
@ -226,21 +238,25 @@ class MikrotikAPI:
|
||||||
ssl.SSLError,
|
ssl.SSLError,
|
||||||
BrokenPipeError,
|
BrokenPipeError,
|
||||||
OSError,
|
OSError,
|
||||||
ValueError
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
_LOGGER.error("Mikrotik %s error while update %s", self._host, api_error)
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while update %s", self._host, api_error
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Mikrotik %s error while update %s", self._host, "unknown")
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while update %s", self._host, "unknown"
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
if not entry_found:
|
if not entry_found:
|
||||||
error = "Parameter \"{}\" with value \"{}\" not found".format(param, value)
|
error = 'Parameter "{}" with value "{}" not found'.format(param, value)
|
||||||
raise ApiEntryNotFound(error)
|
raise ApiEntryNotFound(error)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -258,21 +274,21 @@ class MikrotikAPI:
|
||||||
if not self.connect():
|
if not self.connect():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
response = self.path('/system/script')
|
response = self.path("/system/script")
|
||||||
if response is None:
|
if response is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for tmp in response:
|
for tmp in response:
|
||||||
if 'name' not in tmp:
|
if "name" not in tmp:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if tmp['name'] != name:
|
if tmp["name"] != name:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entry_found = True
|
entry_found = True
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
try:
|
try:
|
||||||
run = response('run', **{'.id': tmp['.id']})
|
run = response("run", **{".id": tmp[".id"]})
|
||||||
tuple(run)
|
tuple(run)
|
||||||
except librouteros.exceptions.ConnectionClosed:
|
except librouteros.exceptions.ConnectionClosed:
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||||
|
@ -287,21 +303,25 @@ class MikrotikAPI:
|
||||||
ssl.SSLError,
|
ssl.SSLError,
|
||||||
BrokenPipeError,
|
BrokenPipeError,
|
||||||
OSError,
|
OSError,
|
||||||
ValueError
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, api_error)
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while run_script %s", self._host, api_error
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, "unknown")
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while run_script %s", self._host, "unknown"
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
if not entry_found:
|
if not entry_found:
|
||||||
error = "Script \"{}\" not found".format(name)
|
error = 'Script "{}" not found'.format(name)
|
||||||
raise ApiEntryNotFound(error)
|
raise ApiEntryNotFound(error)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -319,15 +339,17 @@ class MikrotikAPI:
|
||||||
if not self.connect():
|
if not self.connect():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
response = self.path('/interface')
|
response = self.path("/interface")
|
||||||
if response is None:
|
if response is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
args = {'interface': interfaces, 'once': True}
|
args = {"interface": interfaces, "once": True}
|
||||||
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)
|
_LOGGER.debug(
|
||||||
|
"API response (%s): %s", "/interface/monitor-traffic", traffic
|
||||||
|
)
|
||||||
except librouteros.exceptions.ConnectionClosed:
|
except librouteros.exceptions.ConnectionClosed:
|
||||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
@ -341,14 +363,18 @@ class MikrotikAPI:
|
||||||
ssl.SSLError,
|
ssl.SSLError,
|
||||||
BrokenPipeError,
|
BrokenPipeError,
|
||||||
OSError,
|
OSError,
|
||||||
ValueError
|
ValueError,
|
||||||
) as api_error:
|
) as api_error:
|
||||||
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, api_error)
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while get_traffic %s", self._host, api_error
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, "unknown")
|
_LOGGER.error(
|
||||||
|
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
|
||||||
|
)
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue