diff --git a/custom_components/mikrotik_router/mikrotik_controller.py b/custom_components/mikrotik_router/mikrotik_controller.py index 61f70cc..97a433f 100644 --- a/custom_components/mikrotik_router/mikrotik_controller.py +++ b/custom_components/mikrotik_router/mikrotik_controller.py @@ -734,11 +734,14 @@ class MikrotikControllerData: key=".id", vals=[ {"name": ".id"}, + {"name": "chain", "default": "unknown"}, + {"name": "action", "default": "unknown"}, {"name": "protocol", "default": "any"}, {"name": "dst-port", "default": "any"}, {"name": "in-interface", "default": "any"}, + {"name": "out-interface", "default": "any"}, {"name": "to-addresses"}, - {"name": "to-ports"}, + {"name": "to-ports", "default": "any"}, {"name": "comment"}, { "name": "enabled", @@ -748,13 +751,32 @@ class MikrotikControllerData: }, ], val_proc=[ + [ + {"name": "uniq-id"}, + {"action": "combine"}, + {"key": "chain"}, + {"text": ","}, + {"key": "action"}, + {"text": ","}, + {"key": "protocol"}, + {"text": ","}, + {"key": "in-interface"}, + {"text": ":"}, + {"key": "dst-port"}, + {"text": "-"}, + {"key": "out-interface"}, + {"text": ":"}, + {"key": "to-addresses"}, + {"text": ":"}, + {"key": "to-ports"}, + ], [ {"name": "name"}, {"action": "combine"}, {"key": "protocol"}, {"text": ":"}, {"key": "dst-port"}, - ] + ], ], only=[{"key": "action", "value": "dst-nat"}], ) diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index 3f915af..d3e9ac5 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -132,11 +132,11 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches): # Data point name ["interface", "nat", "mangle", "ppp_secret", "script", "queue"], # Data point unique id - ["name", "name", "name", "name", "name", "name"], + ["name", "uniq-id", "name", "name", "name", "name"], # Entry Name ["name", "name", "comment", "name", "name", "name"], # Entry Unique id - ["port-mac-address", "name", "name", "name", "name", "name"], + ["port-mac-address", "uniq-id", "name", "name", "name", "name"], # Attr [ DEVICE_ATTRIBUTES_IFACE, @@ -338,8 +338,16 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): @property def name(self) -> str: """Return the name of the NAT switch.""" + if self._data["comment"]: + return f"{self._inst} NAT {self._data['comment']}" + return f"{self._inst} NAT {self._data['name']}" + @property + def unique_id(self) -> str: + """Return a unique identifier for this mangle switch.""" + return f"{self._inst.lower()}-enable_nat-{self._data['uniq-id']}" + @property def icon(self): """Return the icon.""" @@ -376,8 +384,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): value = None for uid in self._ctrl.data["nat"]: if ( - self._ctrl.data["nat"][uid]["name"] - == f"{self._data['protocol']}:{self._data['dst-port']}" + self._ctrl.data["nat"][uid]["uniq-id"] + == f"{self._data['chain']},{self._data['action']},{self._data['protocol']},{self._data['in-interface']}:{self._data['dst-port']}-{self._data['out-interface']}:{self._data['to-addresses']}:{self._data['to-ports']}" ): value = self._ctrl.data["nat"][uid][".id"] @@ -393,8 +401,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch): value = None for uid in self._ctrl.data["nat"]: if ( - self._ctrl.data["nat"][uid]["name"] - == f"{self._data['protocol']}:{self._data['dst-port']}" + self._ctrl.data["nat"][uid]["uniq-id"] + == f"{self._data['chain']},{self._data['action']},{self._data['protocol']},{self._data['in-interface']}:{self._data['dst-port']}-{self._data['out-interface']}:{self._data['to-addresses']}:{self._data['to-ports']}" ): value = self._ctrl.data["nat"][uid][".id"] @@ -504,7 +512,6 @@ class MikrotikControllerPPPSecretSwitch(MikrotikControllerSwitch): @property def name(self) -> str: """Return the name of the PPP Secret switch.""" - return f"{self._inst} PPP Secret {self._data['name']}" @property