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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ cached session, so it also overrides a stale cached timezone.

All date parameters use `YYYY-MM-DD` format and default to today when omitted.

### Diary groups

`add_food_entry`, `add_note`, and `add_exercise` accept `diary_group="auto"`
or an enabled group name from `get_diary_groups`. Names match ignoring case
and whitespace, in any language. Group 0 and Uncategorized are writable;
duplicate enabled names must be made distinct in Cronometer.

`"auto"` requires unique enabled groups named Breakfast, Lunch, Dinner, and
Snacks, in any positions. It picks by the entry's time in the account timezone
(`time=` for food and notes, or now). If any of those names is missing,
disabled, or ambiguous, choose an explicit group name. Missing profile prefs
use compatibility defaults; explicit names and visibility flags take precedence.

Python client callers: omit `diary_group` or pass `None` for automatic selection.
An integer, including `0`, always selects that actual group number.

### Relative dates and recent days

`get_food_log`, `get_daily_nutrition`, and `get_nutrition_scores` also accept
Expand Down
120 changes: 69 additions & 51 deletions src/cronometer_api_mcp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import struct
import threading
import time
import unicodedata
from datetime import date, datetime, timedelta
from datetime import time as dtime
from pathlib import Path
Expand Down Expand Up @@ -148,7 +149,7 @@
TARGET_ROW_IDS = {"protein_g": 203, "fat_g": 204, "net_carbs_g": -1205}
SODIUM_ID = 307

