From 2b919a193d5602f4d50c9826dc734801cf9710dd Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Tue, 3 Feb 2026 22:21:35 +0100 Subject: [PATCH] fix(#176): inject device variables into recipes --- app/Models/PlaylistItem.php | 4 +-- app/Models/Plugin.php | 7 +++++ tests/Unit/Models/PlaylistItemTest.php | 38 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/Models/PlaylistItem.php b/app/Models/PlaylistItem.php index ad11f1d..31a6b69 100644 --- a/app/Models/PlaylistItem.php +++ b/app/Models/PlaylistItem.php @@ -143,7 +143,7 @@ class PlaylistItem extends Model 'deviceVariant' => $device?->deviceVariant() ?? 'og', 'scaleLevel' => $device?->scaleLevel(), 'slot' => $this->plugin instanceof Plugin - ? $this->plugin->render('full', false) + ? $this->plugin->render('full', false, $device) : throw new Exception('Invalid plugin instance'), ])->render(); } @@ -157,7 +157,7 @@ class PlaylistItem extends Model foreach ($plugins as $index => $plugin) { $size = $this->getLayoutSize($index); - $pluginMarkups[] = $plugin->render($size, false); + $pluginMarkups[] = $plugin->render($size, false, $device); } return view('trmnl-layouts.mashup', [ diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index dceb795..bc46559 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -447,6 +447,13 @@ class Plugin extends Model 'locale' => 'en', 'time_zone_iana' => $timezone, ], + 'device' => [ + 'friendly_id' => $device?->friendly_id, + 'percent_charged' => $device?->battery_percent, + 'wifi_strength' => $device?->wifi_strength, + 'height' => $device?->height, + 'width' => $device?->width, + ], 'plugin_settings' => [ 'instance_name' => $this->name, 'strategy' => $this->data_strategy, diff --git a/tests/Unit/Models/PlaylistItemTest.php b/tests/Unit/Models/PlaylistItemTest.php index 428a165..162ba4c 100644 --- a/tests/Unit/Models/PlaylistItemTest.php +++ b/tests/Unit/Models/PlaylistItemTest.php @@ -1,8 +1,10 @@ create(); @@ -208,3 +210,39 @@ test('playlist item can create mashup', function (): void { ->is_active->toBeTrue() ->order->toBe($order); }); + +test('playlist item mashup render includes device context in liquid (trmnl.device.friendly_id)', function (): void { + $user = User::factory()->create(); + $device = Device::factory()->create([ + 'user_id' => $user->id, + 'friendly_id' => 'my-kitchen-display', + ]); + $playlist = Playlist::factory()->create(['device_id' => $device->id]); + + $plugin1 = Plugin::factory()->create([ + 'user_id' => $user->id, + 'plugin_type' => 'recipe', + 'markup_language' => 'liquid', + 'render_markup' => '{{ trmnl.device.friendly_id }}', + ]); + $plugin2 = Plugin::factory()->create([ + 'user_id' => $user->id, + 'plugin_type' => 'recipe', + 'markup_language' => 'liquid', + 'render_markup' => 'slot2:{{ trmnl.device.friendly_id }}', + ]); + + $playlistItem = PlaylistItem::createMashup( + $playlist, + '1Lx1R', + [$plugin1->id, $plugin2->id], + 'Device context mashup', + 1 + ); + + $markup = $playlistItem->render($device); + + expect($markup) + ->toContain('my-kitchen-display') + ->toContain('slot2:my-kitchen-display'); +});