43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Kplaczek\FoodsiBot;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
readonly class HomeassistantService
|
|
{
|
|
public function __construct(private Client $client)
|
|
{
|
|
}
|
|
|
|
public function getEntityInfo(string $entityId): array
|
|
{
|
|
$response = $this->client->get(sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'],
|
|
],
|
|
]);
|
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
}
|
|
|
|
public function updateEntity(string $entityId, string $state, array $attributes = []): void
|
|
{
|
|
$this->client->post(
|
|
sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [
|
|
'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']],
|
|
'json' => ['state' => $state, 'attributes' => $attributes],
|
|
]
|
|
);
|
|
}
|
|
|
|
function sendNotification(string $receiver, string $title, string $body): void
|
|
{
|
|
$this->client->post(sprintf('%s/api/services/notify/%s', $_ENV['HOMEASSISTANT_URL'], $receiver), [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'],
|
|
],
|
|
'json' => ['message' => $body, 'title' => $title]
|
|
]);
|
|
}
|
|
} |