From 96db87a722057d9446393e1309045af0ce741035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E6=B1=82=E5=9C=A3=E5=89=91?= Date: Tue, 14 Oct 2025 14:11:59 +0800 Subject: [PATCH] Enhance configuration flow to allow single instance only - Implemented check to abort configuration if an instance already exists - Updated error messages in strings.json to reflect new abort reason --- custom_components/JackeryHome/config_flow.py | 7 +++---- custom_components/JackeryHome/strings.json | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/JackeryHome/config_flow.py b/custom_components/JackeryHome/config_flow.py index e08951a..34d6e53 100644 --- a/custom_components/JackeryHome/config_flow.py +++ b/custom_components/JackeryHome/config_flow.py @@ -40,6 +40,9 @@ class JackeryHomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle the initial step.""" + if self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + errors = {} if user_input is not None: @@ -57,10 +60,6 @@ class JackeryHomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors["base"] = "invalid_port" if not errors: - # 检查是否已经配置 - await self.async_set_unique_id(DOMAIN) - self._abort_if_unique_id_configured() - _LOGGER.info(f"Creating JackeryHome config entry with topic_prefix: {user_input.get('topic_prefix', 'homeassistant/sensor')}") return self.async_create_entry( diff --git a/custom_components/JackeryHome/strings.json b/custom_components/JackeryHome/strings.json index 254356a..985badc 100644 --- a/custom_components/JackeryHome/strings.json +++ b/custom_components/JackeryHome/strings.json @@ -17,7 +17,8 @@ "invalid_port": "端口号必须在 1-65535 范围内" }, "abort": { - "already_configured": "该集成已配置" + "already_configured": "该集成已配置", + "single_instance_allowed": "该集成已配置" } } } \ No newline at end of file