Add comment as accounting sensor attribute

This commit is contained in:
Ivan Pavlina 2020-04-07 20:31:16 +02:00
parent a9b72434e2
commit cd9e30682c
2 changed files with 41 additions and 33 deletions

View file

@ -739,18 +739,20 @@ class MikrotikControllerData:
"""Get Accounting data from Mikrotik"""
# Check if accounting and account-local-traffic is enabled
accounting_enabled, local_traffic_enabled = self.api.is_accounting_and_local_traffic_enabled()
traffic_type, traffic_div = self._get_traffic_type_and_div()
if not accounting_enabled:
# If any hosts were created return counters to 0 so sensors wont get stuck on last value
for mac, vals in self.data["accounting"].items():
self.data['accounting'][mac]["tx-rx-attr"] = traffic_type
if 'wan-tx' in vals:
self.data["accounting"]['wan-tx'] = 0.0
self.data["accounting"][mac]['wan-tx'] = 0.0
if 'wan-rx' in vals:
self.data["accounting"]['wan-rx'] = 0.0
self.data["accounting"][mac]['wan-rx'] = 0.0
if 'lan-tx' in vals:
self.data["accounting"]['lan-tx'] = 0.0
self.data["accounting"][mac]['lan-tx'] = 0.0
if 'lan-rx' in vals:
self.data["accounting"]['lan-rx'] = 0.0
self.data["accounting"][mac]['lan-rx'] = 0.0
return
# Build missing hosts from already retrieved DHCP Server leases
@ -760,17 +762,23 @@ class MikrotikControllerData:
'address': vals['address'],
'mac-address': vals['mac-address'],
'host-name': vals['host-name'],
'comment': vals['comment']
}
# Build missing hosts from already retrieved ARP list
host_update_from_arp = False
for entry in self.raw_arp_entries:
if entry['mac-address'] not in self.data["accounting"]:
self.data["accounting"][entry['mac-address']] = {
'address': entry['address'],
'mac-address': entry['mac-address'],
'host-name': '',
'comment': ''
}
host_update_from_arp = True
# Build name for host
# If some host was added from ARP table build new host-name for it from static DNS entry. Fallback to MAC
if host_update_from_arp:
dns_data = parse_api(
data={},
source=self.api.path("/ip/dns/static"),
@ -781,23 +789,21 @@ class MikrotikControllerData:
],
)
# Try to build hostname from DNS static entry
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 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']
# Then DHCP lease host-name
elif str(vals.get('host-name', '').strip()):
self.data["accounting"][mac]['name'] = vals['host-name']
# If everything fails fallback to device's MAC address
else:
self.data["accounting"][mac]['name'] = vals['mac-address']
if not str(vals.get('host-name', '')).strip() or vals['host-name'] is 'unknown':
if vals['address'] in dns_data and str(dns_data[vals['address']].get('name', '')).strip():
self.data["accounting"][mac]['host-name'] = dns_data[vals['address']]['name']
_LOGGER.debug(f"Generated {len(self.data['accounting'])} accounting devices")
# Check if any host still have empty 'host-name'. Default it to MAC.
# Same check for 'comment' (pretty name)
for mac, vals in self.data["accounting"].items():
if not str(vals.get('host-name', '')).strip() or vals['host-name'] is 'unknown':
self.data["accounting"][mac]['host-name'] = mac
if not str(vals.get('comment', '')).strip() or vals['host-name'] is 'comment':
self.data["accounting"][mac]['comment'] = mac
traffic_type, traffic_div = self._get_traffic_type_and_div()
_LOGGER.debug(f"Working with {len(self.data['accounting'])} accounting devices")
# Build temp accounting values dict with ip address as key
# Also set traffic type for each item

View file

@ -124,6 +124,7 @@ SENSOR_TYPES = {
DEVICE_ATTRIBUTES_ACCOUNTING = [
"address",
"mac-address",
"comment"
]
@ -194,7 +195,7 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
if "accounting_" in sensor:
for uid in mikrotik_controller.data["accounting"]:
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['accounting'][uid]['name']}"
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['accounting'][uid]['mac-address']}"
if item_id in sensors:
if sensors[item_id].enabled:
sensors[item_id].async_schedule_update_ha_state()
@ -370,7 +371,7 @@ class MikrotikAccountingSensor(MikrotikControllerSensor):
@property
def name(self):
"""Return the name."""
return f"{self._inst} {self._data['name']} {self._type[ATTR_LABEL]}"
return f"{self._inst} {self._data['host-name']} {self._type[ATTR_LABEL]} "
@property
def unique_id(self):
@ -409,8 +410,9 @@ class MikrotikAccountingSensor(MikrotikControllerSensor):
async def async_added_to_hass(self):
"""Port entity created."""
_LOGGER.debug(
"New sensor %s (%s %s)",
"New sensor %s (%s [%s] %s)",
self._inst,
self._data["name"],
self._data["host-name"],
self._data["mac-address"],
self._sensor,
)