feat: add credentials change feature. Fix #6.

This commit is contained in:
sususweet
2025-09-28 20:51:14 +08:00
parent f3246eb779
commit d87156d3e3
3 changed files with 78 additions and 5 deletions

View File

@@ -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 等选项步骤
"""初始化选项流程"""
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,
)

View File

@@ -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",

View File

@@ -49,6 +49,15 @@
},
"title": "选项"
},
"change_credentials": {
"title": "修改账号密码",
"description": "修改美的云账号的用户名和密码",
"data": {
"account": "用户名",
"password": "密码",
"server": "服务器"
}
},
"reset":{
"title": "重置配置文件",
"description": "移除已有的设备配置,并使用标准模板重新生成设备配置\n如果你的设备配置json文件进行过修改重置之后修改将丢失\n如果标准模板中没有该设备类型则不会生成设备配置",