diff --git a/custom_components/mikrotik_router/device_tracker.py b/custom_components/mikrotik_router/device_tracker.py index f60bc72..8bb0bc7 100644 --- a/custom_components/mikrotik_router/device_tracker.py +++ b/custom_components/mikrotik_router/device_tracker.py @@ -34,6 +34,10 @@ DEVICE_ATTRIBUTES = [ "default-name", ] + +# --------------------------- +# format_attribute +# --------------------------- def format_attribute(attr): res = attr.replace("-", " ") res = res.capitalize() diff --git a/custom_components/mikrotik_router/mikrotik_controller.py b/custom_components/mikrotik_router/mikrotik_controller.py index 75cdea7..190de86 100644 --- a/custom_components/mikrotik_router/mikrotik_controller.py +++ b/custom_components/mikrotik_router/mikrotik_controller.py @@ -347,7 +347,8 @@ class MikrotikControllerData(): if uid not in self.data['nat']: self.data['nat'][uid] = {} - self.data['nat'][uid]['name'] = entry['protocol'] + ':' + str(entry['dst-port']) + self.data['nat'][uid]['name'] = "{}:{}".format(entry['protocol'], entry['dst-port']) + self.data['nat'][uid]['.id'] = from_entry(entry, '.id') self.data['nat'][uid]['protocol'] = from_entry(entry, 'protocol') self.data['nat'][uid]['dst-port'] = from_entry(entry, 'dst-port') self.data['nat'][uid]['in-interface'] = from_entry(entry, 'in-interface', 'any') diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index 6bb10ac..2991781 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -55,6 +55,10 @@ DEVICE_ATTRIBUTES_SCRIPT = [ "run-count", ] + +# --------------------------- +# format_attribute +# --------------------------- def format_attribute(attr): res = attr.replace("-", " ") res = res.capitalize() @@ -306,7 +310,11 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): """Turn on the switch.""" path = '/ip/firewall/nat' param = '.id' - value = self._uid + value = None + for uid in self._ctrl.data['nat']: + if self._ctrl.data['nat'][uid]['name'] == "{}:{}".format(self._data['protocol'], self._data['dst-port']): + value = self._ctrl.data['nat'][uid]['.id'] + mod_param = 'disabled' mod_value = False self._ctrl.set_value(path, param, value, mod_param, mod_value) @@ -317,7 +325,11 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): """Turn on the switch.""" path = '/ip/firewall/nat' param = '.id' - value = self._uid + value = None + for uid in self._ctrl.data['nat']: + if self._ctrl.data['nat'][uid]['name'] == "{}:{}".format(self._data['protocol'], self._data['dst-port']): + value = self._ctrl.data['nat'][uid]['.id'] + mod_param = 'disabled' mod_value = True self._ctrl.set_value(path, param, value, mod_param, mod_value)