homeos/app/Livewire/Concerns/TogglesEntities.php

26 lines
686 B
PHP

<?php
namespace App\Livewire\Concerns;
use App\Models\Entity;
use App\Services\DeviceCommandService;
/**
* Shared `toggleEntity` for any page whose cards carry an inline switch/light toggle. Routes
* through the command service (H1 — audited + driver); the device echoes its new state back over
* MQTT, which re-renders the card live.
*/
trait TogglesEntities
{
public function toggleEntity(int $entityId): void
{
$entity = Entity::with('device', 'state')->find($entityId);
if ($entity === null || ! in_array($entity->type, ['switch', 'light'], true)) {
return;
}
app(DeviceCommandService::class)->toggle($entity);
}
}