Update fortios device_tracker (#61970)
* FortiOS 7.0 support Added support for FortiOS 7.0 and retaining FortiOS 6.4 support. Since an API was deprecated in FortiOS 7.0 and replace by a new API the integration now also support FortiOS 7.0. It is planned to deprecate the support for FortiOS 6.4 in a year * updated requirement to fortios * Update device_tracker.py indentation fix * Update device_tracker.py run flake8 fixes * flake8 fixes * Update device_tracker.py black fixing line breaks * Update device_tracker.py black fixes * Update device_tracker.py linter fixes * Update device_tracker.py linter fixes * Update device_tracker.py linter fix * Update device_tracker.py removed comment that pylint does not like :-~ * Update homeassistant/components/fortios/device_tracker.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/fortios/device_tracker.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/fortios/device_tracker.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update device_tracker.py to resolve double guard for supported versions. * updated fortios device tracker Deprecated old api. cleaned up code. better checking with try-catch removed unnecessary error output. * Update device_tracker.py lint compliance. * Update device_tracker.py lint updates * Update device_tracker.py lint updates * Update device_tracker.py lint updates * Update device_tracker.py lint updates * Update device_tracker.py updated to use awesomeversion component. * Update device_tracker.py pylint updates * Update device_tracker.py pylint updates * Clean up * Simplify Co-authored-by: Martin Hjelmare <marhje52@gmail.com>pull/62202/head
parent
474ef54477
commit
9cd82e0f00
|
@ -5,6 +5,7 @@ This component is part of the device_tracker platform.
|
|||
"""
|
||||
import logging
|
||||
|
||||
from awesomeversion import AwesomeVersion
|
||||
from fortiosapi import FortiOSAPI
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -47,49 +48,43 @@ def get_scanner(hass, config):
|
|||
return None
|
||||
|
||||
status_json = fgt.monitor("system/status", "")
|
||||
fos_major_version = int(status_json["version"][1])
|
||||
|
||||
if fos_major_version < 6 or fos_major_version > 7:
|
||||
current_version = AwesomeVersion(status_json["version"])
|
||||
minimum_version = AwesomeVersion("6.4.3")
|
||||
if current_version < minimum_version:
|
||||
_LOGGER.error(
|
||||
"Unsupported FortiOS version, fos_major_version = %s",
|
||||
fos_major_version,
|
||||
"Unsupported FortiOS version: %s. Version %s and newer are supported",
|
||||
current_version,
|
||||
minimum_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)
|
||||
return FortiOSDeviceScanner(fgt)
|
||||
|
||||
|
||||
class FortiOSDeviceScanner(DeviceScanner):
|
||||
"""This class queries a FortiOS unit for connected devices."""
|
||||
|
||||
def __init__(self, fgt, fos_major_version, api_url) -> None:
|
||||
def __init__(self, fgt) -> 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(self._api_url, "")
|
||||
clients_json = self._fgt.monitor("user/device/query", "")
|
||||
self._clients_json = clients_json
|
||||
|
||||
self._clients = []
|
||||
|
||||
if clients_json:
|
||||
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:
|
||||
try:
|
||||
for client in clients_json["results"]:
|
||||
if client["is_online"]:
|
||||
self._clients.append(client["mac"].upper())
|
||||
except KeyError as kex:
|
||||
_LOGGER.error("Key not found in clients: %s", kex)
|
||||
|
||||
def scan_devices(self):
|
||||
"""Scan for new devices and return a list with found device IDs."""
|
||||
|
@ -109,15 +104,15 @@ class FortiOSDeviceScanner(DeviceScanner):
|
|||
for client in data["results"]:
|
||||
if client["mac"] == device:
|
||||
try:
|
||||
name = ""
|
||||
if self._fos_major_version == 6:
|
||||
name = client["host"]["name"]
|
||||
elif self._fos_major_version == 7:
|
||||
name = client["hostname"]
|
||||
name = client["hostname"]
|
||||
_LOGGER.debug("Getting device name=%s", name)
|
||||
return name
|
||||
except KeyError as kex:
|
||||
_LOGGER.error("Name not found in client data: %s", kex)
|
||||
return None
|
||||
_LOGGER.debug(
|
||||
"No hostname found for %s in client data: %s",
|
||||
device,
|
||||
kex,
|
||||
)
|
||||
return device.replace(":", "_")
|
||||
|
||||
return None
|
||||
|
|
Loading…
Reference in New Issue