Skip to content

Commit

Permalink
remove duplicate NAT rules to prevent crash #20
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Apr 6, 2020
1 parent fc3ad56 commit 03d1162
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions custom_components/mikrotik_router/mikrotik_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
)
Expand Down Expand Up @@ -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
# ---------------------------
Expand Down

0 comments on commit 03d1162

Please sign in to comment.