core/homeassistant/components/ecoforest/config_flow.py

64 lines
1.9 KiB
Python
Raw Normal View History

Add ecoforest integration (#100647) * Add ecoforest integration * fix file title * remove host default from schema, hints will be given in the documentation * moved input validation to async_step_user * ensure we can receive device data while doing entry setup * remove unecessary check before unique id is set * added shorter syntax for async create entry Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use variable to set unique id Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Use _attr_has_entity_name from base entity Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * remove unecessary comments in coordinator * use shorthand for device information * remove empty objects from manifest * remove unecessary flag Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use _async_abort_entries_match to ensure device is not duplicated * remove unecessary host attr * fixed coordinator host attr to be used by entities to identify device * remove unecessary assert * use default device class temperature trasnlation key * reuse base entity description * use device serial number as identifier * remove unused code * Improve logging message Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unused errors Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Raise a generic update failed Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use coordinator directly Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * No need to check for serial number Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * rename variable Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use renamed variable Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * improve assertion Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use serial number in entity unique id Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * raise config entry not ready on setup when error in connection * improve test readability * Improve python syntax Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * abort when device already configured with same serial number * improve tests * fix test name * use coordinator data Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * improve asserts Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * fix ci * improve error handling --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-21 13:18:55 +00:00
"""Config flow for Ecoforest integration."""
from __future__ import annotations
import logging
from typing import Any
from httpx import BasicAuth
from pyecoforest.api import EcoforestApi
from pyecoforest.exceptions import EcoforestAuthenticationRequired
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN, MANUFACTURER
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Ecoforest."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
api = EcoforestApi(
user_input[CONF_HOST],
BasicAuth(user_input[CONF_USERNAME], user_input[CONF_PASSWORD]),
)
device = await api.get()
except EcoforestAuthenticationRequired:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "cannot_connect"
else:
await self.async_set_unique_id(device.serial_number)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"{MANUFACTURER} {device.serial_number}", data=user_input
)
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
errors=errors,
)