added support for multiple instances
Some checks failed
Validate / Validate (push) Has been cancelled

This commit is contained in:
2026-05-31 11:36:30 +02:00
parent 1f122f2235
commit 85eed58a06
6 changed files with 22 additions and 20 deletions

View File

@@ -35,12 +35,15 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> FlowResult: ) -> FlowResult:
"""Handle the initial step.""" """Handle the initial step."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
errors = {} errors = {}
if user_input is not None: 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 集成是否已配置 # 检查 MQTT 集成是否已配置
if not await mqtt.async_wait_for_mqtt_client(self.hass): if not await mqtt.async_wait_for_mqtt_client(self.hass):
errors["base"] = "mqtt_not_configured" errors["base"] = "mqtt_not_configured"
@@ -52,7 +55,7 @@ class JackeryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) )
return self.async_create_entry( return self.async_create_entry(
title="Jackery", title=f"Jackery {new_sn}",
data=user_input, data=user_input,
) )

View File

@@ -68,15 +68,16 @@ class JackeryMainNumber(NumberEntity):
self._key = key self._key = key
self._coordinator = coordinator self._coordinator = coordinator
self._attr_name = name 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_has_entity_name = True
self._attr_mode = NumberMode.SLIDER self._attr_mode = NumberMode.SLIDER
self._attr_native_min_value = min_value self._attr_native_min_value = min_value
self._attr_native_max_value = max_value self._attr_native_max_value = max_value
self._attr_native_step = step self._attr_native_step = step
device_sn = coordinator._device_sn or "Unknown"
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, config_entry_id)}, "identifiers": {(DOMAIN, config_entry_id)},
"name": "Jackery", "name": f"Jackery {device_sn}",
"manufacturer": "Jackery", "manufacturer": "Jackery",
"model": "Energy Monitor", "model": "Energy Monitor",
} }

View File

@@ -1115,12 +1115,13 @@ class JackerySensor(SensorEntity):
self._attr_icon = self._config["icon"] self._attr_icon = self._config["icon"]
self._attr_device_class = self._config["device_class"] self._attr_device_class = self._config["device_class"]
self._attr_state_class = self._config["state_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 self._attr_has_entity_name = True
device_sn = coordinator._device_sn or "Unknown"
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, config_entry_id)}, "identifiers": {(DOMAIN, config_entry_id)},
"name": "Jackery", "name": f"Jackery {device_sn}",
"manufacturer": "Jackery", "manufacturer": "Jackery",
"model": "Energy Monitor", "model": "Energy Monitor",
} }

View File

@@ -13,13 +13,11 @@
} }
}, },
"error": { "error": {
"already_configured": "该集成已配置", "already_configured": "已配置相同序列号的设备",
"mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT", "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT"
"single_instance_allowed": "只允许一个此集成的实例"
}, },
"abort": { "abort": {
"already_configured": "该集成已配置", "already_configured": "已配置相同序列号的设备"
"single_instance_allowed": "只允许一个此集成的实例"
} }
} }
} }

View File

@@ -167,11 +167,12 @@ class JackeryMainSwitch(SwitchEntity):
self._key = key self._key = key
self._coordinator = coordinator self._coordinator = coordinator
self._attr_name = name 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_has_entity_name = True
device_sn = coordinator._device_sn or "Unknown"
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, config_entry_id)}, "identifiers": {(DOMAIN, config_entry_id)},
"name": "Jackery", "name": f"Jackery {device_sn}",
"manufacturer": "Jackery", "manufacturer": "Jackery",
"model": "Energy Monitor", "model": "Energy Monitor",
} }

View File

@@ -13,13 +13,11 @@
} }
}, },
"error": { "error": {
"already_configured": "该集成已配置", "already_configured": "已配置相同序列号的设备",
"mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT", "mqtt_not_configured": "MQTT 集成未配置或不可用。请先设置 MQTT 集成:设置 -> 设备与服务 -> 添加集成 -> MQTT"
"single_instance_allowed": "只允许一个此集成的实例"
}, },
"abort": { "abort": {
"already_configured": "该集成已配置", "already_configured": "已配置相同序列号的设备"
"single_instance_allowed": "只允许一个此集成的实例"
} }
} }
} }