diff --git a/homeassistant/components/fortios/device_tracker.py b/homeassistant/components/fortios/device_tracker.py index a1507af99dc..e160268e3fc 100644 --- a/homeassistant/components/fortios/device_tracker.py +++ b/homeassistant/components/fortios/device_tracker.py @@ -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