mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
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>
This commit is contained in:
parent
474ef54477
commit
9cd82e0f00
@ -5,6 +5,7 @@ This component is part of the device_tracker platform.
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from awesomeversion import AwesomeVersion
|
||||||
from fortiosapi import FortiOSAPI
|
from fortiosapi import FortiOSAPI
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -47,49 +48,43 @@ def get_scanner(hass, config):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
status_json = fgt.monitor("system/status", "")
|
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(
|
_LOGGER.error(
|
||||||
"Unsupported FortiOS version, fos_major_version = %s",
|
"Unsupported FortiOS version: %s. Version %s and newer are supported",
|
||||||
fos_major_version,
|
current_version,
|
||||||
|
minimum_version,
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
api_url = "user/device/query"
|
return FortiOSDeviceScanner(fgt)
|
||||||
if fos_major_version == 6:
|
|
||||||
api_url = "user/device/select"
|
|
||||||
|
|
||||||
return FortiOSDeviceScanner(fgt, fos_major_version, api_url)
|
|
||||||
|
|
||||||
|
|
||||||
class FortiOSDeviceScanner(DeviceScanner):
|
class FortiOSDeviceScanner(DeviceScanner):
|
||||||
"""This class queries a FortiOS unit for connected devices."""
|
"""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."""
|
"""Initialize the scanner."""
|
||||||
self._clients = {}
|
self._clients = {}
|
||||||
self._clients_json = {}
|
self._clients_json = {}
|
||||||
self._fgt = fgt
|
self._fgt = fgt
|
||||||
self._fos_major_version = fos_major_version
|
|
||||||
self._api_url = api_url
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update clients from the device."""
|
"""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_json = clients_json
|
||||||
|
|
||||||
self._clients = []
|
self._clients = []
|
||||||
|
|
||||||
if clients_json:
|
if clients_json:
|
||||||
if self._fos_major_version == 6:
|
try:
|
||||||
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"]:
|
for client in clients_json["results"]:
|
||||||
if client["is_online"]:
|
if client["is_online"]:
|
||||||
self._clients.append(client["mac"].upper())
|
self._clients.append(client["mac"].upper())
|
||||||
|
except KeyError as kex:
|
||||||
|
_LOGGER.error("Key not found in clients: %s", kex)
|
||||||
|
|
||||||
def scan_devices(self):
|
def scan_devices(self):
|
||||||
"""Scan for new devices and return a list with found device IDs."""
|
"""Scan for new devices and return a list with found device IDs."""
|
||||||
@ -109,15 +104,15 @@ class FortiOSDeviceScanner(DeviceScanner):
|
|||||||
for client in data["results"]:
|
for client in data["results"]:
|
||||||
if client["mac"] == device:
|
if client["mac"] == device:
|
||||||
try:
|
try:
|
||||||
name = ""
|
name = client["hostname"]
|
||||||
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)
|
_LOGGER.debug("Getting device name=%s", name)
|
||||||
return name
|
return name
|
||||||
except KeyError as kex:
|
except KeyError as kex:
|
||||||
_LOGGER.error("Name not found in client data: %s", kex)
|
_LOGGER.debug(
|
||||||
return None
|
"No hostname found for %s in client data: %s",
|
||||||
|
device,
|
||||||
|
kex,
|
||||||
|
)
|
||||||
|
return device.replace(":", "_")
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user