概览
本指南介绍如何为新的健康设备或平台构建自定义 Provider 集成。Provider 系统的设计目标是在保证安全与数据质量的前提下,让新增集成尽可能简单直观。如果你只是想使用现有 Providers(如 Garmin、Whoop),请参见使用 Providers。
前置条件
在集成新的 Provider 之前,请确保你具备:技术要求
技术要求
- Python 3.12+ 环境
- 可访问目标设备/服务的 API 文档
- 设备厂商提供的 OAuth 凭据(OAuth 1.0 或 OAuth 2.0)
- 熟悉 Python 的 async/await 模式
- 熟悉 REST APIs 与 JSON 数据格式
OAuth 凭据
OAuth 凭据
从健康设备厂商获取 OAuth 凭据:
- Client ID / Consumer Key
- Client Secret / Consumer Secret
- 用于认证与数据访问的 API endpoints
- 访问健康数据所需的 OAuth scopes
数据库准备
数据库准备
你的 Provider 需要一个专用表来存储 raw 数据:
Provider Architecture
Directory Structure
Class Hierarchy
Quick Start
1
创建 Provider 模块
为你的 Provider 创建一个新目录:
2
实现 Provider class
创建继承自
BaseThetaProvider 的 Provider class:provider_yourprovider.py
3
配置 OAuth
在
config.yaml 中添加配置:4
测试 Provider
测试 OAuth flow 与数据拉取:
Implementation Checklist
Required Methods
Required Methods
你的 Provider 必须实现:
- ✅
factory()- Provider instantiation - ✅
info()- Provider metadata - ✅
link()- Initiate OAuth flow - ✅
callback()- Handle OAuth callback - ✅
unlink()- Disconnect provider - ✅
pull_from_vendor_api()- Fetch data from API - ✅
format_data()- Transform to standard format - ✅
save_raw_data_to_db()- Persist raw data - ✅
is_data_already_processed()- Check for duplicates
OAuth Implementation
OAuth Implementation
根据 Provider 的要求选择 OAuth 版本:OAuth 1.0 (e.g., Garmin):
- Request token
- User authorization
- Access token exchange
- Authorization code
- Token exchange
- Token refresh handling
Data Mapping
Data Mapping
Testing
Testing
充分测试你的 Provider:
- Unit tests for each method
- OAuth flow integration tests
- Data transformation tests
- Error handling tests
Key Concepts
Provider Lifecycle
Data Flow
- Raw Data Fetch:从 vendor API 拉取数据
- Raw Data Storage:保存到 provider 专用表
- Transformation:转换为标准指标
- Standard Storage:写入
th_series_data表 - Deduplication:跳过已处理记录
Configuration
在config.yaml 中添加 Provider 配置:
config.yaml
Example: Minimal Provider
Here’s a minimal provider implementation:Detailed Documentation
For comprehensive implementation details, refer to these guides:OAuth Implementation
Implement OAuth 1.0 or 2.0 flows
Data Mapping
Transform vendor data to standard format
Provider Testing
Test your provider integration
Full Integration Guide
Complete guide in repository
Standard Health Indicators
Map your provider’s data to these standard indicators:| Indicator | Description | Unit |
|---|---|---|
DAILY_STEPS | Daily step count | steps |
DAILY_SLEEP_DURATION | Total sleep time | milliseconds |
HEART_RATE | Heart rate measurement | bpm |
DAILY_HEART_RATE_RESTING | Resting heart rate | bpm |
HRV | Heart rate variability | ms |
WEIGHT | Body weight | grams |
DAILY_CALORIES_ACTIVE | Active calories burned | kcal |
SLEEP_EFFICIENCY | Sleep efficiency | percentage |
Contributing Your Provider
Once you’ve built a provider, consider contributing it back:1
Test thoroughly
Ensure all OAuth flows work and data transforms correctly
2
Document your provider
Add configuration instructions and supported metrics
3
Submit pull request
Follow the Contributing Guide
See the complete Provider Integration Guide in the repository for detailed implementation instructions, including all required methods, OAuth patterns, data transformation examples, and best practices.