Update fortios device tracker to support FortiOS 7.0 (#51640)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Kim Frellsen
2021-06-15 00:29:37 +02:00
committed by GitHub
parent 3a2d50fe23
commit e1538594cd
3 changed files with 34 additions and 9 deletions

View File

@ -46,29 +46,50 @@ def get_scanner(hass, config):
_LOGGER.error("Failed to login to FortiOS API: %s", ex)
return None
return FortiOSDeviceScanner(fgt)
status_json = fgt.monitor("system/status", "")
fos_major_version = int(status_json["version"][1])
if fos_major_version < 6 or fos_major_version > 7:
_LOGGER.error(
"Unsupported FortiOS version, fos_major_version = %s",
fos_major_version,
)
return None
api_url = "user/device/query"
if fos_major_version == 6:
api_url = "user/device/select"
return FortiOSDeviceScanner(fgt, fos_major_version, api_url)
class FortiOSDeviceScanner(DeviceScanner):
"""This class queries a FortiOS unit for connected devices."""
def __init__(self, fgt) -> None:
def __init__(self, fgt, fos_major_version, api_url) -> None:
"""Initialize the scanner."""
self._clients = {}
self._clients_json = {}
self._fgt = fgt
self._fos_major_version = fos_major_version
self._api_url = api_url
def update(self):
"""Update clients from the device."""
clients_json = self._fgt.monitor("user/device/select", "")
clients_json = self._fgt.monitor(self._api_url, "")
self._clients_json = clients_json
self._clients = []
if clients_json:
for client in clients_json["results"]:
if client["last_seen"] < 180:
self._clients.append(client["mac"].upper())
if self._fos_major_version == 6:
for client in clients_json["results"]:
if client["last_seen"] < 180:
self._clients.append(client["mac"].upper())
elif self._fos_major_version == 7:
for client in clients_json["results"]:
if client["is_online"]:
self._clients.append(client["mac"].upper())
def scan_devices(self):
"""Scan for new devices and return a list with found device IDs."""
@ -90,7 +111,11 @@ class FortiOSDeviceScanner(DeviceScanner):
for client in data["results"]:
if client["mac"] == device:
try:
name = client["host"]["name"]
name = ""
if self._fos_major_version == 6:
name = client["host"]["name"]
elif self._fos_major_version == 7:
name = client["hostname"]
_LOGGER.debug("Getting device name=%s", name)
return name
except KeyError as kex:

View File

@ -2,7 +2,7 @@
"domain": "fortios",
"name": "FortiOS",
"documentation": "https://www.home-assistant.io/integrations/fortios/",
"requirements": ["fortiosapi==0.10.8"],
"requirements": ["fortiosapi==1.0.5"],
"codeowners": ["@kimfrellsen"],
"iot_class": "local_polling"
}

View File

@ -624,7 +624,7 @@ fnvhash==0.1.0
foobot_async==1.0.0
# homeassistant.components.fortios
fortiosapi==0.10.8
fortiosapi==1.0.5
# homeassistant.components.freebox
freebox-api==0.0.10