Compare commits

...

16 Commits

Author SHA1 Message Date
80be82726c reduced polling interval
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 12:48:06 +02:00
b7e13b73a5 add polling for endpoint 23
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 12:39:23 +02:00
ca1b870b89 add debug info
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 12:25:40 +02:00
b81215415e try fix
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 12:17:42 +02:00
abb3c346de try fix
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 12:07:03 +02:00
c3f160919a ignore data for other devices
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 11:59:31 +02:00
85eed58a06 added support for multiple instances
Some checks failed
Validate / Validate (push) Has been cancelled
2026-05-31 11:36:30 +02:00
nius
1f122f2235 1. Fix the issue of duplicate entity names;
2. Fix the issue of some entities being unavailable;
2026-05-06 18:13:13 +08:00
nius
3b562b0371 1. Fix the issue of duplicate entity names;
2. Fix the issue of some entities being unavailable;
2026-03-27 18:50:25 +08:00
ht-it-lab
4dfe66b635 Update README.md
version change
2026-03-26 13:56:25 +08:00
nius
ffa46ebac0 1. Fix the issue of duplicate entity names;
2. Fix the issue of some entities being unavailable;
2026-03-12 16:48:12 +08:00
ht-it-lab
618bf71296 Update README.md 2026-03-06 17:15:39 +08:00
ht-it-lab
217b277dbe Delete docs directory 2026-03-06 16:25:39 +08:00
wongshan-m1
a1a1f9d2c0 docs: replace Mermaid diagrams with PNG images for GitLab compatibility
GitLab 11 does not support Mermaid rendering. Exported 4 diagrams
(architecture, auth flow, config flow, energy mapping) as PNG images
and updated PRD.md to reference them.

Made-with: Cursor
2026-03-05 13:56:59 +08:00
wongshan-m1
6b183d6f5d docs: update PRD to MQTT architecture, add acceptance criteria
- Replace HTTP/REST with MQTT throughout PRD
- Add MQTT Broker prerequisites section (§2.2)
- Convert architecture and auth flow diagrams to Mermaid
- Add comprehensive Home Energy Management adaptation (§6.2)
- Add product acceptance criteria document (78 items)

Made-with: Cursor
2026-03-05 11:48:12 +08:00
wongshan-m1
9b4cf798ef docs: add PRD prompt and product requirements document
Add complete product requirements document (PRD) for Jackery DIY3
Home Assistant integration, covering architecture, entity mapping,
authorization flow, and future roadmap.

Made-with: Cursor
2026-03-04 20:10:01 +08:00
11 changed files with 776 additions and 1312 deletions

View File

