PHP code example of indexzer0 / ha-rest-api-client
1. Go to this page and download the library: Download indexzer0/ha-rest-api-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
indexzer0 / ha-rest-api-client example snippets
/*
* ---------------------------
* HaRestApiClient
* ---------------------------
*/
$restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient(
'token',
'http://localhost:8123/api/'
);
$restApiClient->status(); // ['message' => 'API running.']
/*
* ---------------------------
* HaWebhookClient
* ---------------------------
*/
$webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient(
'http://localhost:8123/api/'
);
$webhookClient->send('GET', 'webhook_id'); // ['response' => '']
/*
* ---------------------------
* HaRestApiClient
* ---------------------------
*/
$restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient(
'token',
'http://localhost:8123/api/',
new \IndexZer0\HaRestApiClient\HttpClient\Builder(
new \GuzzleHttp\Client(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
)
);
$restApiClient->status(); // ['message' => 'API running.']
/*
* ---------------------------
* HaWebhookClient
* ---------------------------
*/
$webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient(
'http://localhost:8123/api/',
new \IndexZer0\HaRestApiClient\HttpClient\Builder(
new \GuzzleHttp\Client(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
)
);
$webhookClient->send('GET', 'webhook_id'); // ['response' => '']
$restApiClient = new \IndexZer0\HaRestApiClient\HaRestApiClient(
'token',
'http://localhost:8123/api/'
);
$restApiClient->status();
$restApiClient->config();
$restApiClient->events();
$restApiClient->services();
$restApiClient->history(['light.bedroom_ceiling']);
$restApiClient->logbook();
$restApiClient->states();
$restApiClient->state('light.bedroom_ceiling');
$restApiClient->errorLog();
$restApiClient->calendars();
$restApiClient->calendarEvents('calendar.birthdays');
$restApiClient->updateState('light.bedroom_ceiling', 'on');
$restApiClient->fireEvent('script_started', [
'name' => 'Turn All Lights Off',
'entity_id' => 'script.turn_all_lights_off'
]);
$restApiClient->callService('light', 'turn_on', [
'entity_id' => 'light.bedroom_ceiling'
]);
$restApiClient->renderTemplate("The bedroom ceiling light is {{ states('light.bedroom_ceiling') }}.");
$restApiClient->checkConfig();
$restApiClient->handleIntent([
'name' => 'SetTimer',
'data' => [
'seconds' => '30',
]
]);
$webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient(
'http://localhost:8123/api/'
);
/*
* ---------------------------
* GET request example
* ---------------------------
*/
$webhookClient->send(
method: 'GET',
webhookId: 'webhook_id',
queryParams: ['query' => 'param'],
);
/*
* ---------------------------
* POST request example - with json body
* ---------------------------
*/
$webhookClient->send(
method: 'POST',
webhookId: 'webhook_id',
payloadType: 'json',
data: ['json' => 'data']
);
/*
* ---------------------------
* POST request example - with form params body
* ---------------------------
*/
$webhookClient->send(
method: 'POST',
webhookId: 'webhook_id',
payloadType: 'form_params',
data: ['form' => 'param']
);
try {
$webhookClient = new \IndexZer0\HaRestApiClient\HaWebhookClient(
'http://localhost:8123/api/'
);
$response = $webhookClient->send('GET', 'webhook_id');
} catch (\IndexZer0\HaRestApiClient\Exception\HaExceptionInterface $haException) {
} catch (\Throwable $t) {
// Shouldn't happen - but failsafe.
}