diff --git a/custom_components/jackery/config_flow.py b/custom_components/jackery/config_flow.py index ddc6ca3..3492e79 100644 --- a/custom_components/jackery/config_flow.py +++ b/custom_components/jackery/config_flow.py @@ -35,12 +35,15 @@ class JackeryConfigFlow(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: + # Check for duplicate device SN + new_sn = user_input.get("device_sn", "") + for entry in self._async_current_entries(): + if entry.data.get("device_sn") == new_sn: + return self.async_abort(reason="already_configured") + # 检查 MQTT 集成是否已配置 if not await mqtt.async_wait_for_mqtt_client(self.hass): errors["base"] = "mqtt_not_configured" @@ -52,7 +55,7 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) return self.async_create_entry( - title="Jackery", + title=f"Jackery {new_sn}", data=user_input, ) diff --git a/custom_components/jackery/number.py b/custom_components/jackery/number.py index 07510a3..0efead7 100644 --- a/custom_components/jackery/number.py +++ b/custom_components/jackery/number.py @@ -68,15 +68,16 @@ class JackeryMainNumber(NumberEntity): self._key = key self._coordinator = coordinator self._attr_name = name - self._attr_unique_id = f"jackery_main_{key}" + self._attr_unique_id = f"jackery_{config_entry_id}_main_{key}" self._attr_has_entity_name = True self._attr_mode = NumberMode.SLIDER self._attr_native_min_value = min_value self._attr_native_max_value = max_value self._attr_native_step = step + device_sn = coordinator._device_sn or "Unknown" self._attr_device_info = { "identifiers": {(DOMAIN, config_entry_id)}, - "name": "Jackery", + "name": f"Jackery {device_sn}", "manufacturer": "Jackery", "model": "Energy Monitor", } diff --git a/custom_components/jackery/sensor.py b/custom_components/jackery/sensor.py index c6adeb7..951fd46 100644 --- a/custom_components/jackery/sensor.py +++ b/custom_components/jackery/sensor.py @@ -1115,12 +1115,13 @@ class JackerySensor(SensorEntity): self._attr_icon = self._config["icon"] self._attr_device_class = self._config["device_class"] self._attr_state_class = self._config["state_class"] - self._attr_unique_id = f"jackery_{sensor_id}" + self._attr_unique_id = f"jackery_{config_entry_id}_{sensor_id}" self._attr_has_entity_name = True + device_sn = coordinator._device_sn or "Unknown" self._attr_device_info = { "identifiers": {(DOMAIN, config_entry_id)}, - "name": "Jackery", + "name": f"Jackery {device_sn}", "manufacturer": "Jackery", "model": "Energy Monitor", } diff --git a/custom_components/jackery/strings.json b/custom_components/jackery/strings.json index 9a105d2..1c68a05 100644 --- a/custom_components/jackery/strings.json +++ b/custom_components/jackery/strings.json @@ -13,13 +13,11 @@ } }, "error": { - "already_configured": "该集成已配置", - "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT", - "single_instance_allowed": "只允许一个此集成的实例" + "already_configured": "已配置相同序列号的设备", + "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT" }, "abort": { - "already_configured": "该集成已配置", - "single_instance_allowed": "只允许一个此集成的实例" + "already_configured": "已配置相同序列号的设备" } } } \ No newline at end of file diff --git a/custom_components/jackery/switch.py b/custom_components/jackery/switch.py index a9cca69..afbb4b3 100644 --- a/custom_components/jackery/switch.py +++ b/custom_components/jackery/switch.py @@ -167,11 +167,12 @@ class JackeryMainSwitch(SwitchEntity): self._key = key self._coordinator = coordinator self._attr_name = name - self._attr_unique_id = f"jackery_main_{key}" + self._attr_unique_id = f"jackery_{config_entry_id}_main_{key}" self._attr_has_entity_name = True + device_sn = coordinator._device_sn or "Unknown" self._attr_device_info = { "identifiers": {(DOMAIN, config_entry_id)}, - "name": "Jackery", + "name": f"Jackery {device_sn}", "manufacturer": "Jackery", "model": "Energy Monitor", } diff --git a/custom_components/jackery/translations/zh-Hans.json b/custom_components/jackery/translations/zh-Hans.json index 8939c7e..da05d53 100644 --- a/custom_components/jackery/translations/zh-Hans.json +++ b/custom_components/jackery/translations/zh-Hans.json @@ -13,13 +13,11 @@ } }, "error": { - "already_configured": "该集成已配置", - "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT", - "single_instance_allowed": "只允许一个此集成的实例" + "already_configured": "已配置相同序列号的设备", + "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT" }, "abort": { - "already_configured": "该集成已配置", - "single_instance_allowed": "只允许一个此集成的实例" + "already_configured": "已配置相同序列号的设备" } } }