Compare commits
16 Commits
26bc49a202
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 80be82726c | |||
| b7e13b73a5 | |||
| ca1b870b89 | |||
| b81215415e | |||
| abb3c346de | |||
| c3f160919a | |||
| 85eed58a06 | |||
|
|
1f122f2235 | ||
|
|
3b562b0371 | ||
|
|
4dfe66b635 | ||
|
|
ffa46ebac0 | ||
|
|
618bf71296 | ||
|
|
217b277dbe | ||
|
|
a1a1f9d2c0 | ||
|
|
6b183d6f5d | ||
|
|
9b4cf798ef |
18
README.md
18
README.md
@@ -1,8 +1,8 @@
|
|||||||
## Jackery – Home Assistant Energy Monitoring Integration
|
## Jackery – Home Assistant Energy Monitoring Integration
|
||||||
|
|
||||||
[](https://github.com/hacs/integration)
|
[](https://github.com/hacs/integration)
|
||||||
[](https://github.com/suyulin/jackery/releases)
|
[](https://github.com/ht-it-lab/jackery/releases)
|
||||||
[](LICENSE)
|
[](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**.
|
||||||

|

|
||||||
|
|
||||||
@@ -52,16 +52,10 @@ 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)
|
||||||
|
|||||||
@@ -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,28 +13,28 @@ 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
|
||||||
@@ -42,19 +42,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -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,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
}
|
}
|
||||||
@@ -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,11 +10,10 @@ 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},
|
||||||
@@ -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
@@ -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": "只允许一个此集成的实例"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 |
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user