homeos/app/Livewire/Modals/AddDevice.php

58 lines
1.4 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\Room;
use App\Services\ShellyLocalOnboarder;
use LivewireUI\Modal\ModalComponent;
/**
* Manually add a Shelly by its IP (for devices not found via discovery). HomeOS probes the local
* API and onboards it — no MQTT setup on the device.
*/
class AddDevice extends ModalComponent
{
public string $ip = '';
public string $name = '';
public ?int $roomId = null;
public ?string $error = null;
public static function modalMaxWidth(): string
{
return 'lg';
}
public function save()
{
$this->validate([
'ip' => ['required', 'string', 'max:64', 'regex:/^[a-zA-Z0-9.\-]+$/'],
'name' => ['nullable', 'string', 'max:100'],
'roomId' => ['nullable', 'integer', 'exists:rooms,id'],
]);
$this->error = null;
try {
$device = app(ShellyLocalOnboarder::class)->onboard(trim($this->ip), $this->name ?: null, $this->roomId ?: null);
} catch (\Throwable) {
$this->error = __('devices.add_unreachable');
return;
}
$this->closeModal();
return redirect()->route('devices.show', $device);
}
public function render()
{
return view('livewire.modals.add-device', [
'rooms' => Room::orderBy('sort')->orderBy('name')->get(),
]);
}
}