diff --git a/custom_components/xiaomi_home/config_flow.py b/custom_components/xiaomi_home/config_flow.py index 36f9815..a47bd6c 100644 --- a/custom_components/xiaomi_home/config_flow.py +++ b/custom_components/xiaomi_home/config_flow.py @@ -50,7 +50,7 @@ import hashlib import json import secrets import traceback -from typing import Optional +from typing import Any, Optional from aiohttp import web from aiohttp.hdrs import METH_GET import voluptuous as vol @@ -59,6 +59,8 @@ import logging from homeassistant import config_entries from homeassistant.components import zeroconf from homeassistant.components.zeroconf import HaAsyncZeroconf +from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE +from homeassistant.config_entries import FlowResult from homeassistant.components.webhook import ( async_register as webhook_async_register, async_unregister as webhook_async_unregister, @@ -68,6 +70,7 @@ from homeassistant.core import callback from homeassistant.data_entry_flow import AbortFlow import homeassistant.helpers.config_validation as cv + from .miot.const import ( DEFAULT_CLOUD_SERVER, DEFAULT_CTRL_MODE, @@ -383,8 +386,9 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # TASK 4: Abort if unique_id configured # Each MiHome account can only configure one instance await self.async_set_unique_id(f'{self._cloud_server}{self._uid}') - self._abort_if_unique_id_configured() - + if self.source not in (SOURCE_REAUTH, SOURCE_RECONFIGURE): + self._abort_if_unique_id_configured() + # TASK 5: Query mdns info mips_list = None if self._cloud_server in SUPPORT_CENTRAL_GATEWAY_CTRL: @@ -525,6 +529,22 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): 'auth_info': self._auth_info })): raise MIoTError('miot_storage.update_user_config_async error') + + if self.source in (SOURCE_REAUTH, SOURCE_RECONFIGURE): + entry = self._get_reauth_entry() if self.source == SOURCE_REAUTH else self._get_reconfigure_entry() + if user_input: + data_updates = { + "ctrl_mode": self._ctrl_mode, + "home_selected": self._home_selected, + "area_name_rule": self._area_name_rule, + "action_debug": self._action_debug, + "hide_non_standard_entities": self._hide_non_standard_entities, + } + devices_list_sort = dict(sorted(devices_list.items(), key=lambda item: item[1].get('home_id', '') + item[1].get('room_id', ''))) + if not await self._miot_storage.save_async(domain='miot_devices', name=f'{self._uid}_{self._cloud_server}', data=devices_list_sort): + return self.async_abort(reason="storage_error") + return self.async_update_reload_and_abort(entry=entry, data_updates=data_updates, reason="reconfigure_successful") + return self.async_create_entry( title=( f'{self._nick_name}: {self._uid} ' @@ -575,15 +595,37 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): }, last_step=False, ) + async def async_step_reauth(self, entry_data: dict[str, Any]) -> FlowResult: + """Handle reauthorization request.""" + entry = self._get_reauth_entry() + self._virtual_did = entry.data["virtual_did"] + self._uid = entry.data["uid"] + self._storage_path = entry.data["storage_path"] + self._cloud_server = entry.data["cloud_server"] + self._integration_language = entry.data["integration_language"] + + await self.async_set_unique_id(f'{self._cloud_server}{self._uid}') + self._abort_if_unique_id_mismatch() - @staticmethod - @callback + return await self.async_step_auth_config() + + async def async_step_reconfigure(self, entry_data: dict[str, Any]) -> FlowResult: + """Handle a reconfigure flow.""" + # For reconfigure, just start from the beginning like a new setup + return await self.async_step_user() + + + @ staticmethod + @ callback def async_get_options_flow( config_entry: config_entries.ConfigEntry, ) -> config_entries.OptionsFlow: return OptionsFlowHandler(config_entry) + + + class OptionsFlowHandler(config_entries.OptionsFlow): """Xiaomi MiHome options flow.""" # pylint: disable=unused-argument diff --git a/custom_components/xiaomi_home/translations/en.json b/custom_components/xiaomi_home/translations/en.json index ddb424a..0412e02 100644 --- a/custom_components/xiaomi_home/translations/en.json +++ b/custom_components/xiaomi_home/translations/en.json @@ -51,7 +51,9 @@ "network_connect_error": "Configuration failed. The network connection is abnormal. Please check the equipment network configuration.", "already_configured": "Configuration for this user is already completed. Please go to the integration page and click the CONFIGURE button for modifications.", "invalid_auth_info": "Authentication information has expired. Please go to the integration page and click the CONFIGURE button to re-authenticate.", - "config_flow_error": "Integration configuration error: {error}." + "config_flow_error": "Integration configuration error: {error}.", + "reauth_failed": "Re-authorization failed", + "reconfigure_successful": "Reconfiguration successful" } }, "options": { @@ -141,4 +143,4 @@ "inconsistent_account": "Account information is inconsistent. Please login with the correct account." } } -} \ No newline at end of file +} diff --git a/custom_components/xiaomi_home/translations/zh-Hans.json b/custom_components/xiaomi_home/translations/zh-Hans.json index 37b905d..fa6eb52 100644 --- a/custom_components/xiaomi_home/translations/zh-Hans.json +++ b/custom_components/xiaomi_home/translations/zh-Hans.json @@ -51,7 +51,9 @@ "network_connect_error": "配置失败。网络连接异常,请检查设备网络配置。", "already_configured": "该用户已配置完成。请进入集成页面,点击“配置”按钮修改配置。", "invalid_auth_info": "认证信息已过期。请进入集成页面,点击“配置”按钮重新认证。", - "config_flow_error": "集成配置错误:{error}" + "config_flow_error": "集成配置错误:{error}", + "reauth_failed": "重新授权失败", + "reconfigure_successful": "重新配置成功" } }, "options": { @@ -141,4 +143,4 @@ "inconsistent_account": "账号信息不一致。请使用正确的账号登录。" } } -} \ No newline at end of file +} diff --git a/custom_components/xiaomi_home/translations/zh-Hant.json b/custom_components/xiaomi_home/translations/zh-Hant.json index c8551a0..03d849c 100644 --- a/custom_components/xiaomi_home/translations/zh-Hant.json +++ b/custom_components/xiaomi_home/translations/zh-Hant.json @@ -51,7 +51,9 @@ "network_connect_error": "配置失敗。網絡連接異常,請檢查設備網絡配置。", "already_configured": "該用戶已配置完成。請進入集成頁面,點擊“配置”按鈕修改配置。", "invalid_auth_info": "認證信息已過期。請進入集成頁面,點擊“配置”按鈕重新認證。", - "config_flow_error": "集成配置錯誤:{error}" + "config_flow_error": "集成配置錯誤:{error}", + "reauth_failed": "重新授權失敗", + "reconfigure_successful": "重新配置成功" } }, "options": { @@ -141,4 +143,4 @@ "inconsistent_account": "帳號信息不一致。請使用正確的帳號登錄。" } } -} \ No newline at end of file +}