# Cronometer's defaults when DGnn names or DGnnON visibility prefs are unset.
# Compatibility defaults for missing DGnn names or DGnnON visibility prefs.
_DIARY_GROUP_DEFAULTS = (
("Uncategorized", True),
("Breakfast", True),
Expand Down Expand Up @@ -1609,7 +1610,7 @@ def add_serving(
grams: float,
translation_id: int = 0,
day: date | None = None,
diary_group: int = 0,
diary_group: int | None = None,
at: dtime | None = None,
) -> dict:
"""Log a food serving to the diary.
Expand All @@ -1623,12 +1624,10 @@ def add_serving(
grams: Weight in grams.
translation_id: Translation ID (from search results, usually 0).
day: Date to log to. Defaults to today.
diary_group: Meal group number. 0 = auto: the group is picked by
time of day as 1-4 (see _meal_group_for_hour), which
is Breakfast/Lunch/Dinner/Snacks only on an account
with the default groups. Otherwise the number from
get_diary_groups; resolve_diary_group maps a name to
it.
diary_group: Group number from get_diary_groups, including 0.
None picks by time of day and requires unique enabled
Breakfast/Lunch/Dinner/Snacks names in the account.
resolve_diary_group maps an explicit name to a number.
at: Time of day to stamp the entry with, in the account timezone.
Defaults to now. Also drives the auto meal group.

Expand All @@ -1638,8 +1637,8 @@ def add_serving(
day_str = self._format_day(day)
time_str = f"{when.hour}:{when.minute}:{when.second}"

if diary_group == 0:
diary_group = _meal_group_for_hour(when.hour)
if diary_group is None:
diary_group = self._auto_diary_group_for_hour(when.hour)

serving = {
"order": (diary_group << 16) | 1,
Expand Down Expand Up @@ -1679,7 +1678,7 @@ def add_note(
self,
text: str,
day: date | None = None,
diary_group: int = 0,
diary_group: int | None = None,
at: dtime | None = None,
) -> dict:
"""Add a note to a diary day.
Expand All @@ -1688,16 +1687,16 @@ def add_note(
text: The note text.
day: Date to add it to. Defaults to today.
diary_group: Meal group number the note sits in, as for
add_serving (0 = auto, by time of day).
add_serving (None = auto, by time of day; 0 is a real group).
at: Time of day to stamp the note with, in the account timezone.
Defaults to now. Also drives the auto meal group.

Returns the stored note: {"noteId", "text", "day", "time", "order", ...}.
"""
now = self.now()
when = at or now.time()
if diary_group == 0:
diary_group = _meal_group_for_hour(when.hour)
if diary_group is None:
diary_group = self._auto_diary_group_for_hour(when.hour)
# The UTC offset in force on the note's own date and time, in the
# account's zone (not now's offset: they differ across a DST change).
stamped = datetime.combine(day or now.date(), when, tzinfo=self._tzinfo())
Expand Down Expand Up @@ -2482,11 +2481,11 @@ def get_diary_groups(self) -> list[dict]:
Group names are account settings stored as DG01..DG08 profile prefs
(name) with a DGnnON twin (visibility). A diary entry's `order` carries
its group in the bits above 16 (`order >> 16`), and that index is the
pref number minus one: DG01 is group 0 (where exercise and biometrics
sit), DG02 is group 1, and so on. The names and count are customizable,
pref number minus one: DG01 is group 0, DG02 is group 1, and so on.
The names and count are customizable,
so "lunch" is not always group 2 -- resolve names through this list
instead of assuming the default Breakfast/Lunch/Dinner/Snacks layout.
Unset names and visibility flags use Cronometer's defaults: groups
Unset names and visibility flags use compatibility defaults: groups
0-4 are enabled and groups 5-7 are hidden.

Returns [{"group": int, "pref": "DG04", "name": str, "enabled": bool}].
Expand Down Expand Up @@ -2526,36 +2525,35 @@ def _diary_groups_for_lookup(self) -> tuple[list[dict], bool]:
def resolve_diary_group(self, diary_group: str) -> dict:
"""Turn a diary group name into the group number the write calls take.

"auto" (any case) resolves to 0, which add_serving, add_note and
"auto" (any case) resolves to None, which add_serving, add_note and
add_exercise treat as "pick by time of day". Any other value is
matched, case-insensitively, against the names of the account's
matched, ignoring case and whitespace, against the names of the account's
enabled groups (see get_diary_groups), so "dinner" is group 4 on an
account with a "Post-workout" slot and 3 on one without.

A name that isn't in the cached list is looked up again once before
failing, so groups renamed or added in the app work without a restart.

Raises ValueError for an unknown, disabled or ambiguous name, and for
the group numbered 0 (Uncategorized by default): 0 is the "auto"
sentinel everywhere below, so it cannot be addressed by name.
Raises ValueError for an unknown, disabled or ambiguous name.
Group 0 is writable like every other enabled group.

Returns {"group": int, "name": str}; "auto" gives {"group": 0, "name":
Returns {"group": int, "name": str}; "auto" gives {"group": None, "name":
"auto"}.
"""
key = diary_group.strip().lower()
key = _diary_group_key(diary_group)
if key == "auto":
return {"group": 0, "name": "auto"}
return {"group": None, "name": "auto"}

def find(groups: list[dict]) -> list[dict]:
return [g for g in groups if g["name"].strip().lower() == key]
return [g for g in groups if _diary_group_key(g["name"]) == key]

groups, fetched = self._diary_groups_for_lookup()
matches = find(groups)
if not matches and not fetched:
groups = self.get_diary_groups()
matches = find(groups)

writable = [g for g in matches if g["enabled"] and g["group"] != 0]
writable = [g for g in matches if g["enabled"]]
if len(writable) > 1:
numbers = ", ".join(str(g["group"]) for g in writable)
raise ValueError(
Expand All @@ -2566,23 +2564,43 @@ def find(groups: list[dict]) -> list[dict]:
match = writable[0]
return {"group": match["group"], "name": match["name"]}
if not matches:
names = ["auto"] + [
g["name"] for g in groups if g["enabled"] and g["group"] != 0
]
names = ["auto"] + [g["name"] for g in groups if g["enabled"]]
raise ValueError(
f"Invalid diary_group '{diary_group}'. "
f"Must be one of: {', '.join(names)}."
)
match = matches[0]
if not match["enabled"]:
raise ValueError(
f"Diary group '{match['name']}' is disabled in the account."
)
raise ValueError(
f"Diary group '{match['name']}' is group 0, which means "
'"auto" here and cannot be chosen by name.'
f"Diary group '{matches[0]['name']}' is disabled in the account."
)

def _auto_diary_group_for_hour(self, hour: int) -> int:
"""Resolve all four meal names from one account layout before picking."""

def find(groups: list[dict]) -> dict[str, list[int]]:
meals: dict[str, list[int]] = {
name: [] for name in ("breakfast", "lunch", "dinner", "snacks")
}
for group in groups:
key = _diary_group_key(group["name"])
if group["enabled"] and key in meals:
meals[key].append(group["group"])
return meals

groups, fetched = self._diary_groups_for_lookup()
meals = find(groups)
if any(len(matches) != 1 for matches in meals.values()) and not fetched:
groups = self.get_diary_groups()
meals = find(groups)
if any(len(matches) != 1 for matches in meals.values()):
names = ", ".join(g["name"] for g in groups if g["enabled"]) or "(none)"
raise ValueError(
"Cannot resolve diary_group 'auto': requires unique enabled "
"Breakfast, Lunch, Dinner and Snacks groups. "
f"Available groups: {names}. "
"Choose an explicit diary_group from get_diary_groups."
)
return meals[_meal_name_for_hour(hour)][0]

def get_macro_split(self) -> dict:
"""Saved macro ratios as percentages of energy.

Expand Down Expand Up @@ -2856,7 +2874,7 @@ def add_exercise(
minutes: float,
calories: float,
day: date | None = None,
diary_group: int = 0,
diary_group: int | None = None,
) -> dict:
"""Log an exercise with a given calorie burn.

Expand All @@ -2871,7 +2889,7 @@ def add_exercise(
minutes: Duration in minutes.
calories: Calories burned (a positive number).
day: Date to log to. Defaults to today.
diary_group: Meal group, as for add_serving (0 = auto).
diary_group: Meal group, as for add_serving (None = auto).

Uses: POST /api/v2/add_exercise

Expand All @@ -2881,8 +2899,8 @@ def add_exercise(
raise ValueError(f"minutes must be positive, got {minutes}")
if not math.isfinite(calories) or calories < 0:
raise ValueError(f"calories must be zero or positive, got {calories}")
if diary_group == 0:
diary_group = _meal_group_for_hour(self.now().hour)
if diary_group is None:
diary_group = self._auto_diary_group_for_hour(self.now().hour)

exercise = {
"exerciseId": None,
Expand Down Expand Up @@ -2997,18 +3015,18 @@ def _v3_diary_entry(entry: dict) -> dict:
return out


def _meal_group_for_hour(hour: int) -> int:
"""Map hour of day to a Cronometer diary meal group number.
def _diary_group_key(name: str) -> str:
"""Match account names without translating them or discarding accents."""
return "".join(unicodedata.normalize("NFC", name).casefold().split())

Returns 1-4, which are Breakfast, Lunch, Dinner and Snacks only on an
account with the default groups; it does not consult the account's own
group names. This is what "auto" uses.
"""

def _meal_name_for_hour(hour: int) -> str:
"""Pick a meal name; its group number comes from the account's prefs."""
if 4 <= hour < 10:
return 1 # Breakfast
return "breakfast"
elif 10 <= hour < 14:
return 2 # Lunch
return "lunch"
elif 14 <= hour < 21:
return 3 # Dinner
return "dinner"
else:
return 4 # Snacks
return "snacks"
19 changes: 12 additions & 7 deletions src/cronometer_api_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _group_echo(group: dict) -> dict:
Only for an explicitly named group: "auto" is chosen from the time of day
inside the client, so there is nothing resolved to report.
"""
if group["group"] == 0:
if group["group"] is None:
return {}
return {"diary_group": group}

Expand Down Expand Up @@ -273,8 +273,8 @@ def get_diary_groups() -> str:
`group` here is that number, so an entry with order 196609 is in group 3.

Returns each group as {group, pref, name, enabled}, in diary order. Group 0
is where exercise and biometrics entries sit. A group with enabled=false is
hidden in the app, so no entries are expected there.
is a regular slot whose name depends on the account. A group with
enabled=false is hidden in the app but may still contain older entries.
"""
try:
client = _get_client()
Expand Down Expand Up @@ -335,8 +335,9 @@ def add_food_entry(
translation_id: Translation ID from search results (usually 0).
diary_group: Meal slot: "auto" (default, picks by time of day) or the
name of one of the account's diary groups, matched
case-insensitively. Group names are customizable; call
get_diary_groups to see them.
ignoring case and whitespace. "auto" requires unique
enabled Breakfast, Lunch, Dinner and Snacks groups;
otherwise choose an explicit name from get_diary_groups.
time: Time of day to log at, 24-hour HH:MM or HH:MM:SS in the account
timezone (defaults to now). With diary_group "auto", this also
picks the meal slot, so logging lunch late still lands in lunch.
Expand Down Expand Up @@ -418,7 +419,9 @@ def add_note(
date: Date as YYYY-MM-DD (defaults to today).
diary_group: Meal slot the note sits in: "auto" (default, picks by
time of day) or the name of one of the account's diary groups,
matched case-insensitively (see get_diary_groups).
matched ignoring case and whitespace. "auto" requires unique
enabled Breakfast, Lunch, Dinner and Snacks groups; otherwise
choose an explicit name from get_diary_groups.
time: Time of day for the note, 24-hour HH:MM or HH:MM:SS in the
account timezone (defaults to now). With diary_group "auto", this
also picks the meal slot.
Expand Down Expand Up @@ -1388,7 +1391,9 @@ def add_exercise(
date: Date as YYYY-MM-DD (defaults to today).
diary_group: Meal slot: "auto" (default, picks by time of day) or the
name of one of the account's diary groups, matched
case-insensitively (see get_diary_groups).
ignoring case and whitespace. "auto" requires unique enabled
Breakfast, Lunch, Dinner and Snacks groups; otherwise choose an
explicit name from get_diary_groups.
"""
try:
client = _get_client()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ def test_add_serving_cold_start_embeds_real_user_id(tmp_path):
"""First write on a cold client logs in once and sends the real userId."""
client, state = make_cold_client(tmp_path, [{"result": "SUCCESS", "id": 7}])

client.add_serving(food_id=1, measure_id=0, grams=100.0)
client.add_serving(food_id=1, measure_id=0, grams=100.0, diary_group=0)

assert state["login"] == 1
assert state["post"] == 1 # no stale-payload double failure (#31)
assert state["payloads"][0]["serving"]["userId"] == 42
assert state["payloads"][0]["serving"]["order"] == 1


def test_user_id_property_triggers_login(tmp_path):
Expand Down
Loading
Loading