Files

78 lines
2.9 KiB
Python
Raw Permalink Normal View History

2023-09-02 16:30:03 +08:00
import voluptuous as vol
import logging
2025-09-12 00:15:14 +08:00
from typing import Any
2023-09-02 16:30:03 +08:00
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant import config_entries
2025-09-12 00:15:14 +08:00
from homeassistant.config_entries import ConfigFlowResult
2023-09-17 19:40:54 +08:00
from homeassistant.core import callback
2023-09-02 16:30:03 +08:00
from homeassistant.const import (
CONF_TYPE,
)
from .const import (
CONF_ACCOUNT,
2025-09-12 00:15:14 +08:00
CONF_PASSWORD,
DOMAIN,
CONF_SERVER, CONF_SERVERS
2023-09-02 16:30:03 +08:00
)
2025-09-12 00:15:14 +08:00
from .core.cloud import get_midea_cloud
2023-09-02 16:30:03 +08:00
_LOGGER = logging.getLogger(__name__)
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_session = None
2023-09-17 19:40:54 +08:00
@staticmethod
@callback
def async_get_options_flow(config_entry):
return OptionsFlowHandler(config_entry)
2025-09-12 00:15:14 +08:00
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
errors: dict[str, str] = {}
2023-09-03 22:15:41 +08:00
if self._session is None:
self._session = async_create_clientsession(self.hass)
2023-09-02 16:30:03 +08:00
if user_input is not None:
2025-09-12 00:15:14 +08:00
cloud = get_midea_cloud(
session=self._session,
cloud_name=CONF_SERVERS[user_input[CONF_SERVER]],
account=user_input[CONF_ACCOUNT],
password=user_input[CONF_PASSWORD]
)
2025-09-09 23:52:48 +08:00
try:
2025-09-12 00:15:14 +08:00
if await cloud.login():
await self.async_set_unique_id(user_input[CONF_ACCOUNT])
self._abort_if_unique_id_configured()
2025-09-09 23:52:48 +08:00
return self.async_create_entry(
2025-09-12 00:15:14 +08:00
title=user_input[CONF_ACCOUNT],
2025-09-09 23:52:48 +08:00
data={
CONF_TYPE: CONF_ACCOUNT,
CONF_ACCOUNT: user_input[CONF_ACCOUNT],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_SERVER: user_input[CONF_SERVER]
2025-09-12 00:15:14 +08:00
},
)
2025-09-09 23:52:48 +08:00
else:
2025-09-12 00:15:14 +08:00
errors["base"] = "login_failed"
2025-09-09 23:52:48 +08:00
except Exception as e:
2025-09-12 00:15:14 +08:00
_LOGGER.exception("Login error: %s", e)
errors["base"] = "login_failed"
2023-09-02 16:30:03 +08:00
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({
2023-09-17 19:40:54 +08:00
vol.Required(CONF_ACCOUNT): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_SERVER, default=2): vol.In(CONF_SERVERS)
2023-09-03 22:15:41 +08:00
}),
2025-09-12 00:15:14 +08:00
errors=errors,
2023-09-03 22:15:41 +08:00
)
2023-09-02 16:30:03 +08:00
class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry):
2023-09-17 19:40:54 +08:00
self._config_entry = config_entry
async def async_step_init(self, user_input=None, error=None):
2025-09-12 00:15:14 +08:00
# 账号型条目不支持配置项
return self.async_abort(reason="account_unsupport_config")
# 不再提供任何可配置项
return self.async_abort(reason="account_unsupport_config")
# 不提供 reset/configure 等选项步骤