diff --git a/homeassistant/components/fortios/device_tracker.py b/homeassistant/components/fortios/device_tracker.py index 2b2d14f60e0..1b9134bee44 100644 --- a/homeassistant/components/fortios/device_tracker.py +++ b/homeassistant/components/fortios/device_tracker.py @@ -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: diff --git a/homeassistant/components/fortios/manifest.json b/homeassistant/components/fortios/manifest.json index 251cb900adc..cc351441cdd 100644 --- a/homeassistant/components/fortios/manifest.json +++ b/homeassistant/components/fortios/manifest.json @@ -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" } diff --git a/requirements_all.txt b/requirements_all.txt index 78b6eb8fe81..03a5fff0f60 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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