From 03d1162e7d951050789450b3001aed5878920eb0 Mon Sep 17 00:00:00 2001 From: tomaae <23486452+tomaae@users.noreply.github.com> Date: Mon, 6 Apr 2020 22:50:01 +0200 Subject: [PATCH] remove duplicate NAT rules to prevent crash #20 --- .../mikrotik_router/mikrotik_controller.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/custom_components/mikrotik_router/mikrotik_controller.py b/custom_components/mikrotik_router/mikrotik_controller.py index 8e9e28b..8cb58e1 100644 --- a/custom_components/mikrotik_router/mikrotik_controller.py +++ b/custom_components/mikrotik_router/mikrotik_controller.py @@ -45,6 +45,7 @@ def __init__( """Initialize MikrotikController.""" self.name = name self.hass = hass + self.host = host self.config_entry = config_entry self.traffic_type = traffic_type @@ -64,6 +65,8 @@ def __init__( self.api = MikrotikAPI(host, username, password, port, use_ssl) + self.nat_removed = {} + async_track_time_interval( self.hass, self.force_update, self.option_scan_interval ) @@ -462,6 +465,24 @@ def get_nat(self): only=[{"key": "action", "value": "dst-nat"}], ) + nat_uniq = {} + nat_del = {} + for uid in self.data["nat"]: + tmp_name = self.data["nat"][uid]["name"] + if tmp_name not in nat_uniq: + nat_uniq[tmp_name] = uid + else: + nat_del[uid] = 1 + nat_del[nat_uniq[tmp_name]] = 1 + + for uid in nat_del: + if self.data["nat"][uid]["name"] not in self.nat_removed: + self.nat_removed[self.data["nat"][uid]["name"]] = 1 + _LOGGER.error("Mikrotik %s duplicate NAT rule %s, entity will be unavailable.", + self.host, self.data["nat"][uid]["name"]) + + del self.data["nat"][uid] + # --------------------------- # get_system_routerboard # ---------------------------