forked from HomeAssistant/fn_nas
修复飞牛nas关机后重启home assistant后,飞牛nas开机后部分实体显示不可用的问题
去除ssh连接数限制和缓存清理时间
This commit is contained in:
@@ -15,63 +15,38 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
config = {**entry.data, **entry.options}
|
config = {**entry.data, **entry.options}
|
||||||
|
|
||||||
|
|
||||||
coordinator = FlynasCoordinator(hass, config, entry)
|
coordinator = FlynasCoordinator(hass, config, entry)
|
||||||
|
# 直接初始化,不阻塞等待NAS上线
|
||||||
setup_task = hass.async_create_task(
|
hass.data.setdefault(DOMAIN, {})
|
||||||
async_delayed_setup(hass, entry, coordinator),
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
f"fn_nas_setup_{entry.entry_id}"
|
DATA_UPDATE_COORDINATOR: coordinator,
|
||||||
)
|
"ups_coordinator": None,
|
||||||
|
CONF_ENABLE_DOCKER: coordinator.config.get(CONF_ENABLE_DOCKER, False)
|
||||||
|
}
|
||||||
|
# 异步后台初始化
|
||||||
|
hass.async_create_task(async_delayed_setup(hass, entry, coordinator))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def async_delayed_setup(hass: HomeAssistant, entry: ConfigEntry, coordinator: FlynasCoordinator):
|
async def async_delayed_setup(hass: HomeAssistant, entry: ConfigEntry, coordinator: FlynasCoordinator):
|
||||||
try:
|
try:
|
||||||
# 首先进行轻量级系统状态检测
|
# 不阻塞等待NAS上线,直接尝试刷新数据
|
||||||
is_online = await coordinator.ping_system()
|
|
||||||
|
|
||||||
if not is_online:
|
|
||||||
_LOGGER.warning("系统离线,等待系统上线...")
|
|
||||||
# 等待系统上线
|
|
||||||
while not await coordinator.ping_system():
|
|
||||||
await asyncio.sleep(30)
|
|
||||||
|
|
||||||
_LOGGER.info("系统已上线,继续初始化飞牛NAS集成")
|
|
||||||
|
|
||||||
# 系统在线,继续正常初始化
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
# 检查是否启用Docker,并初始化Docker管理器(如果有)
|
|
||||||
enable_docker = coordinator.config.get(CONF_ENABLE_DOCKER, False)
|
enable_docker = coordinator.config.get(CONF_ENABLE_DOCKER, False)
|
||||||
if enable_docker:
|
if enable_docker:
|
||||||
# 导入Docker管理器并初始化
|
|
||||||
from .docker_manager import DockerManager
|
from .docker_manager import DockerManager
|
||||||
coordinator.docker_manager = DockerManager(coordinator)
|
coordinator.docker_manager = DockerManager(coordinator)
|
||||||
_LOGGER.debug("已启用Docker容器监控")
|
_LOGGER.debug("已启用Docker容器监控")
|
||||||
else:
|
else:
|
||||||
coordinator.docker_manager = None
|
coordinator.docker_manager = None
|
||||||
_LOGGER.debug("未启用Docker容器监控")
|
_LOGGER.debug("未启用Docker容器监控")
|
||||||
|
|
||||||
ups_coordinator = UPSDataUpdateCoordinator(hass, coordinator.config, coordinator)
|
ups_coordinator = UPSDataUpdateCoordinator(hass, coordinator.config, coordinator)
|
||||||
await ups_coordinator.async_config_entry_first_refresh()
|
await ups_coordinator.async_config_entry_first_refresh()
|
||||||
|
hass.data[DOMAIN][entry.entry_id]["ups_coordinator"] = ups_coordinator
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
|
||||||
DATA_UPDATE_COORDINATOR: coordinator,
|
|
||||||
"ups_coordinator": ups_coordinator,
|
|
||||||
CONF_ENABLE_DOCKER: enable_docker # 存储启用状态
|
|
||||||
}
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
entry.async_on_unload(entry.add_update_listener(async_update_entry))
|
entry.async_on_unload(entry.add_update_listener(async_update_entry))
|
||||||
|
|
||||||
_LOGGER.info("飞牛NAS集成初始化完成")
|
_LOGGER.info("飞牛NAS集成初始化完成")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("飞牛NAS集成初始化失败: %s", str(e))
|
_LOGGER.error("飞牛NAS集成初始化失败: %s", str(e))
|
||||||
# 如果初始化失败,确保清理资源
|
|
||||||
await coordinator.async_disconnect()
|
await coordinator.async_disconnect()
|
||||||
if hasattr(coordinator, '_ping_task') and coordinator._ping_task:
|
if hasattr(coordinator, '_ping_task') and coordinator._ping_task:
|
||||||
coordinator._ping_task.cancel()
|
coordinator._ping_task.cancel()
|
||||||
|
@@ -111,10 +111,10 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
self.ssh_closed = False
|
self.ssh_closed = False
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("切换到 root 会话失败,将使用 sudo")
|
# 切换到 root 会话失败,将使用 sudo
|
||||||
self.use_sudo = True
|
self.use_sudo = True
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("非 root 用户且未提供 root 密码,将使用 sudo")
|
# 非 root 用户且未提供 root 密码,将使用 sudo
|
||||||
self.use_sudo = True
|
self.use_sudo = True
|
||||||
|
|
||||||
self.ssh_closed = False
|
self.ssh_closed = False
|
||||||
@@ -258,23 +258,21 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
_LOGGER.debug("Starting data update...")
|
_LOGGER.debug("Starting data update...")
|
||||||
|
|
||||||
# 先进行轻量级系统状态检测
|
|
||||||
is_online = await self.ping_system()
|
is_online = await self.ping_system()
|
||||||
self._system_online = is_online
|
self._system_online = is_online
|
||||||
|
|
||||||
# 系统离线处理
|
|
||||||
if not is_online:
|
if not is_online:
|
||||||
_LOGGER.debug("系统离线,跳过数据更新")
|
_LOGGER.debug("系统离线,跳过数据更新")
|
||||||
|
# 修复:确保 self.data 结构有效
|
||||||
|
if self.data is None or not isinstance(self.data, dict):
|
||||||
|
self.data = {}
|
||||||
|
if "system" not in self.data or not isinstance(self.data.get("system"), dict):
|
||||||
|
self.data["system"] = {}
|
||||||
self.data["system"]["status"] = "off"
|
self.data["system"]["status"] = "off"
|
||||||
|
# 启动后台监控任务(非阻塞)
|
||||||
# 启动监控任务(如果尚未启动)
|
|
||||||
if not self._ping_task or self._ping_task.done():
|
if not self._ping_task or self._ping_task.done():
|
||||||
self._ping_task = asyncio.create_task(self._monitor_system_status())
|
self._ping_task = asyncio.create_task(self._monitor_system_status())
|
||||||
|
|
||||||
# 关闭SSH连接
|
|
||||||
await self.async_disconnect()
|
await self.async_disconnect()
|
||||||
|
# 直接返回空数据,不阻塞
|
||||||
return {
|
return {
|
||||||
"disks": [],
|
"disks": [],
|
||||||
"system": {
|
"system": {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"domain": "fn_nas",
|
"domain": "fn_nas",
|
||||||
"name": "飞牛NAS",
|
"name": "飞牛NAS",
|
||||||
"version": "1.3.1",
|
"version": "1.3.4",
|
||||||
"documentation": "https://github.com/anxms/fn_nas",
|
"documentation": "https://github.com/anxms/fn_nas",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@anxms"],
|
"codeowners": ["@anxms"],
|
||||||
|
Reference in New Issue
Block a user