[ 'Authorization: Bearer ' . HA_LONG_LIVED_TOKEN, 'Content-Type: application/json' ], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, ]); $res = curl_exec($ch); $errno = curl_errno($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($errno !== 0 || $status < 200 || $status >= 300) { return null; } $json = json_decode($res, true); // HA는 상태값을 문자열로 줍니다. 숫자면 (string)"12" 형태. if (!is_array($json) || !isset($json['state'])) return null; // 숫자로 캐스팅 시도 (실패해도 문자열 반환 가능) $stateRaw = $json['state']; if (is_numeric($stateRaw)) { return (int)$stateRaw; } return $stateRaw; // 숫자가 아니면 원본 문자열 } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $password = $_POST['password'] ?? ''; if ($password === '0010') { echo json_encode(['result' => 'ok']); } else { echo json_encode(['result' => 'fail']); } exit; } if ($_SERVER['REQUEST_METHOD'] === 'GET') { if (isset($_GET['failMessages'])) { echo json_encode([ 'failMessages' => [ '비밀번호가 틀립니다.', ] ], JSON_UNESCAPED_UNICODE); exit; } if (isset($_GET['pokeCount'])) { // HA 엔티티 상태 읽기 $count = ha_get_entity_state(POKE_ENTITY_ID); if ($count === null) { http_response_code(502); echo json_encode([ 'ok' => false, 'error' => 'failed_to_fetch_from_ha' ], JSON_UNESCAPED_UNICODE); exit; } echo json_encode([ 'ok' => true, 'count' => $count ], JSON_UNESCAPED_UNICODE); exit; } http_response_code(404); echo json_encode(['error' => 'not_found']); exit; } http_response_code(405); echo json_encode(['error' => 'method_not_allowed']); ?>