mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-10 01:14:33 +02:00
Removed building hosts from ARP table for accounting devices - will use only DHCP leases.
Removed unnecessary code, put all accounting sensors in the same group, set accounting sensors key to mac-address
This commit is contained in:
parent
8244a146de
commit
aee33b8c13
3 changed files with 13 additions and 128 deletions
|
@ -57,9 +57,6 @@ async def async_setup_entry(hass, config_entry):
|
|||
)
|
||||
await mikrotik_controller.hwinfo_update()
|
||||
|
||||
#if track_accounting:
|
||||
# await mikrotik_controller.async_accounting_hosts_update()
|
||||
|
||||
await mikrotik_controller.async_update()
|
||||
|
||||
if not mikrotik_controller.data:
|
||||
|
|
|
@ -585,7 +585,7 @@ class MikrotikControllerData:
|
|||
self.data["resource"]["hdd-usage"] = "unknown"
|
||||
|
||||
# ---------------------------
|
||||
# get_system_routerboard
|
||||
# get_firmware_update
|
||||
# ---------------------------
|
||||
def get_firmware_update(self):
|
||||
"""Check for firmware update on Mikrotik"""
|
||||
|
@ -748,89 +748,6 @@ class MikrotikControllerData:
|
|||
self.data["dhcp"][uid]['available'] = \
|
||||
self.api.arp_ping(self.data["dhcp"][uid]['address'], self.data["dhcp"][uid]['interface'])
|
||||
|
||||
def build_accounting_hosts(self):
|
||||
# Build hosts from already retrieved DHCP Server leases
|
||||
for mac in self.data["dhcp"]:
|
||||
if mac not in self.data["accounting"]:
|
||||
self.data["accounting"][mac] = self.data["dhcp"][mac]
|
||||
|
||||
# self.data["accounting"] = parse_api(
|
||||
# data=self.data["accounting"],
|
||||
# source=self.api.path("/ip/dhcp-server/lease", return_list=True),
|
||||
# key="address",
|
||||
# vals=[
|
||||
# {"name": "address"},
|
||||
# {"name": "mac-address"},
|
||||
# {"name": "host-name"},
|
||||
# {"name": "comment"},
|
||||
# {"name": "disabled", "default": True},
|
||||
# ],
|
||||
# only=[
|
||||
# {"key": "disabled", "value": False},
|
||||
# ],
|
||||
# ensure_vals=[
|
||||
# {"name": "address"},
|
||||
# {"name": "mac-address"},
|
||||
# ]
|
||||
# )
|
||||
|
||||
# Also add hosts not found in DHCP Leases from ARP table
|
||||
arp_hosts = parse_api(
|
||||
data={},
|
||||
source=self.api.path("/ip/arp", return_list=True),
|
||||
key="mac-address",
|
||||
vals=[
|
||||
{"name": "address"},
|
||||
{"name": "mac-address"},
|
||||
{"name": "disabled", "default": True},
|
||||
{"name": "invalid", "default": True},
|
||||
],
|
||||
only=[
|
||||
{"key": "disabled", "value": False},
|
||||
{"key": "invalid", "value": False}
|
||||
],
|
||||
ensure_vals=[
|
||||
{"name": "address"},
|
||||
{"name": "mac-address"},
|
||||
]
|
||||
)
|
||||
|
||||
for mac in arp_hosts:
|
||||
if mac not in self.data["accounting"]:
|
||||
self.data["accounting"][mac] = {
|
||||
"address": arp_hosts[mac]['address'],
|
||||
"mac-address": arp_hosts[mac]['address']
|
||||
}
|
||||
|
||||
# Build name for host
|
||||
dns_data = parse_api(
|
||||
data={},
|
||||
source=self.api.path("/ip/dns/static", return_list=True),
|
||||
key="address",
|
||||
vals=[
|
||||
{"name": "address"},
|
||||
{"name": "name"},
|
||||
],
|
||||
)
|
||||
|
||||
for mac, vals in self.data["accounting"].items():
|
||||
# First try getting DHCP lease comment
|
||||
if str(vals.get('comment', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = vals['comment']
|
||||
# Then entry in static DNS entry
|
||||
elif vals['address'] in dns_data and str(dns_data[vals['address']].get('name', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = dns_data[vals['address']]['name']
|
||||
# And then DHCP lease host-name
|
||||
elif str(vals.get('host-name', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = vals['host-name']
|
||||
# If everything fails use hosts IP address as name
|
||||
else:
|
||||
self.data["accounting"][mac]['name'] = vals['address']
|
||||
|
||||
_LOGGER.debug(f"Generated {len(self.data['accounting'])} accounting devices")
|
||||
_LOGGER.debug(self.data['accounting'])
|
||||
|
||||
|
||||
def _address_part_of_local_network(self, address):
|
||||
address = ip_address(address)
|
||||
for network in self.local_dhcp_networks:
|
||||
|
@ -846,39 +763,10 @@ class MikrotikControllerData:
|
|||
|
||||
def get_accounting(self):
|
||||
"""Get Accounting data from Mikrotik"""
|
||||
|
||||
# Build missing hosts from already retrieved DHCP Server leases
|
||||
for mac in self.data["dhcp"]:
|
||||
for mac, vals in self.data["dhcp"].items():
|
||||
if mac not in self.data["accounting"]:
|
||||
self.data["accounting"][mac] = self.data["dhcp"][mac]
|
||||
|
||||
# Also add hosts not found in DHCP Leases from ARP table
|
||||
arp_hosts = parse_api(
|
||||
data={},
|
||||
source=self.api.path("/ip/arp"),
|
||||
key="mac-address",
|
||||
vals=[
|
||||
{"name": "address"},
|
||||
{"name": "mac-address"},
|
||||
{"name": "disabled", "default": True},
|
||||
{"name": "invalid", "default": True},
|
||||
],
|
||||
only=[
|
||||
{"key": "disabled", "value": False},
|
||||
{"key": "invalid", "value": False}
|
||||
],
|
||||
ensure_vals=[
|
||||
{"name": "address"},
|
||||
{"name": "mac-address"},
|
||||
]
|
||||
)
|
||||
|
||||
for mac in arp_hosts:
|
||||
if mac not in self.data["accounting"]:
|
||||
self.data["accounting"][mac] = {
|
||||
"address": arp_hosts[mac]['address'],
|
||||
"mac-address": arp_hosts[mac]['mac-address']
|
||||
}
|
||||
self.data["accounting"][mac] = vals
|
||||
|
||||
# Build name for host
|
||||
dns_data = parse_api(
|
||||
|
@ -895,18 +783,14 @@ class MikrotikControllerData:
|
|||
# First try getting DHCP lease comment
|
||||
if str(vals.get('comment', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = vals['comment']
|
||||
# Then entry in static DNS entry
|
||||
# Then static DNS entry
|
||||
elif vals['address'] in dns_data and str(dns_data[vals['address']].get('name', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = dns_data[vals['address']]['name']
|
||||
# And then DHCP lease host-name
|
||||
elif str(vals.get('host-name', '').strip()):
|
||||
self.data["accounting"][mac]['name'] = vals['host-name']
|
||||
# If everything fails use hosts IP address as name
|
||||
# If everything fails use hosts DHCP lease host-name
|
||||
else:
|
||||
self.data["accounting"][mac]['name'] = vals['address']
|
||||
self.data["accounting"][mac]['name'] = vals['host-name']
|
||||
|
||||
_LOGGER.debug(f"Generated {len(self.data['accounting'])} accounting devices")
|
||||
_LOGGER.debug(self.data['accounting'])
|
||||
|
||||
traffic_type, traffic_div = self._get_traffic_type_and_div()
|
||||
|
||||
|
@ -926,7 +810,7 @@ class MikrotikControllerData:
|
|||
if time_diff:
|
||||
accounting_data = parse_api(
|
||||
data={},
|
||||
source=self.api.path("/ip/accounting/snapshot", return_list=True),
|
||||
source=self.api.path("/ip/accounting/snapshot"),
|
||||
key=".id",
|
||||
vals=[
|
||||
{"name": ".id"},
|
||||
|
|
|
@ -83,6 +83,7 @@ SENSOR_TYPES = {
|
|||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:download-network",
|
||||
ATTR_LABEL: "LAN TX",
|
||||
ATTR_GROUP: "Accounting",
|
||||
ATTR_UNIT: "ps",
|
||||
ATTR_UNIT_ATTR: "tx-rx-attr",
|
||||
ATTR_PATH: "accounting",
|
||||
|
@ -92,6 +93,7 @@ SENSOR_TYPES = {
|
|||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:upload-network",
|
||||
ATTR_LABEL: "LAN RX",
|
||||
ATTR_GROUP: "Accounting",
|
||||
ATTR_UNIT: "ps",
|
||||
ATTR_UNIT_ATTR: "tx-rx-attr",
|
||||
ATTR_PATH: "accounting",
|
||||
|
@ -101,6 +103,7 @@ SENSOR_TYPES = {
|
|||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:download-network",
|
||||
ATTR_LABEL: "WAN TX",
|
||||
ATTR_GROUP: "Accounting",
|
||||
ATTR_UNIT: "ps",
|
||||
ATTR_UNIT_ATTR: "tx-rx-attr",
|
||||
ATTR_PATH: "accounting",
|
||||
|
@ -110,6 +113,7 @@ SENSOR_TYPES = {
|
|||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:upload-network",
|
||||
ATTR_LABEL: "WAN RX",
|
||||
ATTR_GROUP: "Accounting",
|
||||
ATTR_UNIT: "ps",
|
||||
ATTR_UNIT_ATTR: "tx-rx-attr",
|
||||
ATTR_PATH: "accounting",
|
||||
|
@ -371,7 +375,7 @@ class MikrotikAccountingSensor(MikrotikControllerSensor):
|
|||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique_id for this entity."""
|
||||
return f"{self._inst.lower()}-{self._sensor.lower()}-{self._data['address'].lower()}"
|
||||
return f"{self._inst.lower()}-{self._sensor.lower()}-{self._data['mac-address'].lower()}"
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
|
@ -381,7 +385,7 @@ class MikrotikAccountingSensor(MikrotikControllerSensor):
|
|||
(CONNECTION_NETWORK_MAC, self._data["mac-address"])},
|
||||
"manufacturer": self._ctrl.data["resource"]["platform"],
|
||||
"model": self._ctrl.data["resource"]["board-name"],
|
||||
"name": self._data["name"],
|
||||
"name": SENSOR_TYPES[ATTR_GROUP],
|
||||
}
|
||||
return info
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue