Split files into smaller classed. Add enable_bot.php file to enable bot daily.
This commit is contained in:
43
src/HomeassistantService.php
Normal file
43
src/HomeassistantService.php
Normal 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]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user