@@ -1,8 +1,8 @@
## Jackery Home Assistant Energy Monitoring Integration ## Jackery Home Assistant Energy Monitoring Integration
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg)](https://github.com/hacs/integration) [![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg)](https://github.com/hacs/integration)
[![GitHub Release](https://img.shields.io/github/release/suyulin/jackery.svg)](https://github.com/suyulin/jackery/releases) [![GitHub Release](https://img.shields.io/github/release/ht-it-lab/jackery.svg)](https://github.com/ht-it-lab/jackery/releases)
[![License](https://img.shields.io/github/license/suyulin/jackery.svg)](LICENSE) [![License](https://img.shields.io/github/license/ht-it-lab/jackery.svg)](LICENSE)
> **⚠️ Beta Stage**: This integration is currently in Beta testing phase and may be unstable. Please use with caution and report any issues. > **⚠️ Beta Stage**: This integration is currently in Beta testing phase and may be unstable. Please use with caution and report any issues.
@@ -36,9 +36,9 @@ Before the Jackery integration can receive any data, **two things must be in pla
2. **Device is configured from the Jackery app** 2. **Device is configured from the Jackery app**
- Use the vendor/Jackery mobile app to add the device/gateway and complete its initial setup. - Use the vendor/Jackery mobile app to add the device/gateway and complete its initial setup.
- **⚠️ APP Version Requirement**: Jackery APP version must be greater than **2.10.18** to support this integration. - **⚠️ APP Version Requirement**: Jackery APP version must be greater than **2.0.0** to support this integration.
- Make sure the device has network access and is configured so that it can connect to your MQTT/cloud backend. - Make sure the device has network access and is configured so that it can connect to your MQTT/cloud backend.
- In the Jackery app, long-press the app logo to open the configuration screen. - Go to Device Details Page > Settings > MQTT in the Jackery app to open the configuration page.
- In the Jackery app configuration, **replace the IP with the address of your own MQTT server**. - In the Jackery app configuration, **replace the IP with the address of your own MQTT server**.
![jackery_config](./img/app_config_mqtt.png) ![jackery_config](./img/app_config_mqtt.png)
@@ -52,24 +52,18 @@ Before the Jackery integration can receive any data, **two things must be in pla
- Open HACS in Home Assistant - Open HACS in Home Assistant
- Click the three dots in the top-right → **Custom repositories** - Click the three dots in the top-right → **Custom repositories**
- Add repository URL: `https://github.com/suyulin/jackery` - Add repository URL: `https://github.com/ht-it-lab/jackery`
- Category: `Integration` - Category: `Integration`
- Click **Add** - Click **Add**
2. **Install the integration** 2. **Configure the integration**
- In HACS, search for **"Jackery"**
- Click **Install**
- Restart Home Assistant
3. **Configure the integration**
- Go to **Settings → Devices & Services → Add Integration** - Go to **Settings → Devices & Services → Add Integration**
- Search for **"Jackery"** - Search for **"Jackery"**
- **Enter your Token** (Required for authentication) - **Enter your Token** (Required for authentication)
- You can find this token in your Jackery app settings or device documentation. - You can find this token in your Jackery app settings or device documentation.
- Enter an MQTT topic prefix if needed (default: `hb`) - Enter an MQTT topic prefix if needed (default: `hb`)
- Submit to finish configuration - Submit to finish configuration
![config](./img/jackery_home_add.png) ![config](./img/jackery_home_add.png)
![config](./img/jackery_home_config.png) ![config](./img/jackery_home_config.png)
> **Requirement**: The built-in **MQTT integration** must be configured and connected to your MQTT broker **before** Jackery will work. > **Requirement**: The built-in **MQTT integration** must be configured and connected to your MQTT broker **before** Jackery will work.
### Example: Energy Flow Card Plus ### Example: Energy Flow Card Plus

View File

@@ -1,9 +1,9 @@
"""Energy Monitor MQTT Integration for Home Assistant.""" """Jackery Home Assistant Integration."""
import logging import logging
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.components import mqtt from homeassistant.components import mqtt
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -13,48 +13,42 @@ PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Jackery from a config entry.""" """Set up one Jackery device from a config entry."""
_LOGGER.info("Setting up Jackery integration")
# 检查 MQTT 集成是否已配置和可用
if not await mqtt.async_wait_for_mqtt_client(hass): if not await mqtt.async_wait_for_mqtt_client(hass):
_LOGGER.error( _LOGGER.error("MQTT integration is not available")
"MQTT integration is not available or not configured. "
"Please set up the MQTT integration first: "
"Settings -> Devices & Services -> Add Integration -> MQTT"
)
return False return False
_LOGGER.info("MQTT integration is available and ready") from .sensor import JackeryCoordinator
# 初始化存储结构 config = entry.data
coordinator = JackeryCoordinator(
hass=hass,
entry_id=entry.entry_id,
device_sn=config["device_sn"],
token=config.get("token", ""),
topic_prefix=config.get("topic_prefix", "hb"),
)
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = { hass.data[DOMAIN][entry.entry_id] = coordinator
"config": entry.data,
"coordinator": None, # 将在 sensor.py 中设置 # Start MQTT subscriptions now; the poll loop waits 2 s so platform
} # async_setup_entry callbacks are registered before the first poll fires.
await coordinator.async_start()
# 加载传感器平台
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
_LOGGER.info("Unloading Jackery integration") coordinator = hass.data[DOMAIN].get(entry.entry_id)
# 停止协调器
entry_data = hass.data[DOMAIN].get(entry.entry_id, {})
coordinator = entry_data.get("coordinator")
if coordinator: if coordinator:
await coordinator.async_stop() await coordinator.async_stop()
_LOGGER.info("Coordinator stopped")
# 卸载传感器平台
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok: if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id, None)
return unload_ok return unload_ok

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

@@ -2,14 +2,14 @@
"domain": "jackery", "domain": "jackery",
"name": "Jackery", "name": "Jackery",
"codeowners": [ "codeowners": [
"@suyulin" "@ht-it-lab"
], ],
"config_flow": true, "config_flow": true,
"dependencies": [ "dependencies": [
"mqtt" "mqtt"
], ],
"documentation": "https://github.com/suyulin/jackery", "documentation": "https://github.com/ht-it-lab/jackery",
"issue_tracker": "https://github.com/suyulin/jackery/issues", "issue_tracker": "https://github.com/ht-it-lab/jackery/issues",
"iot_class": "local_push", "iot_class": "local_push",
"version": "1.1.62" "version": "1.1.62"
} }

View File

@@ -1,6 +1,6 @@
"""Jackery Number Platform.""" """Jackery Number Platform."""
import logging import logging
from typing import Any, TYPE_CHECKING from typing import TYPE_CHECKING
from homeassistant.components.number import NumberEntity, NumberMode from homeassistant.components.number import NumberEntity, NumberMode
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@@ -10,16 +10,15 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN from . import DOMAIN
if TYPE_CHECKING: if TYPE_CHECKING:
from .sensor import JackeryDataCoordinator from .sensor import JackeryCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
NUMBERS = { NUMBERS = {
"socChgLimit": {"name": "SOC Charge Limit", "min": 0, "max": 100, "step": 1}, "socChgLimit": {"name": "SOC Charge Limit", "min": 0, "max": 100, "step": 1},
"socDischgLimit": {"name": "SOC Discharge Limit", "min": 0, "max": 100, "step": 1}, "socDischgLimit": {"name": "SOC Discharge Limit", "min": 0, "max": 100, "step": 1},
"maxOutPw": {"name": "Max Output Power (OnGrid)", "min": 0, "max": 10000, "step": 10}, "maxOutPw": {"name": "Max Output Power (OnGrid)", "min": 0, "max": 10000, "step": 10},
"autoStandby": {"name": "Auto Standby Mode", "min": 0, "max": 2, "step": 1}, "autoStandby": {"name": "Auto Standby Mode", "min": 0, "max": 2, "step": 1},
} }
@@ -28,74 +27,53 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Jackery number entities.""" coordinator: "JackeryCoordinator" = hass.data[DOMAIN][config_entry.entry_id]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
if coordinator is None:
_LOGGER.warning("Coordinator not ready for numbers")
return
entities = [] async_add_entities([
for key, cfg in NUMBERS.items(): JackeryMainNumber(key=key, coordinator=coordinator, **cfg)
entities.append( for key, cfg in NUMBERS.items()
JackeryMainNumber( ])
key=key,
name=cfg["name"],
min_value=cfg["min"],
max_value=cfg["max"],
step=cfg["step"],
coordinator=coordinator,
config_entry_id=config_entry.entry_id,
)
)
if entities:
async_add_entities(entities)
class JackeryMainNumber(NumberEntity): class JackeryMainNumber(NumberEntity):
"""Main device number (cmd=5).""" """Numeric setting on the main Jackery device."""
_attr_should_poll = False
_attr_has_entity_name = True
_attr_mode = NumberMode.SLIDER
def __init__( def __init__(
self, self,
key: str, key: str,
name: str, name: str,
min_value: float, min: float,
max_value: float, max: float,
step: float, step: float,
coordinator: "JackeryDataCoordinator", coordinator: "JackeryCoordinator",
config_entry_id: str,
) -> None: ) -> None:
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_{coordinator.entry_id}_main_{key}"
self._attr_has_entity_name = True self._attr_native_min_value = min
self._attr_mode = NumberMode.SLIDER self._attr_native_max_value = max
self._attr_native_min_value = min_value
self._attr_native_max_value = max_value
self._attr_native_step = step self._attr_native_step = step
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, config_entry_id)}, "identifiers": {(DOMAIN, coordinator.entry_id)},
"name": "Jackery", "name": f"Jackery {coordinator.device_sn}",
"manufacturer": "Jackery", "manufacturer": "Jackery",
"model": "Energy Monitor", "model": "Energy Monitor",
} }
@property
def should_poll(self) -> bool:
return False
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
await super().async_added_to_hass() await super().async_added_to_hass()
self._coordinator.register_sensor(f"main_number_{self._key}", self) self._coordinator.register_entity(f"main_number_{self._key}", self)
async def async_will_remove_from_hass(self) -> None: async def async_will_remove_from_hass(self) -> None:
self._coordinator.unregister_sensor(f"main_number_{self._key}") self._coordinator.unregister_entity(f"main_number_{self._key}")
await super().async_will_remove_from_hass() await super().async_will_remove_from_hass()
def _update_from_coordinator(self, data: dict) -> None: def _update_from_coordinator(self, data: dict) -> None:
if self._key not in data:
return
val = data.get(self._key) val = data.get(self._key)
if val is None: if val is None:
return return
@@ -107,4 +85,4 @@ class JackeryMainNumber(NumberEntity):
pass pass
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
await self._coordinator.async_control_main_device({self._key: int(value)}) await self._coordinator.control_main({self._key: int(value)})

File diff suppressed because it is too large Load Diff

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

@@ -10,7 +10,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN from . import DOMAIN
if TYPE_CHECKING: if TYPE_CHECKING:
from .sensor import JackeryDataCoordinator from .sensor import JackeryCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -20,177 +20,43 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Jackery switches.""" coordinator: "JackeryCoordinator" = hass.data[DOMAIN][config_entry.entry_id]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
if coordinator is None:
_LOGGER.warning("Coordinator not ready for switches")
return
# Register callback for dynamic switch entities coordinator.add_switch_entities = async_add_entities
def add_switch_entities_callback(new_entities):
async_add_entities(new_entities)
coordinator.add_switch_entities_callback = add_switch_entities_callback
entities = [] async_add_entities([
JackeryMainSwitch("isAutoStandby", "Auto Standby Allowed", coordinator),
# Main device switches JackeryMainSwitch("swEps", "EPS Switch", coordinator),
entities.extend( ])
[
JackeryMainSwitch(
key="isAutoStandby",
name="Auto Standby Allowed",
coordinator=coordinator,
config_entry_id=config_entry.entry_id,
),
JackeryMainSwitch(
key="swEps",
name="EPS Switch",
coordinator=coordinator,
config_entry_id=config_entry.entry_id,
),
]
)
# Add any existing sub-devices as switches (non-CT)
for item in coordinator.get_subdevices():
sn = item.get("deviceSn") or item.get("sn")
dev_type = item.get("devType")
if dev_type is None and item.get("subType") == 2:
dev_type = 2
if sn and dev_type != 2:
entities.append(
JackeryPlugSwitch(
plug_sn=sn,
dev_type=dev_type,
coordinator=coordinator,
config_entry_id=config_entry.entry_id,
)
)
if entities:
async_add_entities(entities)
class JackeryPlugSwitch(SwitchEntity):
"""Jackery Smart Plug Switch."""
def __init__(
self,
plug_sn: str,
dev_type: int,
coordinator: "JackeryDataCoordinator",
config_entry_id: str,
) -> None:
"""Initialize."""
self._plug_sn = plug_sn
self._dev_type = dev_type
self._coordinator = coordinator
self._raw_data = {}
self._attr_name = "Switch"
self._attr_unique_id = f"jackery_plug_{plug_sn}_switch"
self._attr_has_entity_name = True
self._attr_device_info = {
"identifiers": {(DOMAIN, f"sub_{plug_sn}")},
"via_device": (DOMAIN, config_entry_id),
"name": f"Jackery Plug {plug_sn}",
"manufacturer": "Jackery",
"model": f"Sub-device Type {dev_type}",
}
@property
def should_poll(self) -> bool:
return False
async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
self._coordinator.register_sensor(f"plug_switch_{self._plug_sn}", self)
async def async_will_remove_from_hass(self) -> None:
self._coordinator.unregister_sensor(f"plug_switch_{self._plug_sn}")
await super().async_will_remove_from_hass()
def _update_from_coordinator(self, data: dict) -> None:
plugs = data.get("plugs") or data.get("plug")
if not plugs or not isinstance(plugs, list):
return
my_plug = next((p for p in plugs if (p.get("sn") == self._plug_sn or p.get("deviceSn") == self._plug_sn)), None)
if not my_plug:
return
self._raw_data = dict(my_plug)
val = my_plug.get("sysSwitch")
if val is None:
val = my_plug.get("switchSta")
if val is None:
return
self._attr_is_on = bool(int(val))
self._attr_available = True
self.async_write_ha_state()
async def async_turn_on(self, **kwargs: Any) -> None:
await self._coordinator.async_control_subdevice_switch(
plug_sn=self._plug_sn,
dev_type=self._dev_type,
is_on=True,
)
async def async_turn_off(self, **kwargs: Any) -> None:
await self._coordinator.async_control_subdevice_switch(
plug_sn=self._plug_sn,
dev_type=self._dev_type,
is_on=False,
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
return {
"plug_sn": self._plug_sn,
"dev_type": self._dev_type,
"raw_data": self._raw_data,
}
class JackeryMainSwitch(SwitchEntity): class JackeryMainSwitch(SwitchEntity):
"""Main device switch (cmd=5).""" """On/off switch for a main-device boolean setting."""
def __init__( _attr_should_poll = False
self, _attr_has_entity_name = True
key: str,
name: str, def __init__(self, key: str, name: str, coordinator: "JackeryCoordinator") -> None:
coordinator: "JackeryDataCoordinator",
config_entry_id: str,
) -> None:
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_{coordinator.entry_id}_main_{key}"
self._attr_has_entity_name = True
self._attr_device_info = { self._attr_device_info = {
"identifiers": {(DOMAIN, config_entry_id)}, "identifiers": {(DOMAIN, coordinator.entry_id)},
"name": "Jackery", "name": f"Jackery {coordinator.device_sn}",
"manufacturer": "Jackery", "manufacturer": "Jackery",
"model": "Energy Monitor", "model": "Energy Monitor",
} }
@property
def should_poll(self) -> bool:
return False
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
await super().async_added_to_hass() await super().async_added_to_hass()
self._coordinator.register_sensor(f"main_switch_{self._key}", self) self._coordinator.register_entity(f"main_switch_{self._key}", self)
async def async_will_remove_from_hass(self) -> None: async def async_will_remove_from_hass(self) -> None:
self._coordinator.unregister_sensor(f"main_switch_{self._key}") self._coordinator.unregister_entity(f"main_switch_{self._key}")
await super().async_will_remove_from_hass() await super().async_will_remove_from_hass()
def _update_from_coordinator(self, data: dict) -> None: def _update_from_coordinator(self, data: dict) -> None:
if self._key not in data:
return
val = data.get(self._key) val = data.get(self._key)
if val is None: if val is None:
return return
@@ -199,7 +65,77 @@ class JackeryMainSwitch(SwitchEntity):
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
await self._coordinator.async_control_main_device({self._key: 1}) await self._coordinator.control_main({self._key: 1})
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
await self._coordinator.async_control_main_device({self._key: 0}) await self._coordinator.control_main({self._key: 0})
class JackeryPlugSwitch(SwitchEntity):
"""On/off switch for a smart plug sub-device."""
_attr_should_poll = False
_attr_has_entity_name = True
def __init__(
self,
plug_sn: str,
dev_type: int,
coordinator: "JackeryCoordinator",
entry_id: str,
) -> None:
self._plug_sn = plug_sn
self._dev_type = dev_type
self._coordinator = coordinator
self._raw_data: dict = {}
self._attr_name = "Switch"
self._attr_unique_id = f"jackery_plug_{plug_sn}_switch"
self._attr_device_info = {
"identifiers": {(DOMAIN, f"sub_{plug_sn}")},
"via_device": (DOMAIN, entry_id),
"name": f"Jackery Plug {plug_sn}",
"manufacturer": "Jackery",
"model": f"Sub-device Type {dev_type}",
}
async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
self._coordinator.register_entity(f"plug_switch_{self._plug_sn}", self)
async def async_will_remove_from_hass(self) -> None:
self._coordinator.unregister_entity(f"plug_switch_{self._plug_sn}")
await super().async_will_remove_from_hass()
def _update_from_coordinator(self, data: dict) -> None:
pool = data.get("plugs") or data.get("plug")
if not isinstance(pool, list):
return
my = next(
(p for p in pool
if p.get("sn") == self._plug_sn or p.get("deviceSn") == self._plug_sn),
None,
)
if not my:
return
self._raw_data = dict(my)
val = my.get("sysSwitch") if my.get("sysSwitch") is not None else my.get("switchSta")
if val is None:
return
self._attr_is_on = bool(int(val))
self._attr_available = True
self.async_write_ha_state()
async def async_turn_on(self, **kwargs: Any) -> None:
await self._coordinator.control_subdevice(self._plug_sn, self._dev_type, True)
async def async_turn_off(self, **kwargs: Any) -> None:
await self._coordinator.control_subdevice(self._plug_sn, self._dev_type, False)
@property
def extra_state_attributes(self) -> dict:
return {
"plug_sn": self._plug_sn,
"dev_type": self._dev_type,
"raw_data": self._raw_data,
}

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": "只允许一个此集成的实例"
} }
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -167,7 +167,7 @@ fi
if [ "$RELEASE_CREATED" = false ]; then if [ "$RELEASE_CREATED" = false ]; then
echo "📋 下一步操作 (手动发布):" echo "📋 下一步操作 (手动发布):"
echo "1. 访问 GitHub 创建 Release:" echo "1. 访问 GitHub 创建 Release:"
echo " https://github.com/suyulin/jackery/releases/new?tag=$TAG_NAME" echo " https://github.com/ht-it-lab/jackery/releases/new?tag=$TAG_NAME"
echo "" echo ""
echo "2. 如果尚未安装,推荐安装 GitHub CLI (gh) 以便下次自动发布。" echo "2. 如果尚未安装,推荐安装 GitHub CLI (gh) 以便下次自动发布。"
fi fi