Modified kid-control switches to block/unblock traffic #70

This commit is contained in:
tomaae 2020-12-28 01:26:46 +01:00
parent 1bdd8e81f6
commit 7dc9120b19
3 changed files with 80 additions and 6 deletions

View file

@ -304,6 +304,13 @@ class MikrotikControllerData:
"""Change value using Mikrotik API""" """Change value using Mikrotik API"""
return self.api.update(path, param, value, mod_param, mod_value) return self.api.update(path, param, value, mod_param, mod_value)
# ---------------------------
# execute
# ---------------------------
def execute(self, path, command, param, value):
"""Change value using Mikrotik API"""
return self.api.execute(path, command, param, value)
# --------------------------- # ---------------------------
# run_script # run_script
# --------------------------- # ---------------------------
@ -924,6 +931,7 @@ class MikrotikControllerData:
{"name": "sat", "default": "None"}, {"name": "sat", "default": "None"},
{"name": "sun", "default": "None"}, {"name": "sun", "default": "None"},
{"name": "comment"}, {"name": "comment"},
{"name": "paused", "type": "bool", "reverse": True},
{ {
"name": "enabled", "name": "enabled",
"source": "disabled", "source": "disabled",

View file

@ -312,6 +312,69 @@ class MikrotikAPI:
return True return True
# ---------------------------
# execute
# ---------------------------
def execute(self, path, command, param, value) -> bool:
"""Modify a parameter"""
entry_found = False
if not self.connection_check():
return False
response = self.path(path, return_list=False)
if response is None:
return False
for tmp in response:
if param not in tmp:
continue
if tmp[param] != value:
continue
entry_found = True
params = {".id": tmp[".id"]}
print(params)
self.lock.acquire()
try:
tuple(response(command, **params))
except librouteros.exceptions.ConnectionClosed:
self.disconnect()
self.lock.release()
return False
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError,
socket_timeout,
socket_error,
ssl.SSLError,
BrokenPipeError,
OSError,
ValueError,
) as api_error:
self.disconnect("update", api_error)
self.lock.release()
return False
except:
self.disconnect("update")
self.lock.release()
return False
self.lock.release()
if not entry_found:
_LOGGER.error(
"Mikrotik %s Execute %s parameter %s with value %s not found",
self._host,
command,
param,
value,
)
return True
# --------------------------- # ---------------------------
# run_script # run_script
# --------------------------- # ---------------------------

View file

@ -722,6 +722,11 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
"""Initialize.""" """Initialize."""
super().__init__(inst, uid, mikrotik_controller, sid_data) super().__init__(inst, uid, mikrotik_controller, sid_data)
@property
def is_on(self) -> bool:
"""Return true if device is on."""
return self._data["paused"]
@property @property
def icon(self) -> str: def icon(self) -> str:
"""Return the icon.""" """Return the icon."""
@ -756,9 +761,8 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
path = "/ip/kid-control" path = "/ip/kid-control"
param = "name" param = "name"
value = self._data["name"] value = self._data["name"]
mod_param = "disabled" command = "resume"
mod_value = False self._ctrl.execute(path, command, param, value)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.force_update() await self._ctrl.force_update()
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
@ -766,7 +770,6 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
path = "/ip/kid-control" path = "/ip/kid-control"
param = "name" param = "name"
value = self._data["name"] value = self._data["name"]
mod_param = "disabled" command = "pause"
mod_value = True self._ctrl.execute(path, command, param, value)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.async_update() await self._ctrl.async_update()