From 8400c1eaf1495d272c14e4012cc0fce6ddc5f9b9 Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Thu, 12 Feb 2026 21:48:03 +0100 Subject: [PATCH] feat(#188): support `battery-percent` header --- routes/api.php | 5 ++-- tests/Feature/Api/DeviceEndpointsTest.php | 34 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/routes/api.php b/routes/api.php index 73b2749..c759d61 100644 --- a/routes/api.php +++ b/routes/api.php @@ -49,8 +49,9 @@ Route::get('/display', function (Request $request) { 'last_refreshed_at' => now(), ]); - if ($request->hasHeader('battery-percent')) { - $batteryPercent = (int) $request->header('battery-percent'); + $batteryPercent = $request->header('battery-percent') ?? $request->header('percent-charged'); + if ($batteryPercent !== null) { + $batteryPercent = (int) $batteryPercent; $batteryVoltage = $device->calculateVoltageFromPercent($batteryPercent); $device->update([ 'last_battery_voltage' => $batteryVoltage, diff --git a/tests/Feature/Api/DeviceEndpointsTest.php b/tests/Feature/Api/DeviceEndpointsTest.php index 44fb651..f64d203 100644 --- a/tests/Feature/Api/DeviceEndpointsTest.php +++ b/tests/Feature/Api/DeviceEndpointsTest.php @@ -725,6 +725,40 @@ test('display endpoint updates last_refreshed_at timestamp', function (): void { ->and($device->last_refreshed_at->diffInSeconds(now()))->toBeLessThan(2); }); +test('display endpoint accepts battery-percent header and updates device', function (): void { + $device = Device::factory()->create([ + 'mac_address' => '00:11:22:33:44:56', + 'api_key' => 'test-api-key-battery', + 'last_battery_voltage' => null, + ]); + + $this->withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'battery-percent' => '67', + ])->get('/api/display')->assertOk(); + + $device->refresh(); + expect($device->battery_percent)->toEqual(67); +}); + +test('display endpoint accepts Percent-Charged header and updates device', function (): void { + $device = Device::factory()->create([ + 'mac_address' => '00:11:22:33:44:57', + 'api_key' => 'test-api-key-percent-charged', + 'last_battery_voltage' => null, + ]); + + $this->withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'Percent-Charged' => '51', + ])->get('/api/display')->assertOk(); + + $device->refresh(); + expect($device->battery_percent)->toEqual(51); +}); + test('display endpoint updates last_refreshed_at timestamp for mirrored devices', function (): void { // Create source device $sourceDevice = Device::factory()->create([