1 """Support to use FortiOS device like FortiGate as device tracker.
3 This component is part of the device_tracker platform.
6 from __future__
import annotations
11 from awesomeversion
import AwesomeVersion
12 from fortiosapi
import FortiOSAPI
13 import voluptuous
as vol
16 DOMAIN
as DEVICE_TRACKER_DOMAIN,
17 PLATFORM_SCHEMA
as DEVICE_TRACKER_PLATFORM_SCHEMA,
25 _LOGGER = logging.getLogger(__name__)
26 DEFAULT_VERIFY_SSL =
False
29 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
31 vol.Required(CONF_HOST): cv.string,
32 vol.Required(CONF_TOKEN): cv.string,
33 vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
38 def get_scanner(hass: HomeAssistant, config: ConfigType) -> FortiOSDeviceScanner |
None:
39 """Validate the configuration and return a FortiOSDeviceScanner."""
40 config = config[DEVICE_TRACKER_DOMAIN]
42 host = config[CONF_HOST]
43 verify_ssl = config[CONF_VERIFY_SSL]
44 token = config[CONF_TOKEN]
49 fgt.tokenlogin(host, token, verify_ssl,
None, 12,
"root")
50 except ConnectionError
as ex:
51 _LOGGER.error(
"ConnectionError to FortiOS API: %s", ex)
53 except Exception
as ex:
54 _LOGGER.error(
"Failed to login to FortiOS API: %s", ex)
57 status_json = fgt.monitor(
"system/status",
"")
59 current_version = AwesomeVersion(status_json[
"version"])
60 minimum_version = AwesomeVersion(
"6.4.3")
61 if current_version < minimum_version:
63 "Unsupported FortiOS version: %s. Version %s and newer are supported",
73 """Class which queries a FortiOS unit for connected devices."""
76 """Initialize the scanner."""
77 self.
_clients_clients: list[str] = []
82 """Update clients from the device."""
83 clients_json = self.
_fgt_fgt.monitor(
86 parameters={
"filter":
"format=master_mac|hostname|is_online"},
95 for client
in clients_json[
"results"]:
98 and "master_mac" in client
99 and client[
"is_online"]
101 self.
_clients_clients.append(client[
"master_mac"].upper())
102 except KeyError
as kex:
103 _LOGGER.error(
"Key not found in clients: %s", kex)
106 """Scan for new devices and return a list with found device IDs."""
111 """Return the name of the given device or None if we don't know."""
112 _LOGGER.debug(
"Getting name of device %s", device)
114 device = device.lower()
117 _LOGGER.error(
"No json results to get device names")
120 for client
in data[
"results"]:
121 if "master_mac" in client
and client[
"master_mac"] == device:
122 if "hostname" in client:
123 name = client[
"hostname"]
125 name = client[
"master_mac"].replace(
":",
"_")
def get_device_name(self, device)
FortiOSDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)