Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds a reconfiguration option #238

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions custom_components/xiaomi_home/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
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
Expand All @@ -59,6 +59,8 @@
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,
Expand All @@ -68,6 +70,7 @@
from homeassistant.data_entry_flow import AbortFlow
import homeassistant.helpers.config_validation as cv


from .miot.const import (
DEFAULT_CLOUD_SERVER,
DEFAULT_CTRL_MODE,
Expand Down Expand Up @@ -374,8 +377,9 @@ async def __check_oauth_async(self) -> None:
# 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:
Expand Down Expand Up @@ -513,6 +517,22 @@ async def async_step_devices_filter(self, user_input=None):
'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} '
Expand Down Expand Up @@ -563,15 +583,37 @@ async def display_device_filter_form(self, reason: str):
},
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()

@staticmethod
@callback
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
Expand Down
6 changes: 4 additions & 2 deletions custom_components/xiaomi_home/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -141,4 +143,4 @@
"inconsistent_account": "Account information is inconsistent. Please login with the correct account."
}
}
}
}
6 changes: 4 additions & 2 deletions custom_components/xiaomi_home/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -141,4 +143,4 @@
"inconsistent_account": "账号信息不一致。请使用正确的账号登录。"
}
}
}
}
6 changes: 4 additions & 2 deletions custom_components/xiaomi_home/translations/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -141,4 +143,4 @@
"inconsistent_account": "帳號信息不一致。請使用正確的帳號登錄。"
}
}
}
}
Loading