From d87156d3e39707d061239af270c96421fb2a8fa5 Mon Sep 17 00:00:00 2001 From: sususweet Date: Sun, 28 Sep 2025 20:51:14 +0800 Subject: [PATCH] feat: add credentials change feature. Fix #6. --- .../midea_auto_cloud/config_flow.py | 65 +++++++++++++++++-- .../midea_auto_cloud/translations/en.json | 9 +++ .../translations/zh-Hans.json | 9 +++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/custom_components/midea_auto_cloud/config_flow.py b/custom_components/midea_auto_cloud/config_flow.py index d3a9e0c..9c9922a 100644 --- a/custom_components/midea_auto_cloud/config_flow.py +++ b/custom_components/midea_auto_cloud/config_flow.py @@ -71,8 +71,63 @@ class OptionsFlowHandler(config_entries.OptionsFlow): self._config_entry = config_entry async def async_step_init(self, user_input=None, error=None): - # 账号型条目不支持配置项 - return self.async_abort(reason="account_unsupport_config") - # 不再提供任何可配置项 - return self.async_abort(reason="account_unsupport_config") - # 不提供 reset/configure 等选项步骤 \ No newline at end of file + """初始化选项流程""" + if user_input is not None: + if user_input["option"] == "change_credentials": + return await self.async_step_change_credentials() + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema({ + vol.Required("option", default="change_credentials"): vol.In({ + "change_credentials": "修改账号密码", + "reset": "重置配置", + "configure": "设备配置" + }) + }), + errors=error + ) + + async def async_step_change_credentials(self, user_input=None, error=None): + """账号密码变更步骤""" + errors: dict[str, str] = {} + + if user_input is not None: + # 验证新密码 + cloud = get_midea_cloud( + session=async_create_clientsession(self.hass), + cloud_name=CONF_SERVERS[user_input[CONF_SERVER]], + account=user_input[CONF_ACCOUNT], + password=user_input[CONF_PASSWORD] + ) + try: + if await cloud.login(): + # 更新配置条目 + self.hass.config_entries.async_update_entry( + self._config_entry, + data={ + CONF_TYPE: CONF_ACCOUNT, + CONF_ACCOUNT: user_input[CONF_ACCOUNT], + CONF_PASSWORD: user_input[CONF_PASSWORD], + CONF_SERVER: user_input[CONF_SERVER] + } + ) + return self.async_create_entry(title="", data={}) + else: + errors["base"] = "login_failed" + except Exception as e: + _LOGGER.exception("Login error: %s", e) + errors["base"] = "login_failed" + + # 获取当前配置 + current_data = self._config_entry.data + + return self.async_show_form( + step_id="change_credentials", + data_schema=vol.Schema({ + vol.Required(CONF_ACCOUNT, default=current_data.get(CONF_ACCOUNT, "")): str, + vol.Required(CONF_PASSWORD, default=""): str, + vol.Required(CONF_SERVER, default=current_data.get(CONF_SERVER, 2)): vol.In(CONF_SERVERS) + }), + errors=errors, + ) diff --git a/custom_components/midea_auto_cloud/translations/en.json b/custom_components/midea_auto_cloud/translations/en.json index 0ce43d9..d6884b8 100644 --- a/custom_components/midea_auto_cloud/translations/en.json +++ b/custom_components/midea_auto_cloud/translations/en.json @@ -49,6 +49,15 @@ }, "title": "Configure" }, + "change_credentials": { + "title": "Change Credentials", + "description": "Change Midea cloud account's username and password", + "data": { + "account": "Username", + "password": "Password", + "server": "Server" + } + }, "reset":{ "title": "Reset the configuration of appliance", "description": "Remove the old configuration and make a new configuration use template\nIf your configuration was modified, the changes will lost\nIf your appliance type or model not in template, then the new configuration won't be made", diff --git a/custom_components/midea_auto_cloud/translations/zh-Hans.json b/custom_components/midea_auto_cloud/translations/zh-Hans.json index ddd2be0..57d6b3d 100644 --- a/custom_components/midea_auto_cloud/translations/zh-Hans.json +++ b/custom_components/midea_auto_cloud/translations/zh-Hans.json @@ -49,6 +49,15 @@ }, "title": "选项" }, + "change_credentials": { + "title": "修改账号密码", + "description": "修改美的云账号的用户名和密码", + "data": { + "account": "用户名", + "password": "密码", + "server": "服务器" + } + }, "reset":{ "title": "重置配置文件", "description": "移除已有的设备配置,并使用标准模板重新生成设备配置\n如果你的设备配置json文件进行过修改,重置之后修改将丢失\n如果标准模板中没有该设备类型,则不会生成设备配置",