69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Modals;
|
|
|
|
use App\Models\Entity;
|
|
use App\Services\DeviceCommandService;
|
|
use Livewire\Attributes\Computed;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
class LightControl extends ModalComponent
|
|
{
|
|
public string $entityUuid = '';
|
|
|
|
public int $brightness = 100;
|
|
|
|
public string $color = '#FFD9A0';
|
|
|
|
public static function modalMaxWidth(): string
|
|
{
|
|
return 'md';
|
|
}
|
|
|
|
public function mount(string $entity): void
|
|
{
|
|
$model = Entity::where('uuid', $entity)->firstOrFail();
|
|
$this->entityUuid = $model->uuid;
|
|
|
|
$state = $model->state?->state ?? [];
|
|
$this->brightness = (int) ($state['brightness'] ?? 100);
|
|
|
|
if (isset($state['rgb']) && is_array($state['rgb'])) {
|
|
$rgb = array_pad(array_map('intval', $state['rgb']), 3, 255);
|
|
$this->color = sprintf('#%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
|
|
}
|
|
}
|
|
|
|
#[Computed]
|
|
public function entity(): Entity
|
|
{
|
|
return Entity::with('device', 'state')->where('uuid', $this->entityUuid)->firstOrFail();
|
|
}
|
|
|
|
public function apply(): void
|
|
{
|
|
$rgb = sscanf($this->color, '#%02x%02x%02x');
|
|
|
|
app(DeviceCommandService::class)->setLight($this->entity(), [
|
|
'on' => true,
|
|
'brightness' => $this->brightness,
|
|
'rgb' => $rgb,
|
|
]);
|
|
|
|
$this->closeModal();
|
|
}
|
|
|
|
public function turnOff(): void
|
|
{
|
|
app(DeviceCommandService::class)->setOn($this->entity(), false);
|
|
$this->closeModal();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.modals.light-control', [
|
|
'presets' => ['#FFD9A0', '#FFFFFF', '#FF6B6B', '#4FC1FF', '#34D399', '#A78BFA', '#FBBF24'],
|
|
]);
|
|
}
|
|
}
|