fix: ha_list reads from ha_entities table (real-time agent data, not 5min cache)

This commit is contained in:
2026-05-31 06:05:45 +00:00
parent a96f8a3f85
commit 1f90d83073
+36 -14
View File
@@ -234,22 +234,44 @@ if ($action) {
// ── HOME ASSISTANT ENTITIES ─────────────────────────────────────────── // ── HOME ASSISTANT ENTITIES ───────────────────────────────────────────
case 'ha_list': case 'ha_list':
$raw = JarvisDB::single("SELECT data, UNIX_TIMESTAMP(updated_at) ts FROM api_cache WHERE cache_key='ha_entities'"); // Read from ha_entities table (real-time pushes from jarvis_agent custom component)
if (!$raw) j(['entities'=>[],'domains'=>[],'ts'=>null]);
$cache = json_decode($raw['data'], true) ?? [];
$domain = $_GET['domain'] ?? ''; $domain = $_GET['domain'] ?? '';
$search = strtolower(trim($_GET['search'] ?? '')); $search = strtolower(trim($_GET['search'] ?? ''));
$all = []; $skipDomains = ['sensor','binary_sensor','button','update','select','number',
foreach ($cache['entities'] ?? [] as $dom => $ents) { 'device_tracker','event','image','person','zone','tts','conversation',
if ($domain && $dom !== $domain) continue; 'assist_satellite','input_button'];
foreach ($ents as $e) { $skipKeywords = ['pre_release','_record','_ftp_','_push_','_hub_ringtone',
if ($search && strpos(strtolower($e['name']??''),$search)===false && strpos(strtolower($e['entity_id']??''),$search)===false) continue; '_siren_on','_email_on','_manual_record','_infrared_',
$e['domain'] = $dom; 'do_not_disturb','matter_server','zerotier','mariadb',
$all[] = $e; 'spotify_connect','file_editor','ssh_web','uptime_kuma',
'folding_home','music_assistant','get_hacs','mealie',
'mosquitto','social_to','esphome_device','motion_detection',
'front_yard_record','down_hill_record','camera1_record',
'back_yard_record','nvr_','assist_microphone','cec_scanner'];
$where = "state NOT IN ('unavailable','unknown')";
$params = [];
if ($domain) { $where .= " AND domain=?"; $params[] = $domain; }
$rows = JarvisDB::query(
"SELECT entity_id, entity_name name, domain, state, updated_at
FROM ha_entities WHERE $where ORDER BY domain, entity_name LIMIT 500",
$params
) ?? [];
$all = []; $domains = [];
foreach ($rows as $e) {
$dom = $e['domain'];
if (in_array($dom, $skipDomains)) continue;
$skip = false;
if ($dom === 'switch') {
foreach ($skipKeywords as $kw) {
if (strpos($e['entity_id'], $kw) !== false) { $skip = true; break; }
}
} }
if ($skip) continue;
if ($search && strpos(strtolower($e['name']??''), $search) === false) continue;
$all[] = $e;
$domains[$dom] = true;
} }
usort($all, fn($a,$b) => strcmp($a['name']??'',$b['name']??'')); j(['entities'=>$all,'domains'=>array_keys($domains),'total'=>count($all),'ts'=>time()]);
j(['entities'=>array_slice($all,0,500),'domains'=>array_keys($cache['entities']??[]),'total'=>count($all),'ts'=>$raw['ts']]);
case 'ha_toggle': case 'ha_toggle':
$eid = trim($_POST['entity_id'] ?? ''); if (!$eid) bad('Missing entity_id'); $eid = trim($_POST['entity_id'] ?? ''); if (!$eid) bad('Missing entity_id');
@@ -1334,8 +1356,8 @@ function haToggle(entityId, currentState, el) {
ent.state = wasOn ? 'off' : 'on'; ent.state = wasOn ? 'off' : 'on';
filterHATable(); filterHATable();
} }
// Also sync from HA after 3s (actual state confirmation) // Sync from ha_entities (real-time agent data) after 5s — enough time for HA to execute + push
setTimeout(loadHA, 3000); setTimeout(loadHA, 5000);
} else { } else {
toast('Toggle failed (code ' + (res.code||'?') + ')', 'err'); toast('Toggle failed (code ' + (res.code||'?') + ')', 'err');
} }