Resolve device manufacturer for tracked devices #67

This commit is contained in:
tomaae 2020-12-03 16:41:06 +01:00
parent 0976f880a1
commit 5e7ae94cea
4 changed files with 30 additions and 7 deletions

View file

@ -391,10 +391,12 @@ class MikrotikControllerHostDeviceTracker(MikrotikControllerDeviceTracker):
"connections": { "connections": {
(CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]]) (CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]])
}, },
# "manufacturer": self._ctrl.data["resource"]["platform"],
# "model": self._ctrl.data["resource"]["board-name"],
"default_name": self._data[self._sid_data["sid_name"]], "default_name": self._data[self._sid_data["sid_name"]],
} }
if self._data["manufacturer"] != "":
info["manufacturer"] = self._data["manufacturer"]
if self._sid_data["sid"] == "interface": if self._sid_data["sid"] == "interface":
info["name"] = f"{self._inst} {self._data[self._sid_data['sid_name']]}" info["name"] = f"{self._inst} {self._data[self._sid_data['sid_name']]}"
return info return info

View file

@ -5,7 +5,8 @@
"documentation": "https://github.com/tomaae/homeassistant-mikrotik_router", "documentation": "https://github.com/tomaae/homeassistant-mikrotik_router",
"dependencies": [], "dependencies": [],
"requirements": [ "requirements": [
"librouteros==3.0.0" "librouteros==3.0.0",
"mac-vendor-lookup==0.1.11"
], ],
"codeowners": [ "codeowners": [
"@tomaae" "@tomaae"

View file

@ -5,6 +5,7 @@ import asyncio
import logging import logging
from datetime import timedelta from datetime import timedelta
from ipaddress import ip_address, IPv4Network from ipaddress import ip_address, IPv4Network
from mac_vendor_lookup import AsyncMacLookup
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -110,6 +111,9 @@ class MikrotikControllerData:
self._force_fwupdate_check_callback = None self._force_fwupdate_check_callback = None
self._async_ping_tracked_hosts_callback = None self._async_ping_tracked_hosts_callback = None
self.async_mac_lookup = AsyncMacLookup()
# self.async_mac_lookup.update_vendors()
async def async_init(self): async def async_init(self):
self._force_update_callback = async_track_time_interval( self._force_update_callback = async_track_time_interval(
self.hass, self.force_update, self.option_scan_interval self.hass, self.force_update, self.option_scan_interval
@ -686,7 +690,7 @@ class MikrotikControllerData:
self.major_fw_version = int( self.major_fw_version = int(
self.data["routerboard"].get("firmware").split(".")[0] self.data["routerboard"].get("firmware").split(".")[0]
) )
except Exception as e: except:
_LOGGER.error( _LOGGER.error(
"Mikrotik %s unable to determine major FW version (%s).", "Mikrotik %s unable to determine major FW version (%s).",
self.host, self.host,
@ -1144,10 +1148,11 @@ class MikrotikControllerData:
"mac-address", "mac-address",
"interface", "interface",
"host-name", "host-name",
"manufacturer",
"last-seen", "last-seen",
"available", "available",
], ],
["unknown", "unknown", "unknown", "unknown", False, False], ["unknown", "unknown", "unknown", "unknown", "detect", False, False],
): ):
if key not in self.data["host"][uid]: if key not in self.data["host"][uid]:
self.data["host"][uid][key] = default self.data["host"][uid][key] = default
@ -1224,6 +1229,20 @@ class MikrotikControllerData:
elif self.data["host"][uid]["host-name"] == "unknown": elif self.data["host"][uid]["host-name"] == "unknown":
self.data["host"][uid]["host-name"] = uid self.data["host"][uid]["host-name"] = uid
# Resolve manufacturer
if vals["manufacturer"] == "detect" and vals["address"] != "unknown":
try:
self.data["host"][uid][
"manufacturer"
] = await self.async_mac_lookup.lookup(vals["mac-address"])
print(
"MAC address {} is assigned to {}".format(
vals["mac-address"], self.data["host"][uid]["manufacturer"]
)
)
except:
self.data["host"][uid]["manufacturer"] = ""
# --------------------------- # ---------------------------
# process_accounting # process_accounting
# --------------------------- # ---------------------------

View file

@ -432,10 +432,11 @@ class MikrotikAccountingSensor(MikrotikControllerSensor):
"""Return a accounting description for device registry.""" """Return a accounting description for device registry."""
info = { info = {
"connections": {(CONNECTION_NETWORK_MAC, self._data["mac-address"])}, "connections": {(CONNECTION_NETWORK_MAC, self._data["mac-address"])},
# "manufacturer": self._ctrl.data["resource"]["platform"],
# "model": self._ctrl.data["resource"]["board-name"],
"default_name": self._data["host-name"], "default_name": self._data["host-name"],
} }
if self._data["manufacturer"] != "":
info["manufacturer"] = self._data["manufacturer"]
return info return info
@property @property