Skip to content

Commit

Permalink
added support for virtual interfaces #12
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Apr 13, 2020
1 parent 99d6d7c commit cd4d0a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
22 changes: 17 additions & 5 deletions custom_components/mikrotik_router/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def parse_api(
data=None,
source=None,
key=None,
key_secondary=None,
key_search=None,
vals=None,
val_proc=None,
Expand All @@ -69,7 +70,7 @@ def parse_api(

uid = None
if key or key_search:
uid = get_uid(entry, key, key_search, keymap)
uid = get_uid(entry, key, key_secondary, key_search, keymap)
if not uid:
continue

Expand All @@ -92,16 +93,27 @@ def parse_api(
# ---------------------------
# get_uid
# ---------------------------
def get_uid(entry, key, key_search, keymap) -> Optional(str):
def get_uid(entry, key, key_secondary, key_search, keymap) -> Optional(str):
"""Get UID for data list"""
uid = None
if not key_search:
key_primary_found = True
if key not in entry:
return None
key_primary_found = False

if not entry[key]:
if key_primary_found and key not in entry and not entry[key]:
return None

uid = entry[key]
if key_primary_found:
uid = entry[key]
elif key_secondary:
if key_secondary not in entry:
return None

if not entry[key_secondary]:
return None

uid = entry[key_secondary]
else:
if keymap and key_search in entry and entry[key_search] in keymap:
uid = keymap[entry[key_search]]
Expand Down
10 changes: 10 additions & 0 deletions custom_components/mikrotik_router/mikrotik_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def get_interface(self):
data=self.data["interface"],
source=self.api.path("/interface"),
key="default-name",
key_secondary="name",
vals=[
{"name": "default-name"},
{"name": "name", "default_val": "default-name"},
Expand All @@ -441,8 +442,17 @@ def get_interface(self):
{"name": "rx-bits-per-second", "default": 0},
{"name": "tx-bits-per-second", "default": 0},
],
skip=[{"name": "type", "value": "bridge"}],
)

# Udpate virtual interfaces
for uid, vals in self.data["interface"].items():
if vals["default-name"] == "":
self.data["interface"][uid]["default-name"] = vals["name"]
self.data["interface"][uid][
"port-mac-address"
] = f"{vals['port-mac-address']}-{vals['name']}"

# ---------------------------
# get_interface_traffic
# ---------------------------
Expand Down
4 changes: 4 additions & 0 deletions custom_components/mikrotik_router/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ async def async_turn_on(self):
"""Turn on the switch."""
path = "/interface"
param = "default-name"
if "-" in self._data["port-mac-address"]:
param = "name"
value = self._data[param]
mod_param = "disabled"
mod_value = False
Expand All @@ -236,6 +238,8 @@ async def async_turn_off(self):
"""Turn on the switch."""
path = "/interface"
param = "default-name"
if "-" in self._data["port-mac-address"]:
param = "name"
value = self._data[param]
mod_param = "disabled"
mod_value = True
Expand Down

0 comments on commit cd4d0a0

Please sign in to comment.