- Introduced a new `JackeryDataCoordinator` class to manage MQTT subscriptions and data retrieval for all sensor entities. - Updated the setup process to create and start the coordinator, allowing shared data handling among sensors. - Enhanced sensor lifecycle management by registering and unregistering sensors with the coordinator. - Improved logging for better visibility into the coordinator's operations and sensor updates.
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""Energy Monitor MQTT Integration for Home Assistant."""
|
|
import logging
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.const import Platform
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DOMAIN = "jackery_home"
|
|
PLATFORMS = [Platform.SENSOR]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up JackeryHome from a config entry."""
|
|
_LOGGER.info("Setting up JackeryHome integration")
|
|
|
|
# 初始化存储结构
|
|
hass.data.setdefault(DOMAIN, {})
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
"config": entry.data,
|
|
"coordinator": None, # 将在 sensor.py 中设置
|
|
}
|
|
|
|
# 加载传感器平台
|
|
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 JackeryHome integration")
|
|
|
|
# 停止协调器
|
|
entry_data = hass.data[DOMAIN].get(entry.entry_id, {})
|
|
coordinator = entry_data.get("coordinator")
|
|
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)
|
|
|
|
return unload_ok
|
|
|