Use comment as name when available, make sensor more granular #20

This commit is contained in:
tomaae 2020-12-25 16:52:36 +01:00
parent 68e0b4c288
commit 49631e2826
2 changed files with 38 additions and 9 deletions

View file

@ -734,11 +734,14 @@ class MikrotikControllerData:
key=".id", key=".id",
vals=[ vals=[
{"name": ".id"}, {"name": ".id"},
{"name": "chain", "default": "unknown"},
{"name": "action", "default": "unknown"},
{"name": "protocol", "default": "any"}, {"name": "protocol", "default": "any"},
{"name": "dst-port", "default": "any"}, {"name": "dst-port", "default": "any"},
{"name": "in-interface", "default": "any"}, {"name": "in-interface", "default": "any"},
{"name": "out-interface", "default": "any"},
{"name": "to-addresses"}, {"name": "to-addresses"},
{"name": "to-ports"}, {"name": "to-ports", "default": "any"},
{"name": "comment"}, {"name": "comment"},
{ {
"name": "enabled", "name": "enabled",
@ -748,13 +751,32 @@ class MikrotikControllerData:
}, },
], ],
val_proc=[ 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"}, {"name": "name"},
{"action": "combine"}, {"action": "combine"},
{"key": "protocol"}, {"key": "protocol"},
{"text": ":"}, {"text": ":"},
{"key": "dst-port"}, {"key": "dst-port"},
] ],
], ],
only=[{"key": "action", "value": "dst-nat"}], only=[{"key": "action", "value": "dst-nat"}],
) )

View file

@ -132,11 +132,11 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches):
# Data point name # Data point name
["interface", "nat", "mangle", "ppp_secret", "script", "queue"], ["interface", "nat", "mangle", "ppp_secret", "script", "queue"],
# Data point unique id # Data point unique id
["name", "name", "name", "name", "name", "name"], ["name", "uniq-id", "name", "name", "name", "name"],
# Entry Name # Entry Name
["name", "name", "comment", "name", "name", "name"], ["name", "name", "comment", "name", "name", "name"],
# Entry Unique id # Entry Unique id
["port-mac-address", "name", "name", "name", "name", "name"], ["port-mac-address", "uniq-id", "name", "name", "name", "name"],
# Attr # Attr
[ [
DEVICE_ATTRIBUTES_IFACE, DEVICE_ATTRIBUTES_IFACE,
@ -338,8 +338,16 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the NAT switch.""" """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']}" 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 @property
def icon(self): def icon(self):
"""Return the icon.""" """Return the icon."""
@ -376,8 +384,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
value = None value = None
for uid in self._ctrl.data["nat"]: for uid in self._ctrl.data["nat"]:
if ( if (
self._ctrl.data["nat"][uid]["name"] self._ctrl.data["nat"][uid]["uniq-id"]
== f"{self._data['protocol']}:{self._data['dst-port']}" == 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"] value = self._ctrl.data["nat"][uid][".id"]
@ -393,8 +401,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
value = None value = None
for uid in self._ctrl.data["nat"]: for uid in self._ctrl.data["nat"]:
if ( if (
self._ctrl.data["nat"][uid]["name"] self._ctrl.data["nat"][uid]["uniq-id"]
== f"{self._data['protocol']}:{self._data['dst-port']}" == 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"] value = self._ctrl.data["nat"][uid][".id"]
@ -504,7 +512,6 @@ class MikrotikControllerPPPSecretSwitch(MikrotikControllerSwitch):
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the PPP Secret switch.""" """Return the name of the PPP Secret switch."""
return f"{self._inst} PPP Secret {self._data['name']}" return f"{self._inst} PPP Secret {self._data['name']}"
@property @property