hass-core/homeassistant/components/chacon_dio/config_flow.py
cnico 092e362f01
Add new integration for Dio Chacon cover devices (#116267)
* Dio Chacon integration addition with config flow and cover entity

* Addition of model information for device

* Addition of light and service to force reloading states

* Logger improvements

* Convert light to switch and usage of v1.0.0 of the api

* 100% for tests coverage

* Invalid credential implementation and rebase on latest ha dev code

* Simplify PR with only one platform

* Ruff correction

* restore original .gitignore content

* Correction of cover state bug when using cover when using actions on cover group.

* Begin of corrections following review.

* unit tests correction

* Refactor with a coordinator as asked by review

* Implemented a post constructor callback init method via dio-chacon-api-1.0.2. Improved typing.

* Corrections for 2nd review

* Reimplemented without coordinator as reviewed with Joostlek

* Review improvement

* generalize callback in entity

* Other review improvements

* Refactored tests for readability

* Test 100% operationals

* Tests review corrections

* Tests review corrections

* Review tests improvements

* simplified tests with snapshots and callback method

* Final fixes

* Final fixes

* Final fixes

* Rename to chacon_dio

---------

Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-04 16:45:20 +02:00

67 lines
1.9 KiB
Python

"""Config flow for chacon_dio integration."""
from __future__ import annotations
import logging
from typing import Any
from dio_chacon_wifi_api import DIOChaconAPIClient
from dio_chacon_wifi_api.exceptions import DIOChaconAPIError, DIOChaconInvalidAuthError
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
class ChaconDioConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for chacon_dio."""
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
client = DIOChaconAPIClient(
user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
)
try:
_user_id: str = await client.get_user_id()
except DIOChaconAPIError:
errors["base"] = "cannot_connect"
except DIOChaconInvalidAuthError:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(_user_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"Chacon DiO {user_input[CONF_USERNAME]}",
data=user_input,
)
finally:
await client.disconnect()
return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA,
errors=errors,
)