113 lines
4.6 KiB
Python
113 lines
4.6 KiB
Python
"""Config flow for Olimex ESP32-C6-EVB integration."""
|
|
import logging
|
|
import voluptuous as vol
|
|
import aiohttp
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from .const import DOMAIN, DEFAULT_PORT, CONF_CALLBACK_IP, DEFAULT_CALLBACK_IP
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class OlimexESP32C6ConfigFlow(config_entries.ConfigFlow, domain="olimex_esp32_c6"):
|
|
"""Handle a config flow for Olimex ESP32-C6-EVB."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
"""Handle the initial step."""
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
host = user_input.get(CONF_HOST, "192.168.0.181")
|
|
port = user_input.get(CONF_PORT, DEFAULT_PORT)
|
|
|
|
# Validate connection to the board
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
url = f"http://{host}:{port}/api/status"
|
|
async with session.get(
|
|
url,
|
|
timeout=aiohttp.ClientTimeout(total=10)
|
|
) as resp:
|
|
if resp.status != 200:
|
|
errors["base"] = "cannot_connect"
|
|
else:
|
|
await self.async_set_unique_id(f"{host}:{port}")
|
|
self._abort_if_unique_id_configured()
|
|
|
|
_LOGGER.info("Successfully connected to Olimex ESP32-C6 at %s", host)
|
|
return self.async_create_entry(
|
|
title=f"Olimex ESP32-C6 ({host})",
|
|
data={
|
|
CONF_HOST: host,
|
|
CONF_PORT: port,
|
|
CONF_CALLBACK_IP: user_input.get(
|
|
CONF_CALLBACK_IP, DEFAULT_CALLBACK_IP
|
|
),
|
|
},
|
|
)
|
|
except aiohttp.ClientError:
|
|
errors["base"] = "cannot_connect"
|
|
_LOGGER.error("Failed to connect to Olimex ESP32-C6 at %s", host)
|
|
except Exception as err:
|
|
errors["base"] = "unknown"
|
|
_LOGGER.error("Error in config flow: %s", err)
|
|
|
|
data_schema = vol.Schema({
|
|
vol.Required(CONF_HOST, default="192.168.0.181"): str,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
vol.Optional(CONF_CALLBACK_IP, default=DEFAULT_CALLBACK_IP): str,
|
|
})
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=data_schema,
|
|
errors=errors,
|
|
)
|
|
|
|
async def async_step_import(self, import_data):
|
|
"""Handle import from YAML configuration."""
|
|
_LOGGER.debug("Importing Olimex ESP32-C6 from YAML: %s", import_data)
|
|
host = import_data.get(CONF_HOST, "192.168.0.181")
|
|
port = import_data.get(CONF_PORT, DEFAULT_PORT)
|
|
|
|
# Check if already configured
|
|
await self.async_set_unique_id(f"{host}:{port}")
|
|
self._abort_if_unique_id_configured()
|
|
|
|
# Validate connection
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
url = f"http://{host}:{port}/api/status"
|
|
async with session.get(
|
|
url,
|
|
timeout=aiohttp.ClientTimeout(total=10)
|
|
) as resp:
|
|
if resp.status == 200:
|
|
_LOGGER.info("Successfully imported Olimex ESP32-C6 from YAML at %s", host)
|
|
return self.async_create_entry(
|
|
title=f"Olimex ESP32-C6 ({host})",
|
|
data={
|
|
CONF_HOST: host,
|
|
CONF_PORT: port,
|
|
CONF_CALLBACK_IP: import_data.get(CONF_CALLBACK_IP, DEFAULT_CALLBACK_IP),
|
|
},
|
|
)
|
|
except Exception as err:
|
|
_LOGGER.error("Failed to import Olimex ESP32-C6 from YAML: %s", err)
|
|
|
|
# If validation fails, still create entry but log warning
|
|
_LOGGER.warning("Could not validate Olimex ESP32-C6 at %s, creating entry anyway", host)
|
|
return self.async_create_entry(
|
|
title=f"Olimex ESP32-C6 ({host})",
|
|
data={
|
|
CONF_HOST: host,
|
|
CONF_PORT: port,
|
|
CONF_CALLBACK_IP: import_data.get(CONF_CALLBACK_IP, DEFAULT_CALLBACK_IP),
|
|
},
|
|
)
|