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,23 +123,41 @@ 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"""
|
||||||
response = self.path(path)
|
if not self._connected or not self._connection:
|
||||||
if response is None:
|
if not self.connect():
|
||||||
return False
|
return None
|
||||||
|
|
||||||
for tmp in response:
|
try:
|
||||||
if param not in tmp:
|
response = self.path(path)
|
||||||
continue
|
if response is None:
|
||||||
|
return False
|
||||||
|
|
||||||
if tmp[param] != value:
|
for tmp in response:
|
||||||
continue
|
if param not in tmp:
|
||||||
|
continue
|
||||||
|
|
||||||
params = {
|
if tmp[param] != value:
|
||||||
'.id': tmp['.id'],
|
continue
|
||||||
mod_param: mod_value
|
|
||||||
}
|
|
||||||
|
|
||||||
response.update(**params)
|
params = {
|
||||||
|
'.id': tmp['.id'],
|
||||||
|
mod_param: mod_value
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
return True
|
||||||
|
|
||||||
|
@ -148,18 +166,36 @@ class MikrotikAPI:
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def run_script(self, name):
|
def run_script(self, name):
|
||||||
"""Run script"""
|
"""Run script"""
|
||||||
response = self.path('/system/script')
|
if not self._connected or not self._connection:
|
||||||
if response is None:
|
if not self.connect():
|
||||||
return False
|
return None
|
||||||
|
|
||||||
for tmp in response:
|
try:
|
||||||
if 'name' not in tmp:
|
response = self.path('/system/script')
|
||||||
continue
|
if response is None:
|
||||||
|
return False
|
||||||
|
|
||||||
if tmp['name'] != name:
|
for tmp in response:
|
||||||
continue
|
if 'name' not in tmp:
|
||||||
|
continue
|
||||||
|
|
||||||
run = response('run', **{'.id': tmp['.id']})
|
if tmp['name'] != name:
|
||||||
tuple(run)
|
continue
|
||||||
|
|
||||||
|
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
|
return True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue