This commit is contained in:
KNOOP 2024-12-24 02:27:01 +05:00 committed by GitHub
commit c6eeaf49e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 11 deletions

View File

@ -50,7 +50,7 @@ import hashlib
import json import json
import secrets import secrets
import traceback import traceback
from typing import Optional from typing import Any, Optional
from aiohttp import web from aiohttp import web
from aiohttp.hdrs import METH_GET from aiohttp.hdrs import METH_GET
import voluptuous as vol import voluptuous as vol
@ -59,6 +59,8 @@ import logging
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components import zeroconf from homeassistant.components import zeroconf
from homeassistant.components.zeroconf import HaAsyncZeroconf 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 ( from homeassistant.components.webhook import (
async_register as webhook_async_register, async_register as webhook_async_register,
async_unregister as webhook_async_unregister, async_unregister as webhook_async_unregister,
@ -68,6 +70,7 @@ from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow from homeassistant.data_entry_flow import AbortFlow
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from .miot.const import ( from .miot.const import (
DEFAULT_CLOUD_SERVER, DEFAULT_CLOUD_SERVER,
DEFAULT_CTRL_MODE, DEFAULT_CTRL_MODE,
@ -383,6 +386,7 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# TASK 4: Abort if unique_id configured # TASK 4: Abort if unique_id configured
# Each MiHome account can only configure one instance # Each MiHome account can only configure one instance
await self.async_set_unique_id(f'{self._cloud_server}{self._uid}') await self.async_set_unique_id(f'{self._cloud_server}{self._uid}')
if self.source not in (SOURCE_REAUTH, SOURCE_RECONFIGURE):
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
# TASK 5: Query mdns info # TASK 5: Query mdns info
@ -525,6 +529,22 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
'auth_info': self._auth_info 'auth_info': self._auth_info
})): })):
raise MIoTError('miot_storage.update_user_config_async error') 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( return self.async_create_entry(
title=( title=(
f'{self._nick_name}: {self._uid} ' f'{self._nick_name}: {self._uid} '
@ -575,6 +595,25 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
}, },
last_step=False, 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()
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 @ staticmethod
@ callback @ callback
@ -584,6 +623,9 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return OptionsFlowHandler(config_entry) return OptionsFlowHandler(config_entry)
class OptionsFlowHandler(config_entries.OptionsFlow): class OptionsFlowHandler(config_entries.OptionsFlow):
"""Xiaomi MiHome options flow.""" """Xiaomi MiHome options flow."""
# pylint: disable=unused-argument # pylint: disable=unused-argument

View File

@ -51,7 +51,9 @@
"network_connect_error": "Configuration failed. The network connection is abnormal. Please check the equipment network configuration.", "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.", "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.", "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": { "options": {

View File

@ -51,7 +51,9 @@
"network_connect_error": "配置失败。网络连接异常,请检查设备网络配置。", "network_connect_error": "配置失败。网络连接异常,请检查设备网络配置。",
"already_configured": "该用户已配置完成。请进入集成页面,点击“配置”按钮修改配置。", "already_configured": "该用户已配置完成。请进入集成页面,点击“配置”按钮修改配置。",
"invalid_auth_info": "认证信息已过期。请进入集成页面,点击“配置”按钮重新认证。", "invalid_auth_info": "认证信息已过期。请进入集成页面,点击“配置”按钮重新认证。",
"config_flow_error": "集成配置错误:{error}" "config_flow_error": "集成配置错误:{error}",
"reauth_failed": "重新授权失败",
"reconfigure_successful": "重新配置成功"
} }
}, },
"options": { "options": {

View File

@ -51,7 +51,9 @@
"network_connect_error": "配置失敗。網絡連接異常,請檢查設備網絡配置。", "network_connect_error": "配置失敗。網絡連接異常,請檢查設備網絡配置。",
"already_configured": "該用戶已配置完成。請進入集成頁面,點擊“配置”按鈕修改配置。", "already_configured": "該用戶已配置完成。請進入集成頁面,點擊“配置”按鈕修改配置。",
"invalid_auth_info": "認證信息已過期。請進入集成頁面,點擊“配置”按鈕重新認證。", "invalid_auth_info": "認證信息已過期。請進入集成頁面,點擊“配置”按鈕重新認證。",
"config_flow_error": "集成配置錯誤:{error}" "config_flow_error": "集成配置錯誤:{error}",
"reauth_failed": "重新授權失敗",
"reconfigure_successful": "重新配置成功"
} }
}, },
"options": { "options": {