Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Access your Fitbit data directly from your terminal 💻. View 💤 sleep logs,
| [Get AZM Time Series by Interval](https://dev.fitbit.com/build/reference/web-api/active-zone-minutes-timeseries/get-azm-timeseries-by-interval/) | ✅ |
| [Get Breathing Rate Summary by Interval](https://dev.fitbit.com/build/reference/web-api/breathing-rate/get-br-summary-by-interval/) | ✅ |
| [Get Daily Activity Summary](https://dev.fitbit.com/build/reference/web-api/activity/get-daily-activity-summary/) | ✅ |
| [Get HRV Summary by Interval](https://dev.fitbit.com/build/reference/web-api/heartrate-variability/get-hrv-summary-by-interval/) | ✅ |

## Usage Guide

Expand All @@ -45,7 +46,7 @@ python -m pip install fitbit-cli
```bash
fitbit-cli -h
usage: fitbit-cli [-h] [-i] [-j] [-r] [-s [DATE[,DATE]|RELATIVE]] [-o [DATE[,DATE]|RELATIVE]] [-e [DATE[,DATE]|RELATIVE]] [-a [DATE[,DATE]|RELATIVE]]
[-b [DATE[,DATE]|RELATIVE]] [-t [DATE[,DATE]|RELATIVE]] [-u] [-d] [-v]
[-b [DATE[,DATE]|RELATIVE]] [-t [DATE[,DATE]|RELATIVE]] [-H [DATE[,DATE]|RELATIVE]] [-u] [-d] [-v]

Fitbit CLI -- Access your Fitbit data at your terminal.

Expand Down Expand Up @@ -73,6 +74,8 @@ APIs:
Show Breathing Rate Summary by Interval.
-t, --activities [DATE[,DATE]|RELATIVE]
Show Daily Activity Summary.
-H, --hrv [DATE[,DATE]|RELATIVE]
Show HRV Summary by Interval.
-u, --user-profile Show Profile.
-d, --devices Show Devices.
```
Expand Down
2 changes: 1 addition & 1 deletion fitbit_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
fitbit_cli Module
"""

__version__ = "1.6.0"
__version__ = "1.7.0"
9 changes: 9 additions & 0 deletions fitbit_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def parse_arguments():
metavar="DATE[,DATE]|RELATIVE",
help="Show Breathing Rate Summary by Interval.",
)
group.add_argument(
"-H",
"--hrv",
type=parse_date_range,
nargs="?",
const=(datetime.today().date(), None),
metavar="DATE[,DATE]|RELATIVE",
help="Show HRV Summary by Interval.",
)
group.add_argument(
"-t",
"--activities",
Expand Down
8 changes: 8 additions & 0 deletions fitbit_cli/fitbit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def get_breathing_rate_intraday(self, start_date, end_date=None):
response = self.make_request("GET", url)
return response.json()

def get_hrv_summary(self, start_date, end_date=None):
"""Get HRV Summary by Interval and Date"""

date_range = f"{start_date}/{end_date}" if end_date else start_date
url = f"https://api.fitbit.com/1/user/-/hrv/date/{date_range}.json"
response = self.make_request("GET", url)
return response.json()

def get_daily_activity_summary(self, start_date):
"""Get Daily Activity Summary"""

Expand Down
32 changes: 32 additions & 0 deletions fitbit_cli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,38 @@ def display_breathing_rate(breathing_rate_data, as_json=False):
return None


def display_hrv(hrv_data, as_json=False):
"""HRV data formatter"""

if as_json:
return {
"hrv": [
{
"date": h.get("dateTime"),
"daily_rmssd": h.get("value", {}).get("dailyRmssd"),
"deep_rmssd": h.get("value", {}).get("deepRmssd"),
}
for h in hrv_data.get("hrv", [])
]
}

table = Table(title="HRV Data Summary :heartpulse:", show_header=True)

table.add_column("Date :calendar:")
table.add_column("Daily RMSSD :chart_with_upwards_trend:")
table.add_column("Deep RMSSD :sleeping:")

for hrv in hrv_data.get("hrv", []):
table.add_row(
hrv.get("dateTime", "N/A"),
str(hrv.get("value", {}).get("dailyRmssd", "N/A")),
str(hrv.get("value", {}).get("deepRmssd", "N/A")),
)

CONSOLE.print(table)
return None


def display_devices(devices, as_json=False):
"""Devices list formatter"""

Expand Down
6 changes: 6 additions & 0 deletions fitbit_cli/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def json_display(fitbit, args):
fitbit.get_breathing_rate_summary(*args.breathing_rate), as_json=True
)
)
if args.hrv:
result.update(fmt.display_hrv(fitbit.get_hrv_summary(*args.hrv), as_json=True))
if args.activities:
activity_data = collect_activities(fitbit, args)
if profile is None:
Expand Down Expand Up @@ -102,6 +104,8 @@ def raw_json_display(fitbit, args):
result["breathing_rate"] = fitbit.get_breathing_rate_summary(
*args.breathing_rate
)
if args.hrv:
result["hrv"] = fitbit.get_hrv_summary(*args.hrv)
if args.activities:
result["activities"] = collect_activities(fitbit, args)

Expand Down Expand Up @@ -129,6 +133,8 @@ def table_display(fitbit, args):
fmt.display_breathing_rate(
fitbit.get_breathing_rate_summary(*args.breathing_rate)
)
if args.hrv:
fmt.display_hrv(fitbit.get_hrv_summary(*args.hrv))
if args.activities:
activity_data = collect_activities(fitbit, args)
if profile is None:
Expand Down
38 changes: 38 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,44 @@ def test_init_auth_alone_parses_successfully(self):
args = parse_arguments()
self.assertTrue(args.init_auth)

@patch("sys.argv", ["fitbit-cli", "--hrv"])
def test_hrv_flag_parses_successfully(self):
"""Test that --hrv flag parses without error."""
args = parse_arguments()
self.assertIsNotNone(args.hrv)

@patch("sys.argv", ["fitbit-cli", "--json", "--hrv"])
def test_hrv_with_json_flag_parses_successfully(self):
"""Test that --hrv combined with --json parses without error."""
args = parse_arguments()
self.assertTrue(args.json)
self.assertIsNotNone(args.hrv)

@patch("sys.argv", ["fitbit-cli", "--raw-json", "--hrv"])
def test_hrv_with_raw_json_flag_parses_successfully(self):
"""Test that --hrv combined with --raw-json parses without error."""
args = parse_arguments()
self.assertTrue(args.raw_json)
self.assertIsNotNone(args.hrv)

@patch("sys.argv", ["fitbit-cli", "--hrv", "2024-01-01"])
def test_hrv_with_single_date_parses_successfully(self):
"""Test that --hrv with a single date parses without error."""
args = parse_arguments()
self.assertIsNotNone(args.hrv)

@patch("sys.argv", ["fitbit-cli", "--hrv", "2024-01-01,2024-01-07"])
def test_hrv_with_date_range_parses_successfully(self):
"""Test that --hrv with a date range parses without error."""
args = parse_arguments()
self.assertIsNotNone(args.hrv)

@patch("sys.argv", ["fitbit-cli", "--hrv", "last-week"])
def test_hrv_with_relative_date_parses_successfully(self):
"""Test that --hrv with a relative date parses without error."""
args = parse_arguments()
self.assertIsNotNone(args.hrv)


if __name__ == "__main__":
unittest.main()