try fix
Some checks failed
Validate / Validate (push) Has been cancelled

This commit is contained in:
2026-05-31 12:17:42 +02:00
parent abb3c346de
commit b81215415e
4 changed files with 739 additions and 1276 deletions

View File

@@ -1,9 +1,9 @@
"""Energy Monitor MQTT Integration for Home Assistant."""
"""Jackery Home Assistant Integration."""
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.components import mqtt
_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:
"""Set up Jackery from a config entry."""
_LOGGER.info("Setting up Jackery integration")
# 检查 MQTT 集成是否已配置和可用
"""Set up one Jackery device from a config entry."""
if not await mqtt.async_wait_for_mqtt_client(hass):
_LOGGER.error(
"MQTT integration is not available or not configured. "
"Please set up the MQTT integration first: "
"Settings -> Devices & Services -> Add Integration -> MQTT"
)
_LOGGER.error("MQTT integration is not available")
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[DOMAIN][entry.entry_id] = {
"config": entry.data,
"coordinator": None, # 将在 sensor.py 中设置
}
# 加载传感器平台
hass.data[DOMAIN][entry.entry_id] = coordinator
# 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)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.info("Unloading Jackery integration")
# 停止协调器
entry_data = hass.data[DOMAIN].get(entry.entry_id, {})
coordinator = entry_data.get("coordinator")
coordinator = hass.data[DOMAIN].get(entry.entry_id)
if coordinator:
await coordinator.async_stop()
_LOGGER.info("Coordinator stopped")
# 卸载传感器平台
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
hass.data[DOMAIN].pop(entry.entry_id, None)
return unload_ok