39 lines
1.5 KiB
PHP
39 lines
1.5 KiB
PHP
<?php
|
|
|
|
use GuzzleHttp\Client;
|
|
use Kplaczek\FoodsiBot\FoodsiService;
|
|
use Kplaczek\FoodsiBot\HomeassistantService;
|
|
|
|
require_once 'vendor/autoload.php';
|
|
date_default_timezone_set('Europe/Warsaw');
|
|
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
|
$dotenv->load();
|
|
$client = new Client();
|
|
$homeassistant = new HomeassistantService($client);
|
|
if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') {
|
|
return;
|
|
}
|
|
$foodsi = new FoodsiService($client);
|
|
$packageInfo = $foodsi->getPackageInfo();
|
|
$receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
|
|
$notificationSent = false;
|
|
foreach ($receivers as $receiver) {
|
|
if (!file_exists('last_notification_date.txt')) {
|
|
touch('last_notification_date.txt');
|
|
}
|
|
$lastNotificationDate = file_get_contents('last_notification_date.txt');
|
|
if ($packageInfo['current_quantity'] > 0 && $lastNotificationDate !== date('Y-m-d')) {
|
|
$homeassistant->sendNotification(
|
|
$receiver,
|
|
'Dostępna paczka w 3 kromki chleba',
|
|
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
|
|
);
|
|
$notificationSent = true;
|
|
}
|
|
}
|
|
if ($notificationSent) {
|
|
file_put_contents('last_notification_date.txt', date('Y-m-d'));
|
|
$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
|
|
}
|
|
$homeassistant->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
|