2020-08-24 10:43:31 +00:00
|
|
|
"""Config flow for Shelly integration."""
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import aioshelly
|
|
|
|
import async_timeout
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries, core
|
2020-09-01 12:08:37 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2020-08-24 10:43:31 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
|
|
|
from .const import DOMAIN # pylint:disable=unused-import
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
HOST_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
HTTP_CONNECT_ERRORS = (asyncio.TimeoutError, aiohttp.ClientError)
|
|
|
|
|
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
async def validate_input(hass: core.HomeAssistant, host, data):
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Validate the user input allows us to connect.
|
|
|
|
|
|
|
|
Data has the keys from DATA_SCHEMA with values provided by the user.
|
|
|
|
"""
|
2020-09-06 15:34:51 +00:00
|
|
|
options = aioshelly.ConnectionOptions(
|
|
|
|
host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)
|
|
|
|
)
|
2020-08-24 10:43:31 +00:00
|
|
|
async with async_timeout.timeout(5):
|
|
|
|
device = await aioshelly.Device.create(
|
2020-09-01 12:08:37 +00:00
|
|
|
aiohttp_client.async_get_clientsession(hass),
|
2020-09-06 15:34:51 +00:00
|
|
|
options,
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await device.shutdown()
|
|
|
|
|
|
|
|
# Return info that you want to store in the config entry.
|
|
|
|
return {"title": device.settings["name"], "mac": device.settings["device"]["mac"]}
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Shelly."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
|
|
|
host = None
|
|
|
|
info = None
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
2020-09-01 12:08:37 +00:00
|
|
|
host = user_input[CONF_HOST]
|
2020-08-24 10:43:31 +00:00
|
|
|
try:
|
2020-09-01 12:08:37 +00:00
|
|
|
info = await self._async_get_info(host)
|
2020-08-24 10:43:31 +00:00
|
|
|
except HTTP_CONNECT_ERRORS:
|
|
|
|
errors["base"] = "cannot_connect"
|
2020-08-24 11:39:23 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
2020-08-24 10:43:31 +00:00
|
|
|
else:
|
2020-09-01 12:08:37 +00:00
|
|
|
await self.async_set_unique_id(info["mac"])
|
|
|
|
self._abort_if_unique_id_configured({CONF_HOST: host})
|
|
|
|
self.host = host
|
2020-08-24 10:43:31 +00:00
|
|
|
if info["auth"]:
|
2020-09-01 12:08:37 +00:00
|
|
|
return await self.async_step_credentials()
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
try:
|
2020-09-01 12:08:37 +00:00
|
|
|
device_info = await validate_input(self.hass, self.host, {})
|
2020-08-28 15:33:34 +00:00
|
|
|
except HTTP_CONNECT_ERRORS:
|
2020-08-24 10:43:31 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
return self.async_create_entry(
|
2020-09-01 12:08:37 +00:00
|
|
|
title=device_info["title"] or self.host,
|
2020-08-24 10:43:31 +00:00
|
|
|
data=user_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2020-09-01 12:08:37 +00:00
|
|
|
step_id="user", data_schema=HOST_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_credentials(self, user_input=None):
|
|
|
|
"""Handle the credentials step."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
device_info = await validate_input(self.hass, self.host, user_input)
|
|
|
|
except aiohttp.ClientResponseError as error:
|
|
|
|
if error.status == 401:
|
|
|
|
errors["base"] = "invalid_auth"
|
|
|
|
else:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except HTTP_CONNECT_ERRORS:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=device_info["title"] or self.host,
|
|
|
|
data={**user_input, CONF_HOST: self.host},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
user_input = {}
|
|
|
|
|
|
|
|
schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME, default=user_input.get(CONF_USERNAME)): str,
|
|
|
|
vol.Required(CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="credentials", data_schema=schema, errors=errors
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_zeroconf(self, zeroconf_info):
|
|
|
|
"""Handle zeroconf discovery."""
|
|
|
|
if not zeroconf_info.get("name", "").startswith("shelly"):
|
|
|
|
return self.async_abort(reason="not_shelly")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.info = info = await self._async_get_info(zeroconf_info["host"])
|
|
|
|
except HTTP_CONNECT_ERRORS:
|
|
|
|
return self.async_abort(reason="cannot_connect")
|
|
|
|
|
|
|
|
await self.async_set_unique_id(info["mac"])
|
2020-09-01 12:08:37 +00:00
|
|
|
self._abort_if_unique_id_configured({CONF_HOST: zeroconf_info["host"]})
|
2020-08-24 10:43:31 +00:00
|
|
|
self.host = zeroconf_info["host"]
|
|
|
|
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
|
|
|
self.context["title_placeholders"] = {"name": zeroconf_info["host"]}
|
|
|
|
return await self.async_step_confirm_discovery()
|
|
|
|
|
|
|
|
async def async_step_confirm_discovery(self, user_input=None):
|
|
|
|
"""Handle discovery confirm."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
2020-09-01 12:08:37 +00:00
|
|
|
if self.info["auth"]:
|
|
|
|
return await self.async_step_credentials()
|
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
try:
|
2020-09-01 12:08:37 +00:00
|
|
|
device_info = await validate_input(self.hass, self.host, {})
|
2020-08-28 15:33:34 +00:00
|
|
|
except HTTP_CONNECT_ERRORS:
|
2020-08-24 10:43:31 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=device_info["title"] or self.host, data={"host": self.host}
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="confirm_discovery",
|
|
|
|
description_placeholders={
|
|
|
|
"model": aioshelly.MODEL_NAMES.get(
|
|
|
|
self.info["type"], self.info["type"]
|
|
|
|
),
|
|
|
|
"host": self.host,
|
|
|
|
},
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_get_info(self, host):
|
|
|
|
"""Get info from shelly device."""
|
|
|
|
async with async_timeout.timeout(5):
|
|
|
|
return await aioshelly.get_info(
|
2020-08-27 11:56:20 +00:00
|
|
|
aiohttp_client.async_get_clientsession(self.hass),
|
|
|
|
host,
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|