Split files into smaller classed. Add enable_bot.php file to enable bot daily.

This commit is contained in:
Krzysztof Płaczek
2024-11-11 16:22:29 +01:00
parent 499e14de45
commit a262679916
4 changed files with 121 additions and 41 deletions

View File

@@ -0,0 +1,43 @@
<?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]
]);
}
}