firstOrFail(); $this->findingUuid = $model->uuid; $this->name = $model->name ?? $model->identifier; } #[Computed] public function finding(): DiscoveryFinding { return DiscoveryFinding::where('uuid', $this->findingUuid)->firstOrFail(); } public function assign() { $this->validate([ 'name' => 'required|string|max:100', 'roomId' => 'nullable|integer|exists:rooms,id', ]); $finding = $this->finding(); $isShelly = $finding->vendor === 'Shelly'; // The Shelly MQTT topic prefix is its device id — the mDNS service INSTANCE name // (finding->name), not the slugified topic identifier which carries the _shelly._tcp suffix. $prefix = $isShelly ? ($finding->name ?: $finding->identifier) : null; $device = Device::create([ 'name' => $this->name, 'vendor' => $finding->vendor, 'model' => $finding->model, 'protocol' => $isShelly ? 'mqtt' : null, 'config' => $prefix ? ['mqtt_prefix' => $prefix] : null, 'room_id' => $this->roomId ?: null, 'status' => 'active', ]); $finding->update(['status' => 'assigned', 'device_id' => $device->id]); // Provision a per-device MQTT credential (bound to its own prefix by the ACL) and // surface it once so the user can enter it into the Shelly. if ($isShelly && $prefix) { $password = app(MqttCredentialProvisioner::class)->provision($prefix); session()->flash('mqtt_credential', [ 'username' => $prefix, 'password' => $password, 'port' => (int) config('mqtt-client.connections.default.port', 1883), ]); } $this->closeModal(); return redirect()->route('devices.show', $device); } public function render() { return view('livewire.modals.assign-device', [ 'finding' => $this->finding(), 'rooms' => Room::orderBy('sort')->orderBy('name')->get(), ]); } }