56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Rooms;
|
|
|
|
use App\Models\Entity;
|
|
use App\Models\Room;
|
|
use App\Services\DeviceCommandService;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Show extends Component
|
|
{
|
|
public Room $room;
|
|
|
|
public function mount(Room $room): void
|
|
{
|
|
$this->room = $room;
|
|
}
|
|
|
|
#[On('echo-private:home,.DeviceStateChanged')]
|
|
public function onDeviceStateChanged(): void {}
|
|
|
|
public function toggleEntity(int $entityId): void
|
|
{
|
|
$entity = Entity::query()
|
|
->whereHas('device', fn ($q) => $q->where('room_id', $this->room->id))
|
|
->with('device', 'state')
|
|
->find($entityId);
|
|
|
|
if ($entity === null || ! in_array($entity->type, ['switch', 'light'], true)) {
|
|
return;
|
|
}
|
|
|
|
app(DeviceCommandService::class)->toggle($entity);
|
|
}
|
|
|
|
/** Delete the room; its devices are kept but moved to "no room". */
|
|
#[On('deleteRoom')]
|
|
public function deleteRoom()
|
|
{
|
|
$this->room->devices()->update(['room_id' => null]);
|
|
$this->room->delete();
|
|
|
|
return redirect()->route('rooms.index');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$this->room->load(['devices' => fn ($q) => $q->orderBy('name'), 'devices.entities.state']);
|
|
|
|
return view('livewire.rooms.show');
|
|
}
|
|
}
|