* Add ecowitt integration * add tests * use total * use total * test coverage * Update homeassistant/components/ecowitt/__init__.py Co-authored-by: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/ecowitt/binary_sensor.py Co-authored-by: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/ecowitt/entity.py Co-authored-by: Paulus Schoutsen <balloob@gmail.com> * Update homeassistant/components/ecowitt/diagnostics.py Co-authored-by: Paulus Schoutsen <balloob@gmail.com> * add to async_on_unload * remove attr_name / unload callback * support unload platforms * using replace * address mapping * update type * mark final * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix bracket * Fix another bracket * Address comment * Add strings * update tests * Update homeassistant/components/ecowitt/strings.json Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * update text * Update homeassistant/components/ecowitt/strings.json Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
"""Config flow for ecowitt."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import secrets
|
|
from typing import Any
|
|
|
|
from aioecowitt import EcoWittListener
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_PORT
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from .const import CONF_PATH, DEFAULT_PORT, DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
vol.Optional(CONF_PATH, default=f"/{secrets.token_urlsafe(16)}"): cv.string,
|
|
}
|
|
)
|
|
|
|
|
|
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
|
|
"""Validate user input."""
|
|
# Check if the port is in use
|
|
try:
|
|
listener = EcoWittListener(port=data[CONF_PORT])
|
|
await listener.start()
|
|
await listener.stop()
|
|
except OSError:
|
|
raise InvalidPort from None
|
|
|
|
return {"title": f"Ecowitt on port {data[CONF_PORT]}"}
|
|
|
|
|
|
class EcowittConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Config flow for the Ecowitt."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Handle the initial step."""
|
|
if user_input is None:
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
|
|
)
|
|
|
|
errors = {}
|
|
|
|
# Check if the port is in use by another config entry
|
|
self._async_abort_entries_match({CONF_PORT: user_input[CONF_PORT]})
|
|
|
|
try:
|
|
info = await validate_input(self.hass, user_input)
|
|
except InvalidPort:
|
|
errors["base"] = "invalid_port"
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
return self.async_create_entry(title=info["title"], data=user_input)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
)
|
|
|
|
|
|
class InvalidPort(HomeAssistantError):
|
|
"""Error to indicate there port is not usable."""
|