From 4c63d2e5ac6837c47a4eb630c479fac32f082d02 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:27:01 +0800 Subject: [PATCH 1/9] Update config_flow.py --- custom_components/xiaomi_home/config_flow.py | 51 ++++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/custom_components/xiaomi_home/config_flow.py b/custom_components/xiaomi_home/config_flow.py index c1e4e67..d0ebd3f 100644 --- a/custom_components/xiaomi_home/config_flow.py +++ b/custom_components/xiaomi_home/config_flow.py @@ -50,6 +50,7 @@ import hashlib import json import secrets import traceback +from typing import Any from typing import Optional from aiohttp import web from aiohttp.hdrs import METH_GET @@ -59,6 +60,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 +71,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, @@ -374,8 +378,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: @@ -513,6 +518,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} ' @@ -563,15 +584,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 From e65aea145e095e76ef3ba6167a91be44370a9159 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:29:47 +0800 Subject: [PATCH 2/9] Update zh-Hans.json --- custom_components/xiaomi_home/translations/zh-Hans.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_home/translations/zh-Hans.json b/custom_components/xiaomi_home/translations/zh-Hans.json index 8326832..c0b2779 100644 --- a/custom_components/xiaomi_home/translations/zh-Hans.json +++ b/custom_components/xiaomi_home/translations/zh-Hans.json @@ -137,8 +137,10 @@ "network_connect_error": "配置失败。网络连接异常,请检查设备网络配置。", "options_flow_error": "集成重新配置错误:{error}", "re_add": "请重新添加集成,错误信息:{error}", + "reauth_failed": "重新授权失败", + "reconfigure_successful": "重新配置成功", "storage_error": "集成存储模块异常。请重试或者重新添加集成:{error}", "inconsistent_account": "账号信息不一致。请使用正确的账号登录。" } } -} \ No newline at end of file +} From d0a8007d36420d7348ff7330e0aad4690350f99f Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:30:37 +0800 Subject: [PATCH 3/9] Update zh-Hant.json --- custom_components/xiaomi_home/translations/zh-Hant.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_home/translations/zh-Hant.json b/custom_components/xiaomi_home/translations/zh-Hant.json index 04823c1..f3407a6 100644 --- a/custom_components/xiaomi_home/translations/zh-Hant.json +++ b/custom_components/xiaomi_home/translations/zh-Hant.json @@ -137,8 +137,10 @@ "network_connect_error": "配置失敗。網絡連接異常,請檢查設備網絡配置。", "options_flow_error": "集成重新配置錯誤:{error}", "re_add": "請重新添加集成,錯誤信息:{error}", + "reauth_failed": "重新授權失敗", + "reconfigure_successful": "重新配置成功", "storage_error": "集成存儲模塊異常。請重試或者重新添加集成:{error}", "inconsistent_account": "帳號信息不一致。請使用正確的帳號登錄。" } } -} \ No newline at end of file +} From c6707225ce0e1ba707deef4a280ce0dec44aa680 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:31:27 +0800 Subject: [PATCH 4/9] Update en.json --- custom_components/xiaomi_home/translations/en.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_home/translations/en.json b/custom_components/xiaomi_home/translations/en.json index 8fe89c5..f71327e 100644 --- a/custom_components/xiaomi_home/translations/en.json +++ b/custom_components/xiaomi_home/translations/en.json @@ -137,8 +137,10 @@ "network_connect_error": "Configuration failed. The network connection is abnormal. Please check the equipment network configuration.", "options_flow_error": "Integration re-configuration error: {error}", "re_add": "Please re-add the integration. Error message: {error}", + "reauth_failed": "Re-authorization failed", + "reconfigure_successful": "Reconfiguration successful", "storage_error": "Integration storage module exception. Please try again or re-add the integration: {error}", "inconsistent_account": "Account information is inconsistent. Please login with the correct account." } } -} \ No newline at end of file +} From fb83d0ab0c02cb50e97f4a91f6de36eb2f68d7f4 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:42:18 +0800 Subject: [PATCH 5/9] Update zh-Hans.json --- custom_components/xiaomi_home/translations/zh-Hans.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_home/translations/zh-Hans.json b/custom_components/xiaomi_home/translations/zh-Hans.json index c0b2779..8aaba82 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": { From daf7d2a4fea04f7cb3a684070ccf7906b8a716b8 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:43:17 +0800 Subject: [PATCH 6/9] Update zh-Hant.json --- custom_components/xiaomi_home/translations/zh-Hant.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/xiaomi_home/translations/zh-Hant.json b/custom_components/xiaomi_home/translations/zh-Hant.json index f3407a6..642b8c5 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": { @@ -137,8 +139,6 @@ "network_connect_error": "配置失敗。網絡連接異常,請檢查設備網絡配置。", "options_flow_error": "集成重新配置錯誤:{error}", "re_add": "請重新添加集成,錯誤信息:{error}", - "reauth_failed": "重新授權失敗", - "reconfigure_successful": "重新配置成功", "storage_error": "集成存儲模塊異常。請重試或者重新添加集成:{error}", "inconsistent_account": "帳號信息不一致。請使用正確的帳號登錄。" } From 5f86cdb28dc5f1815833e6477528fe498e800064 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 17:43:50 +0800 Subject: [PATCH 7/9] Update en.json --- custom_components/xiaomi_home/translations/en.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/xiaomi_home/translations/en.json b/custom_components/xiaomi_home/translations/en.json index f71327e..fbb1d07 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": { @@ -137,8 +139,6 @@ "network_connect_error": "Configuration failed. The network connection is abnormal. Please check the equipment network configuration.", "options_flow_error": "Integration re-configuration error: {error}", "re_add": "Please re-add the integration. Error message: {error}", - "reauth_failed": "Re-authorization failed", - "reconfigure_successful": "Reconfiguration successful", "storage_error": "Integration storage module exception. Please try again or re-add the integration: {error}", "inconsistent_account": "Account information is inconsistent. Please login with the correct account." } From fdd4da19f6fd29a69b509dfa04216852da70814a Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 18:28:08 +0800 Subject: [PATCH 8/9] Update zh-Hans.json --- custom_components/xiaomi_home/translations/zh-Hans.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom_components/xiaomi_home/translations/zh-Hans.json b/custom_components/xiaomi_home/translations/zh-Hans.json index 8aaba82..5213188 100644 --- a/custom_components/xiaomi_home/translations/zh-Hans.json +++ b/custom_components/xiaomi_home/translations/zh-Hans.json @@ -139,8 +139,6 @@ "network_connect_error": "配置失败。网络连接异常,请检查设备网络配置。", "options_flow_error": "集成重新配置错误:{error}", "re_add": "请重新添加集成,错误信息:{error}", - "reauth_failed": "重新授权失败", - "reconfigure_successful": "重新配置成功", "storage_error": "集成存储模块异常。请重试或者重新添加集成:{error}", "inconsistent_account": "账号信息不一致。请使用正确的账号登录。" } From d797f1faa74fa09a90de5cac1e58966d12163607 Mon Sep 17 00:00:00 2001 From: KNOOP Date: Thu, 19 Dec 2024 18:30:28 +0800 Subject: [PATCH 9/9] Update config_flow.py --- custom_components/xiaomi_home/config_flow.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/custom_components/xiaomi_home/config_flow.py b/custom_components/xiaomi_home/config_flow.py index d0ebd3f..42a25a6 100644 --- a/custom_components/xiaomi_home/config_flow.py +++ b/custom_components/xiaomi_home/config_flow.py @@ -50,8 +50,7 @@ import hashlib import json import secrets import traceback -from typing import Any -from typing import Optional +from typing import Any, Optional from aiohttp import web from aiohttp.hdrs import METH_GET import voluptuous as vol