2019-03-24 15:16:50 +00:00
|
|
|
"""Config flow to configure Axis devices."""
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MAC,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
from .const import CONF_MODEL, DOMAIN
|
|
|
|
from .device import get_device
|
2020-01-04 07:58:18 +00:00
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
AXIS_OUI = {"00408C", "ACCC8E", "B8A44F"}
|
2019-06-10 16:12:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_FILE = "axis.conf"
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_TYPES = ["motion", "vmd3", "pir", "sound", "daynight", "tampering", "input"]
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORMS = ["camera"]
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
AXIS_INCLUDE = EVENT_TYPES + PLATFORMS
|
|
|
|
|
|
|
|
DEFAULT_PORT = 80
|
|
|
|
|
|
|
|
|
2019-09-01 11:15:48 +00:00
|
|
|
class AxisFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Handle a Axis config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize the Axis config flow."""
|
|
|
|
self.device_config = {}
|
|
|
|
self.model = None
|
|
|
|
self.name = None
|
|
|
|
self.serial_number = None
|
|
|
|
|
|
|
|
self.discovery_schema = {}
|
|
|
|
self.import_schema = {}
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a Axis config flow start.
|
|
|
|
|
|
|
|
Manage device specific parameters.
|
|
|
|
"""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
self.device_config = {
|
|
|
|
CONF_HOST: user_input[CONF_HOST],
|
|
|
|
CONF_PORT: user_input[CONF_PORT],
|
|
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
2019-03-24 15:16:50 +00:00
|
|
|
}
|
|
|
|
device = await get_device(self.hass, self.device_config)
|
|
|
|
|
2019-04-16 08:46:29 +00:00
|
|
|
self.serial_number = device.vapix.params.system_serialnumber
|
2020-01-05 09:11:17 +00:00
|
|
|
config_entry = await self.async_set_unique_id(self.serial_number)
|
|
|
|
if config_entry:
|
|
|
|
return self._update_entry(
|
|
|
|
config_entry,
|
|
|
|
host=user_input[CONF_HOST],
|
|
|
|
port=user_input[CONF_PORT],
|
|
|
|
)
|
2019-04-02 18:13:11 +00:00
|
|
|
|
2019-04-16 08:46:29 +00:00
|
|
|
self.model = device.vapix.params.prodnbr
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
return await self._create_entry()
|
|
|
|
|
|
|
|
except AuthenticationRequired:
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "faulty_credentials"
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
except CannotConnect:
|
2019-07-31 19:25:30 +00:00
|
|
|
errors["base"] = "device_unavailable"
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-01-03 20:25:31 +00:00
|
|
|
data = self.discovery_schema or {
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
|
|
|
|
}
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user",
|
2019-03-24 15:16:50 +00:00
|
|
|
description_placeholders=self.device_config,
|
|
|
|
data_schema=vol.Schema(data),
|
2019-07-31 19:25:30 +00:00
|
|
|
errors=errors,
|
2019-03-24 15:16:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _create_entry(self):
|
|
|
|
"""Create entry for device.
|
|
|
|
|
|
|
|
Generate a name to be used as a prefix for device entities.
|
|
|
|
"""
|
2020-01-03 20:25:31 +00:00
|
|
|
same_model = [
|
|
|
|
entry.data[CONF_NAME]
|
|
|
|
for entry in self.hass.config_entries.async_entries(DOMAIN)
|
|
|
|
if entry.data[CONF_MODEL] == self.model
|
|
|
|
]
|
|
|
|
|
|
|
|
self.name = f"{self.model}"
|
|
|
|
for idx in range(len(same_model) + 1):
|
|
|
|
self.name = f"{self.model} {idx}"
|
|
|
|
if self.name not in same_model:
|
|
|
|
break
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
CONF_DEVICE: self.device_config,
|
|
|
|
CONF_NAME: self.name,
|
|
|
|
CONF_MAC: self.serial_number,
|
|
|
|
CONF_MODEL: self.model,
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:11:36 +00:00
|
|
|
title = f"{self.model} - {self.serial_number}"
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=title, data=data)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-01-04 07:58:18 +00:00
|
|
|
def _update_entry(self, entry, host, port):
|
|
|
|
"""Update existing entry."""
|
|
|
|
if (
|
|
|
|
entry.data[CONF_DEVICE][CONF_HOST] == host
|
|
|
|
and entry.data[CONF_DEVICE][CONF_PORT] == port
|
|
|
|
):
|
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
2019-04-02 18:13:11 +00:00
|
|
|
entry.data[CONF_DEVICE][CONF_HOST] = host
|
2020-01-04 07:58:18 +00:00
|
|
|
entry.data[CONF_DEVICE][CONF_PORT] = port
|
|
|
|
|
2019-04-02 18:13:11 +00:00
|
|
|
self.hass.config_entries.async_update_entry(entry)
|
2020-01-04 07:58:18 +00:00
|
|
|
return self.async_abort(reason="updated_configuration")
|
2019-04-02 18:13:11 +00:00
|
|
|
|
2019-05-21 22:36:26 +00:00
|
|
|
async def async_step_zeroconf(self, discovery_info):
|
2020-01-04 07:58:18 +00:00
|
|
|
"""Prepare configuration for a discovered Axis device."""
|
|
|
|
serial_number = discovery_info["properties"]["macaddress"]
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-01-04 07:58:18 +00:00
|
|
|
if serial_number[:6] not in AXIS_OUI:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="not_axis_device")
|
2019-06-10 16:12:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if discovery_info[CONF_HOST].startswith("169.254"):
|
|
|
|
return self.async_abort(reason="link_local_address")
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-01-05 09:11:17 +00:00
|
|
|
config_entry = await self.async_set_unique_id(serial_number)
|
|
|
|
if config_entry:
|
|
|
|
return self._update_entry(
|
|
|
|
config_entry,
|
|
|
|
host=discovery_info[CONF_HOST],
|
|
|
|
port=discovery_info[CONF_PORT],
|
|
|
|
)
|
2019-04-02 18:13:11 +00:00
|
|
|
|
2019-10-16 18:45:03 +00:00
|
|
|
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
|
|
|
self.context["title_placeholders"] = {
|
|
|
|
"name": discovery_info["hostname"][:-7],
|
|
|
|
"host": discovery_info[CONF_HOST],
|
|
|
|
}
|
|
|
|
|
2020-01-03 20:25:31 +00:00
|
|
|
self.discovery_schema = {
|
|
|
|
vol.Required(CONF_HOST, default=discovery_info[CONF_HOST]): str,
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
vol.Required(CONF_PORT, default=discovery_info[CONF_PORT]): int,
|
2019-03-24 15:16:50 +00:00
|
|
|
}
|
2020-01-03 20:25:31 +00:00
|
|
|
|
|
|
|
return await self.async_step_user()
|