Tars/src/Command/HomeAssistantCommand.php
2025-03-14 18:27:56 +01:00

77 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Command;
use App\Core\Services\Home\HomeEntityInterface;
use App\Core\Services\Home\HomeServiceInterface;
use function array_map;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:home',
description: 'Interact with the configured Home Service',
)]
final class HomeAssistantCommand extends Command
{
public function __construct(
private readonly HomeServiceInterface $homeService,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('list-entities', null, InputOption::VALUE_NONE, 'List all entities')
->addOption('domain', null, InputOption::VALUE_REQUIRED, 'Filter entities by domain')
->addOption('entity-id', null, InputOption::VALUE_REQUIRED, 'Entity ID to interact with')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if ($input->getOption('list-entities')) {
return $this->listEntities($io);
}
$entityId = $input->getOption('entity-id');
if ($entityId === null) {
$io->error('You must specify an entity ID using --entity-id option');
return Command::FAILURE;
}
return Command::SUCCESS;
}
private function listEntities(SymfonyStyle $io): int
{
$entities = $this->homeService->findAllEntities();
$rows = array_map(
static fn (HomeEntityInterface $entity) => [
$entity->getId(),
$entity->getName(),
$entity->getState(),
],
$entities,
);
$io->table(['Entity ID', 'Name', 'State'], $rows);
return Command::SUCCESS;
}
}