Files
jarvis/api/endpoints/ha.php
T

101 lines
3.7 KiB
PHP

<?php
// Home Assistant endpoint — entities served from api_cache (refreshed every 5 min by cron)
// Live service calls (turn on/off) still go direct to HA.
function haRequest(string $path, string $method = 'GET', array $payload = []): ?array {
if (HA_TOKEN === 'YOUR_HA_TOKEN_HERE' || strpos(HA_URL, '10.48.200.X') !== false) {
return null;
}
$ch = curl_init(HA_URL . '/api' . $path);
$headers = [
'Authorization: Bearer ' . HA_TOKEN,
'Content-Type: application/json',
];
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 8,
CURLOPT_CONNECTTIMEOUT => 4,
CURLOPT_SSL_VERIFYPEER => false,
]);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
}
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code >= 200 && $code < 300 && $resp) {
return json_decode($resp, true);
}
return null;
}
$configured = !(HA_TOKEN === 'YOUR_HA_TOKEN_HERE' || strpos(HA_URL, '10.48.200.X') !== false);
if (!$configured) {
echo json_encode(['configured' => false, 'message' => 'HA token not configured.', 'entities' => []]);
exit;
}
// Live service call (toggle device) — always direct to HA, never cached
if ($method === 'POST' && $action === 'service') {
$domain = $data['domain'] ?? '';
$service = $data['service'] ?? '';
$entity_id = $data['entity_id'] ?? '';
if ($domain && $service && $entity_id) {
$result = haRequest("/services/{$domain}/{$service}", 'POST', ['entity_id' => $entity_id]);
echo json_encode(['success' => true, 'result' => $result]);
} else {
echo json_encode(['error' => 'Missing domain/service/entity_id']);
}
exit;
}
// Serve entities from ha_entities table (real-time agent push data)
$skipDomains = ['sensor','binary_sensor','button','update','select','number',
'device_tracker','event','image','person','zone','tts','conversation',
'assist_satellite','input_button'];
$skipKeywords = ['pre_release','_record','_ftp_','_push_','_hub_ringtone',
'_siren_on','_email_on','_manual_record','_infrared_',
'do_not_disturb','matter_server','zerotier','mariadb',
'spotify_connect','file_editor','ssh_web','uptime_kuma',
'adguard_','folding_home','music_assistant','get_hacs','mealie',
'mosquitto','social_to','motion_detection',
'front_yard_record','down_hill_record','camera1_record',
'back_yard_record','nvr_','assist_microphone'];
$rows = JarvisDB::query(
"SELECT entity_id, entity_name, domain, state, UNIX_TIMESTAMP(updated_at) as updated_ts
FROM ha_entities
WHERE state NOT IN ('unavailable','unknown')
ORDER BY domain, entity_name"
) ?? [];
$grouped = [];
$latestTs = 0;
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 ((int)$e['updated_ts'] > $latestTs) $latestTs = (int)$e['updated_ts'];
$grouped[$dom][] = [
'entity_id' => $e['entity_id'],
'name' => $e['entity_name'],
'state' => $e['state'],
];
}
echo json_encode([
'configured' => true,
'entities' => $grouped,
'cache_age_s' => $latestTs > 0 ? (int)(time() - $latestTs) : -1,
'cached_at' => $latestTs > 0 ? date('c', $latestTs) : null,
]);