Initial private import
This commit is contained in:
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
|
||||||
|
// Home Assistant 설정
|
||||||
|
$HA_URL = "https://ha.seoul.chaegeon.com/api"; // 또는 http://IP:8123/api
|
||||||
|
$HA_TOKEN = "REDACTED_HA_TOKEN"; // HA 장기 토큰
|
||||||
|
$ENTITY = "switch.keompyuteo"; // 제어할 스위치 엔티티명
|
||||||
|
|
||||||
|
function haRequest($method, $endpoint, $data = null) {
|
||||||
|
global $HA_URL, $HA_TOKEN;
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $HA_URL . $endpoint);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
|
||||||
|
|
||||||
|
$headers = [
|
||||||
|
"Authorization: Bearer $HA_TOKEN",
|
||||||
|
"Content-Type: application/json"
|
||||||
|
];
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
|
||||||
|
if ($data) {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
return json_decode($response, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
--------------------------------------------
|
||||||
|
1) 상태 확인 요청 (/api.php?status=1)
|
||||||
|
--------------------------------------------
|
||||||
|
*/
|
||||||
|
if (isset($_GET['status'])) {
|
||||||
|
$state = haRequest("GET", "/states/$ENTITY");
|
||||||
|
|
||||||
|
if (!$state || !isset($state["state"])) {
|
||||||
|
echo json_encode(["result" => "error"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
"result" => "ok",
|
||||||
|
"state" => $state["state"] // "on" or "off"
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
--------------------------------------------
|
||||||
|
2) 스위치 켜기 요청 (POST)
|
||||||
|
--------------------------------------------
|
||||||
|
*/
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||||
|
$res = haRequest("POST", "/services/switch/turn_on", [
|
||||||
|
"entity_id" => $ENTITY
|
||||||
|
]);
|
||||||
|
|
||||||
|
echo json_encode(["result" => "sent"]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode(["result" => "invalid_request"]);
|
||||||
Reference in New Issue
Block a user