2019-07-16 11:15:59 +02:00
|
|
|
"""Fortigate integration."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 21:25:30 +02:00
|
|
|
CONF_DEVICES,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_USERNAME,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2019-07-16 11:15:59 +02:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.discovery import async_load_platform
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
DOMAIN = "fortigate"
|
2019-07-16 11:15:59 +02:00
|
|
|
|
|
|
|
DATA_FGT = DOMAIN
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-07-16 11:15:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Start the Fortigate component."""
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
|
|
|
|
host = conf[CONF_HOST]
|
|
|
|
user = conf[CONF_USERNAME]
|
|
|
|
api_key = conf[CONF_API_KEY]
|
|
|
|
devices = conf[CONF_DEVICES]
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
is_success = await async_setup_fortigate(hass, config, host, user, api_key, devices)
|
2019-07-16 11:15:59 +02:00
|
|
|
|
|
|
|
return is_success
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_fortigate(hass, config, host, user, api_key, devices):
|
|
|
|
"""Start up the Fortigate component platforms."""
|
|
|
|
from pyFGT.fortigate import FGTConnectionError, FortiGate
|
|
|
|
|
|
|
|
fgt = FortiGate(host, user, apikey=api_key, disable_request_warnings=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
fgt.login()
|
|
|
|
except FGTConnectionError:
|
|
|
|
_LOGGER.error("Failed to connect to Fortigate")
|
|
|
|
return False
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
hass.data[DATA_FGT] = {"fgt": fgt, "devices": devices}
|
2019-07-16 11:15:59 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
hass.async_create_task(
|
|
|
|
async_load_platform(hass, "device_tracker", DOMAIN, {}, config)
|
|
|
|
)
|
2019-07-16 11:15:59 +02:00
|
|
|
|
|
|
|
async def close_fgt(event):
|
|
|
|
"""Close Fortigate connection on HA Stop."""
|
|
|
|
fgt.logout()
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_fgt)
|
|
|
|
|
|
|
|
return True
|