mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-09 00:44:32 +02:00
Modified kid-control switches to block/unblock traffic #70
This commit is contained in:
parent
1bdd8e81f6
commit
7dc9120b19
3 changed files with 80 additions and 6 deletions
|
@ -304,6 +304,13 @@ class MikrotikControllerData:
|
|||
"""Change value using Mikrotik API"""
|
||||
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
|
||||
# ---------------------------
|
||||
|
@ -924,6 +931,7 @@ class MikrotikControllerData:
|
|||
{"name": "sat", "default": "None"},
|
||||
{"name": "sun", "default": "None"},
|
||||
{"name": "comment"},
|
||||
{"name": "paused", "type": "bool", "reverse": True},
|
||||
{
|
||||
"name": "enabled",
|
||||
"source": "disabled",
|
||||
|
|
|
@ -312,6 +312,69 @@ class MikrotikAPI:
|
|||
|
||||
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
|
||||
# ---------------------------
|
||||
|
|
|
@ -722,6 +722,11 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
|
|||
"""Initialize."""
|
||||
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
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
|
@ -756,9 +761,8 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
|
|||
path = "/ip/kid-control"
|
||||
param = "name"
|
||||
value = self._data["name"]
|
||||
mod_param = "disabled"
|
||||
mod_value = False
|
||||
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||
command = "resume"
|
||||
self._ctrl.execute(path, command, param, value)
|
||||
await self._ctrl.force_update()
|
||||
|
||||
async def async_turn_off(self) -> None:
|
||||
|
@ -766,7 +770,6 @@ class MikrotikControllerKidcontrolSwitch(MikrotikControllerSwitch):
|
|||
path = "/ip/kid-control"
|
||||
param = "name"
|
||||
value = self._data["name"]
|
||||
mod_param = "disabled"
|
||||
mod_value = True
|
||||
self._ctrl.set_value(path, param, value, mod_param, mod_value)
|
||||
command = "pause"
|
||||
self._ctrl.execute(path, command, param, value)
|
||||
await self._ctrl.async_update()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue