|
| 1 | +"""Config flow for the Legrand Whole Home Lighting integration.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import voluptuous as vol |
| 7 | + |
| 8 | +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult |
| 9 | +from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT |
| 10 | +from homeassistant.exceptions import HomeAssistantError, IntegrationError |
| 11 | + |
| 12 | +from .const import DEFAULT_HOST, DEFAULT_PORT, DEVICE_HUB, DOMAIN |
| 13 | +from .engine.engine import ConnectionState, Engine |
| 14 | + |
| 15 | +_LOGGER = logging.getLogger(__name__) |
| 16 | + |
| 17 | +STEP_USER_DATA_SCHEMA = vol.Schema( |
| 18 | + { |
| 19 | + vol.Optional(CONF_HOST, default=DEFAULT_HOST): str, |
| 20 | + vol.Optional(CONF_PORT, default=DEFAULT_PORT): int, |
| 21 | + vol.Required(CONF_PASSWORD): str, |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class LcConfigFlow(ConfigFlow, domain=DOMAIN): |
| 27 | + """Handle a config flow for Legrand Whole Home Lighting.""" |
| 28 | + |
| 29 | + VERSION = 1 |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + """Initialize.""" |
| 33 | + super().__init__() |
| 34 | + self.data: dict[str, Any] = {} |
| 35 | + |
| 36 | + async def validate_input(self, data: dict[str, Any]) -> dict[str, Any]: |
| 37 | + """Validate the user input allows us to connect.""" |
| 38 | + |
| 39 | + host = data[CONF_HOST] |
| 40 | + port = data[CONF_PORT] |
| 41 | + password = data[CONF_PASSWORD] |
| 42 | + |
| 43 | + if host is None: |
| 44 | + raise ConfigEntryNotReady |
| 45 | + if port is None: |
| 46 | + raise ConfigEntryNotReady |
| 47 | + if password is None: |
| 48 | + raise ConfigEntryNotReady |
| 49 | + |
| 50 | + try: |
| 51 | + engine = Engine(host=host, port=port, password=password) |
| 52 | + |
| 53 | + engine.connect() |
| 54 | + engine.start() |
| 55 | + |
| 56 | + await engine.waitForState(ConnectionState.Ready) |
| 57 | + mac = engine.systemInfo.MACAddress |
| 58 | + |
| 59 | + except TimeoutError as error: |
| 60 | + raise CannotConnect from error |
| 61 | + |
| 62 | + finally: |
| 63 | + engine.disconnect() |
| 64 | + |
| 65 | + # Return info that you want to store in the config entry. |
| 66 | + return {"title": DEVICE_HUB, "mac": mac} |
| 67 | + |
| 68 | + async def async_step_user( |
| 69 | + self, user_input: dict[str, Any] | None = None |
| 70 | + ) -> ConfigFlowResult: |
| 71 | + """Handle the initial step.""" |
| 72 | + |
| 73 | + errors: dict[str, str] = {} |
| 74 | + if user_input: |
| 75 | + try: |
| 76 | + info = await self.validate_input(data=user_input) |
| 77 | + |
| 78 | + except CannotConnect: |
| 79 | + errors["base"] = "cannot_connect" |
| 80 | + |
| 81 | + except InvalidAuth: |
| 82 | + errors["base"] = "invalid_auth" |
| 83 | + |
| 84 | + except Exception: |
| 85 | + _LOGGER.exception("Unexpected exception") |
| 86 | + errors["base"] = "unknown" |
| 87 | + |
| 88 | + else: |
| 89 | + await self.async_set_unique_id(info["mac"]) |
| 90 | + self._abort_if_unique_id_configured() |
| 91 | + return self.async_create_entry(title=info["title"], data=user_input) |
| 92 | + |
| 93 | + return self.async_show_form( |
| 94 | + step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors |
| 95 | + ) |
| 96 | + |
| 97 | + async def async_step_reconfigure( |
| 98 | + self, user_input: dict[str, Any] |
| 99 | + ) -> ConfigFlowResult: |
| 100 | + """Handle reconfiguration.""" |
| 101 | + return await self.async_step_user() |
| 102 | + |
| 103 | + |
| 104 | +class CannotConnect(HomeAssistantError): |
| 105 | + """Error to indicate we cannot connect.""" |
| 106 | + |
| 107 | + |
| 108 | +class InvalidAuth(HomeAssistantError): |
| 109 | + """Error to indicate there is invalid auth.""" |
| 110 | + |
| 111 | + |
| 112 | +class ConfigEntryNotReady(IntegrationError): |
| 113 | + """Error to indicate there is invalid config.""" |
0 commit comments