子设备加延时
This commit is contained in:
@@ -267,6 +267,7 @@ class JackeryDataCoordinator:
|
||||
self._last_update_time = time.time()
|
||||
|
||||
self._known_plugs = set() # Set of known plug SNs
|
||||
self._subdevice_missing_since = {} # {sn: timestamp} for deletion delay
|
||||
self.add_entities_callback = None # Callback to add new entities
|
||||
self.add_switch_entities_callback = None # Callback to add new switch entities
|
||||
self._data_cache = {} # Cache for merged data from status and events
|
||||
@@ -446,11 +447,41 @@ class JackeryDataCoordinator:
|
||||
if sn:
|
||||
current_sns.add(sn)
|
||||
|
||||
# 1. 处理移除 (Known - Current)
|
||||
removed_sns = self._known_plugs - current_sns
|
||||
for sn in removed_sns:
|
||||
_LOGGER.info(f"Sub-device removed: {sn}")
|
||||
now = time.time()
|
||||
|
||||
# 1. 更新 missing 状态
|
||||
# A. 既然出现了,清除之前的缺失计时
|
||||
for sn in current_sns:
|
||||
if sn in self._subdevice_missing_since:
|
||||
_LOGGER.info(f"Sub-device {sn} reappeared, cancelling deletion.")
|
||||
del self._subdevice_missing_since[sn]
|
||||
|
||||
# B. 检查已知但当前缺失的
|
||||
for sn in self._known_plugs:
|
||||
if sn not in current_sns:
|
||||
if sn not in self._subdevice_missing_since:
|
||||
self._subdevice_missing_since[sn] = now
|
||||
_LOGGER.info(f"Sub-device {sn} missing, starting 60s deletion timer...")
|
||||
|
||||
# 2. 执行真正的移除 (检查 missing 列表)
|
||||
# 使用 list() 复制 keys,允许在迭代中删除字典项
|
||||
for sn in list(self._subdevice_missing_since.keys()):
|
||||
# 如果该设备已不再已知列表里(可能已被删),清理记录并跳过
|
||||
if sn not in self._known_plugs:
|
||||
del self._subdevice_missing_since[sn]
|
||||
continue
|
||||
|
||||
# 只有当确实还在缺失状态(不在 current_sns)时才检查时间
|
||||
# (虽然上面的步骤 A 已经清理了出现的,但双重检查更稳妥)
|
||||
if sn in current_sns:
|
||||
del self._subdevice_missing_since[sn]
|
||||
continue
|
||||
|
||||
missing_time = self._subdevice_missing_since[sn]
|
||||
if now - missing_time > 60:
|
||||
_LOGGER.info(f"Sub-device {sn} missing for >60s. Removing.")
|
||||
self._known_plugs.remove(sn)
|
||||
del self._subdevice_missing_since[sn]
|
||||
|
||||
# 查找并删除相关实体
|
||||
keys_to_remove = []
|
||||
@@ -459,7 +490,7 @@ class JackeryDataCoordinator:
|
||||
keys_to_remove.append(sensor_id)
|
||||
self.hass.async_create_task(entity.async_remove(force_remove=True))
|
||||
|
||||
# 2. 处理新增
|
||||
# 3. 处理新增
|
||||
new_entities = []
|
||||
new_switch_entities = []
|
||||
for plug in plugs:
|
||||
|
||||
Reference in New Issue
Block a user