mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-16 04:14:31 +02:00
added exception test to update and run script in api
added test for active connection and reconnect to update and run script in api
This commit is contained in:
parent
6b9e1177d4
commit
05d21d1f42
1 changed files with 59 additions and 23 deletions
|
@ -123,6 +123,11 @@ class MikrotikAPI:
|
|||
# ---------------------------
|
||||
def update(self, path, param, value, mod_param, mod_value):
|
||||
"""Modify a parameter"""
|
||||
if not self._connected or not self._connection:
|
||||
if not self.connect():
|
||||
return None
|
||||
|
||||
try:
|
||||
response = self.path(path)
|
||||
if response is None:
|
||||
return False
|
||||
|
@ -140,6 +145,19 @@ class MikrotikAPI:
|
|||
}
|
||||
|
||||
response.update(**params)
|
||||
except librouteros.exceptions.ConnectionClosed:
|
||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||
self._connected = False
|
||||
self._connection = None
|
||||
return None
|
||||
except (
|
||||
librouteros.exceptions.TrapError,
|
||||
librouteros.exceptions.MultiTrapError,
|
||||
librouteros.exceptions.ProtocolError,
|
||||
librouteros.exceptions.FatalError
|
||||
) as api_error:
|
||||
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
||||
return None
|
||||
|
||||
return True
|
||||
|
||||
|
@ -148,6 +166,11 @@ class MikrotikAPI:
|
|||
# ---------------------------
|
||||
def run_script(self, name):
|
||||
"""Run script"""
|
||||
if not self._connected or not self._connection:
|
||||
if not self.connect():
|
||||
return None
|
||||
|
||||
try:
|
||||
response = self.path('/system/script')
|
||||
if response is None:
|
||||
return False
|
||||
|
@ -161,5 +184,18 @@ class MikrotikAPI:
|
|||
|
||||
run = response('run', **{'.id': tmp['.id']})
|
||||
tuple(run)
|
||||
except librouteros.exceptions.ConnectionClosed:
|
||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||
self._connected = False
|
||||
self._connection = None
|
||||
return None
|
||||
except (
|
||||
librouteros.exceptions.TrapError,
|
||||
librouteros.exceptions.MultiTrapError,
|
||||
librouteros.exceptions.ProtocolError,
|
||||
librouteros.exceptions.FatalError
|
||||
) as api_error:
|
||||
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
||||
return None
|
||||
|
||||
return True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue