From a555f552c2a7c7befd5c31087047b8247ba9800a Mon Sep 17 00:00:00 2001 From: Florian Heinz Date: Tue, 19 May 2026 15:21:58 +0200 Subject: [PATCH] Support Health Auto Export metric names --- src/App.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/App.php b/src/App.php index b84dec3..c8dda75 100644 --- a/src/App.php +++ b/src/App.php @@ -493,17 +493,17 @@ final class App $sleepBuckets = []; foreach ($metrics as $metric) { - $name = strtolower((string) ($metric['name'] ?? '')); - $data = array_values(array_filter(is_array($metric['data'] ?? null) ? $metric['data'] : [], 'is_array')); + $name = $this->healthMetricName((string) ($metric['name'] ?? ($metric['metric'] ?? ''))); + $data = array_values(array_filter(is_array($metric['data'] ?? null) ? $metric['data'] : (is_array($metric['records'] ?? null) ? $metric['records'] : []), 'is_array')); if ($name === 'step_count') { foreach ($data as $point) { - $date = $this->healthPointDate($point['date'] ?? null); + $date = $this->healthPointDate($point['date'] ?? ($point['startDate'] ?? null)); if ($date === null) { continue; } - $steps[$date] = ($steps[$date] ?? 0) + max(0, (int) round((float) ($point['qty'] ?? 0))); + $steps[$date] = ($steps[$date] ?? 0) + max(0, (int) round((float) ($point['qty'] ?? ($point['value'] ?? 0)))); } } @@ -590,6 +590,17 @@ final class App ]; } + private function healthMetricName(string $name): string + { + $normalized = normalize_sport_type_id($name); + + return match ($normalized) { + 'step-count', 'steps', 'hkquantitytypeidentifierstepcount' => 'step_count', + 'sleep-analysis', 'sleep', 'hkcategorytypeidentifiersleepanalysis' => 'sleep_analysis', + default => str_replace('-', '_', $normalized), + }; + } + private function healthEventsFromWorkouts(array $workouts, array &$settings): array { $sport = []; @@ -598,8 +609,8 @@ final class App $sportTypesAdded = 0; foreach ($workouts as $workout) { - $start = $this->healthDateTime($workout['start'] ?? null); - $end = $this->healthDateTime($workout['end'] ?? null); + $start = $this->healthDateTime($workout['start'] ?? ($workout['startDate'] ?? null)); + $end = $this->healthDateTime($workout['end'] ?? ($workout['endDate'] ?? null)); if ($start === null) { continue; }