2023-12-29 12:40:35 +01:00
|
|
|
"""Config flow for Easy Computer Manager integration."""
|
2023-04-04 13:44:07 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries, exceptions
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-12-30 20:21:37 +01:00
|
|
|
|
2024-08-25 23:20:39 +02:00
|
|
|
from .computer import Computer
|
2023-04-04 13:44:07 +02:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional("name"): str,
|
|
|
|
vol.Required("host"): str,
|
|
|
|
vol.Required("mac"): str,
|
|
|
|
vol.Required("dualboot"): bool,
|
|
|
|
vol.Required("username"): str,
|
|
|
|
vol.Required("password"): str,
|
|
|
|
vol.Optional("port", default=22): int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Hub:
|
|
|
|
"""Dummy for test connection"""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, host: str, username: str, password: str, port: int) -> None:
|
|
|
|
"""Init dummy hub."""
|
|
|
|
self._host = host
|
|
|
|
self._username = username
|
|
|
|
self._password = password
|
|
|
|
self._port = port
|
|
|
|
self._hass = hass
|
|
|
|
self._name = host
|
|
|
|
self._id = host.lower()
|
|
|
|
|
2024-08-25 23:20:39 +02:00
|
|
|
self.computer = Computer(host, "", username, password, port)
|
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
@property
|
|
|
|
def hub_id(self) -> str:
|
|
|
|
"""ID for dummy."""
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
async def test_connection(self) -> bool:
|
|
|
|
"""Test connectivity to the computer is OK."""
|
|
|
|
try:
|
2024-08-25 23:20:39 +02:00
|
|
|
# TODO: check if reachable
|
|
|
|
_LOGGER.info("Testing connection to %s", self._host)
|
|
|
|
return True
|
|
|
|
|
2024-08-26 19:01:32 +02:00
|
|
|
except Exception:
|
2023-04-04 13:44:07 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
|
|
|
|
"""Validate the user input allows us to connect.
|
|
|
|
Data has the keys from DATA_SCHEMA with values provided by the user.
|
|
|
|
"""
|
|
|
|
if len(data["host"]) < 3:
|
|
|
|
raise InvalidHost
|
|
|
|
|
2023-12-31 17:30:27 +01:00
|
|
|
hub = Hub(hass, data["host"], data["username"], data["password"], data["port"])
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-25 23:20:39 +02:00
|
|
|
_LOGGER.info("Validating configuration")
|
2023-12-30 21:22:32 +01:00
|
|
|
if not await hub.test_connection():
|
2023-04-04 13:44:07 +02:00
|
|
|
raise CannotConnect
|
|
|
|
|
|
|
|
return {"title": data["host"]}
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow"""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
info = await validate_input(self.hass, user_input)
|
|
|
|
return self.async_create_entry(title=info["title"], data=user_input)
|
2024-08-26 19:01:32 +02:00
|
|
|
except (CannotConnect, InvalidHost) as ex:
|
2023-12-30 21:22:32 +01:00
|
|
|
errors["base"] = str(ex)
|
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception: %s", ex)
|
2023-04-04 13:44:07 +02:00
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
2023-12-30 21:22:32 +01:00
|
|
|
return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA, errors=errors)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CannotConnect(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate we cannot connect."""
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidHost(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate there is an invalid hostname."""
|