added exception ApiEntryNotFound

This commit is contained in:
tomaae 2019-12-07 21:20:36 +01:00
parent 8f8817a1f7
commit 6467673dce
2 changed files with 18 additions and 1 deletions

View file

@ -1,4 +1,9 @@
"""Exceptions for Mikrotik Router.""" """Exceptions for Mikrotik Router."""
class OldLibrouteros(Exception): class OldLibrouteros(Exception):
"""Old librouteros version.""" """Old librouteros version."""
class ApiEntryNotFound(Exception):
"""Old librouteros version."""

View file

@ -3,7 +3,7 @@
import ssl import ssl
import logging import logging
import librouteros import librouteros
from .exceptions import OldLibrouteros from .exceptions import OldLibrouteros, ApiEntryNotFound
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -138,6 +138,7 @@ class MikrotikAPI:
# --------------------------- # ---------------------------
def update(self, path, param, value, mod_param, mod_value): def update(self, path, param, value, mod_param, mod_value):
"""Modify a parameter""" """Modify a parameter"""
entry_found = False
if not self._connected or not self._connection: if not self._connected or not self._connection:
if not self.connect(): if not self.connect():
return None return None
@ -153,6 +154,7 @@ class MikrotikAPI:
if tmp[param] != value: if tmp[param] != value:
continue continue
entry_found = True
params = { params = {
'.id': tmp['.id'], '.id': tmp['.id'],
mod_param: mod_value mod_param: mod_value
@ -174,6 +176,10 @@ class MikrotikAPI:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error) _LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None return None
if not entry_found:
error = "Parameter \"{}\" with value \"{}\" not found".format(param, value)
raise ApiEntryNotFound(error)
return True return True
# --------------------------- # ---------------------------
@ -181,6 +187,7 @@ class MikrotikAPI:
# --------------------------- # ---------------------------
def run_script(self, name): def run_script(self, name):
"""Run script""" """Run script"""
entry_found = False
if not self._connected or not self._connection: if not self._connected or not self._connection:
if not self.connect(): if not self.connect():
return None return None
@ -196,6 +203,7 @@ class MikrotikAPI:
if tmp['name'] != name: if tmp['name'] != name:
continue continue
entry_found = True
try: try:
run = response('run', **{'.id': tmp['.id']}) run = response('run', **{'.id': tmp['.id']})
except librouteros.exceptions.ConnectionClosed: except librouteros.exceptions.ConnectionClosed:
@ -214,4 +222,8 @@ class MikrotikAPI:
tuple(run) tuple(run)
if not entry_found:
error = "Script \"{}\" not found".format(name)
raise ApiEntryNotFound(error)
return True return True