diff --git a/custom_components/mikrotik_router/mikrotik_controller.py b/custom_components/mikrotik_router/mikrotik_controller.py index d0f818f..053e52c 100644 --- a/custom_components/mikrotik_router/mikrotik_controller.py +++ b/custom_components/mikrotik_router/mikrotik_controller.py @@ -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", diff --git a/custom_components/mikrotik_router/mikrotikapi.py b/custom_components/mikrotik_router/mikrotikapi.py index 712e9f0..1824fc8 100644 --- a/custom_components/mikrotik_router/mikrotikapi.py +++ b/custom_components/mikrotik_router/mikrotikapi.py @@ -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 # --------------------------- diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index d43d332..44acdbf 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -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()