87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
"""Config flow for BenQ Smart Board integration."""
|
|
import socket
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
|
from homeassistant.core import callback
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from . import DOMAIN
|
|
from .benq_smartboard_lib import BenQSmartBoard, BenQSmartBoardError, ConnectionError
|
|
|
|
DEFAULT_PORT = 4660
|
|
|
|
|
|
class BenQSmartBoardConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for BenQ Smart Board."""
|
|
|
|
VERSION = 1
|
|
|
|
@staticmethod
|
|
@callback
|
|
def async_get_options_flow(config_entry):
|
|
"""Define the flow to handle options."""
|
|
return BenQSmartBoardOptionsFlowHandler(config_entry)
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
"""Handle the initial step for user setup."""
|
|
errors = {}
|
|
if user_input is not None:
|
|
host = user_input[CONF_HOST]
|
|
port = user_input[CONF_PORT]
|
|
|
|
# Attempt a short connection test
|
|
board = BenQSmartBoard(host, port, timeout=2.0)
|
|
try:
|
|
await self.hass.async_add_executor_job(board.connect)
|
|
board.disconnect()
|
|
except (ConnectionError, BenQSmartBoardError, socket.error):
|
|
errors["base"] = "cannot_connect"
|
|
else:
|
|
# If no connection error, proceed
|
|
await self.async_set_unique_id(f"benq_smartboard_{host}_{port}")
|
|
self._abort_if_unique_id_configured()
|
|
|
|
return self.async_create_entry(
|
|
title=f"BenQ Smart Board ({host})",
|
|
data={
|
|
CONF_HOST: host,
|
|
CONF_PORT: port,
|
|
},
|
|
)
|
|
|
|
data_schema = vol.Schema({
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
})
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=data_schema,
|
|
errors=errors
|
|
)
|
|
|
|
|
|
class BenQSmartBoardOptionsFlowHandler(config_entries.OptionsFlow):
|
|
"""Handle an options flow for adjusting settings after setup."""
|
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
|
"""Store the current config entry for reference."""
|
|
self.config_entry = config_entry
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
"""Manage the options for the BenQ Smart Board."""
|
|
if user_input is not None:
|
|
# In a real scenario, you might re-validate or re-connect
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
data_schema = vol.Schema({
|
|
vol.Optional(
|
|
CONF_PORT,
|
|
default=self.config_entry.data.get(CONF_PORT, DEFAULT_PORT),
|
|
): cv.port,
|
|
})
|
|
|
|
return self.async_show_form(step_id="init", data_schema=data_schema)
|