From 49fd08c554e45a6bf2b330b9f28af913463d41df Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 16 Nov 2026 13:22:34 -0800 Subject: [PATCH] Add nutrition macronutrient sensors to Google Health (#192853) --- .../google_health/quality_scale.yaml | 4 +- .../components/google_health/sensor.py | 94 ++++ .../components/google_health/strings.json | 21 + .../google_health/fixtures/nutrition.json | 37 +- .../google_health/snapshots/test_sensor.ambr | 409 ++++++++++++++++++ tests/components/google_health/test_sensor.py | 122 ++++-- 6 files changed, 656 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/google_health/quality_scale.yaml b/homeassistant/components/google_health/quality_scale.yaml index b45264f2ff40..faac91dd42eb 100644 --- a/homeassistant/components/google_health/quality_scale.yaml +++ b/homeassistant/components/google_health/quality_scale.yaml @@ -66,9 +66,7 @@ rules: dynamic-devices: done entity-category: done entity-device-class: done - entity-disabled-by-default: - status: exempt - comment: No entities need to be disabled by default. + entity-disabled-by-default: done entity-translations: done exception-translations: done icon-translations: done diff --git a/homeassistant/components/google_health/sensor.py b/homeassistant/components/google_health/sensor.py index 44b49873ea32..48812f93b68b 100644 --- a/homeassistant/components/google_health/sensor.py +++ b/homeassistant/components/google_health/sensor.py @@ -43,6 +43,7 @@ from .coordinator import ( GoogleHealthDataUpdateCoordinator, GoogleHealthDeviceCoordinator, GoogleHealthNutritionCoordinator, + GoogleHealthNutritionData, GoogleHealthSleepCoordinator, GoogleHealthSleepData, ) @@ -71,6 +72,20 @@ def _sleep_stage_minutes( ) +def _nutrient_grams(data: GoogleHealthNutritionData | None, nutrient: str) -> float: + """Return daily intake in grams for a nutrient type.""" + if not data or not data.nutrition: + return 0.0 + return next( + ( + item.quantity.grams_sum + for item in data.nutrition.nutrients + if item.nutrient == nutrient + ), + 0.0, + ) + + @dataclass(frozen=True, kw_only=True) class GoogleHealthSensorEntityDescription[ _CoordinatorT: GoogleHealthDataUpdateCoordinator[Any], @@ -316,6 +331,85 @@ NUTRITION_SENSORS: list[ else 0.0 ), ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="total_fat", + translation_key="total_fat", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_display_precision=1, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: ( + data.nutrition.total_fat.grams_sum + if data and data.nutrition and data.nutrition.total_fat + else 0.0 + ), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="total_carbohydrate", + translation_key="total_carbohydrate", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_display_precision=1, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: ( + data.nutrition.total_carbohydrate.grams_sum + if data and data.nutrition and data.nutrition.total_carbohydrate + else 0.0 + ), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="protein", + translation_key="protein", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_display_precision=1, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: _nutrient_grams(data, "PROTEIN"), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="dietary_fiber", + translation_key="dietary_fiber", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_display_precision=1, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + entity_registry_enabled_default=False, + value_fn=lambda data: _nutrient_grams(data, "DIETARY_FIBER"), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="sugar", + translation_key="sugar", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_display_precision=1, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + entity_registry_enabled_default=False, + value_fn=lambda data: _nutrient_grams(data, "SUGAR"), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="sodium", + translation_key="sodium", + native_unit_of_measurement=UnitOfMass.GRAMS, + suggested_unit_of_measurement=UnitOfMass.MILLIGRAMS, + suggested_display_precision=0, + device_class=SensorDeviceClass.WEIGHT, + state_class=SensorStateClass.TOTAL_INCREASING, + entity_registry_enabled_default=False, + value_fn=lambda data: _nutrient_grams(data, "SODIUM"), + ), + GoogleHealthSensorEntityDescription[GoogleHealthNutritionCoordinator, float]( + key="calories_from_fat", + translation_key="calories_from_fat", + native_unit_of_measurement=UnitOfEnergy.KILO_CALORIE, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + entity_registry_enabled_default=False, + value_fn=lambda data: ( + data.nutrition.energy_from_fat.kcal_sum + if data and data.nutrition and data.nutrition.energy_from_fat + else 0.0 + ), + ), ] diff --git a/homeassistant/components/google_health/strings.json b/homeassistant/components/google_health/strings.json index d5ad0be885dc..cd4df687d6ed 100644 --- a/homeassistant/components/google_health/strings.json +++ b/homeassistant/components/google_health/strings.json @@ -35,6 +35,12 @@ "calories_consumed": { "name": "Calories consumed" }, + "calories_from_fat": { + "name": "Calories from fat" + }, + "dietary_fiber": { + "name": "Dietary fiber" + }, "floors": { "name": "Floors" }, @@ -44,6 +50,9 @@ "last_sync_time": { "name": "Last sync time" }, + "protein": { + "name": "Protein" + }, "resting_heart_rate": { "name": "Resting heart rate" }, @@ -77,12 +86,24 @@ "sleep_to_fall_asleep": { "name": "Time to fall asleep" }, + "sodium": { + "name": "Sodium" + }, "steps": { "name": "Steps", "unit_of_measurement": "steps" }, + "sugar": { + "name": "Sugar" + }, "total_calories": { "name": "Total calories" + }, + "total_carbohydrate": { + "name": "Total carbohydrates" + }, + "total_fat": { + "name": "Total fat" } } }, diff --git a/tests/components/google_health/fixtures/nutrition.json b/tests/components/google_health/fixtures/nutrition.json index fd38dadc4180..8ac07c156bab 100644 --- a/tests/components/google_health/fixtures/nutrition.json +++ b/tests/components/google_health/fixtures/nutrition.json @@ -4,7 +4,42 @@ "nutritionLog": { "energy": { "kcalSum": 1800.0 - } + }, + "energyFromFat": { + "kcalSum": 585.0 + }, + "totalFat": { + "gramsSum": 65.0 + }, + "totalCarbohydrate": { + "gramsSum": 210.5 + }, + "nutrients": [ + { + "nutrient": "PROTEIN", + "quantity": { + "gramsSum": 95.2 + } + }, + { + "nutrient": "DIETARY_FIBER", + "quantity": { + "gramsSum": 28.4 + } + }, + { + "nutrient": "SUGAR", + "quantity": { + "gramsSum": 42.1 + } + }, + { + "nutrient": "SODIUM", + "quantity": { + "gramsSum": 1.85 + } + } + ] }, "civilStartTime": { "date": { "year": 2026, "month": 6, "day": 28 } }, "civilEndTime": { "date": { "year": 2026, "month": 6, "day": 29 } } diff --git a/tests/components/google_health/snapshots/test_sensor.ambr b/tests/components/google_health/snapshots/test_sensor.ambr index c2536624c4c6..13988c6ce7c2 100644 --- a/tests/components/google_health/snapshots/test_sensor.ambr +++ b/tests/components/google_health/snapshots/test_sensor.ambr @@ -381,6 +381,64 @@ 'state': '1800.0', }) # --- +# name: test_all_entities[sensor.google_health_calories_from_fat-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_calories_from_fat', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Calories from fat', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Calories from fat', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'calories_from_fat', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_calories_from_fat', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_calories_from_fat-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'energy', + : 'Google Health Calories from fat', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_calories_from_fat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '585.0', + }) +# --- # name: test_all_entities[sensor.google_health_deep_sleep-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -442,6 +500,64 @@ 'state': '1.5', }) # --- +# name: test_all_entities[sensor.google_health_dietary_fiber-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_dietary_fiber', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Dietary fiber', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Dietary fiber', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'dietary_fiber', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_dietary_fiber', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_dietary_fiber-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Dietary fiber', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_dietary_fiber', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '28.4', + }) +# --- # name: test_all_entities[sensor.google_health_distance-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -617,6 +733,64 @@ 'state': '4.0', }) # --- +# name: test_all_entities[sensor.google_health_protein-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_protein', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Protein', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Protein', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'protein', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_protein', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_protein-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Protein', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_protein', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '95.2', + }) +# --- # name: test_all_entities[sensor.google_health_rem_sleep-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -834,6 +1008,67 @@ 'state': '2026-06-29T22:00:00+00:00', }) # --- +# name: test_all_entities[sensor.google_health_sodium-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_sodium', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Sodium', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Sodium', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sodium', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_sodium', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_sodium-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Sodium', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_sodium', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1850.0', + }) +# --- # name: test_all_entities[sensor.google_health_steps-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -888,6 +1123,64 @@ 'state': '10500', }) # --- +# name: test_all_entities[sensor.google_health_sugar-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_sugar', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Sugar', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Sugar', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sugar', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_sugar', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_sugar-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Sugar', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_sugar', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '42.1', + }) +# --- # name: test_all_entities[sensor.google_health_time_after_waking_up-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -1245,6 +1538,122 @@ 'state': '2100.2', }) # --- +# name: test_all_entities[sensor.google_health_total_carbohydrates-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_total_carbohydrates', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total carbohydrates', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total carbohydrates', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'total_carbohydrate', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_total_carbohydrate', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_total_carbohydrates-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Total carbohydrates', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_total_carbohydrates', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '210.5', + }) +# --- +# name: test_all_entities[sensor.google_health_total_fat-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.google_health_total_fat', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total fat', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total fat', + 'platform': 'google_health', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'total_fat', + 'unique_id': '01J0BC4QM2YBRP6H5G933CETT7_total_fat', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.google_health_total_fat-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'weight', + : 'Google Health Total fat', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.google_health_total_fat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '65.0', + }) +# --- # name: test_all_entities[sensor.google_health_water_intake-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ diff --git a/tests/components/google_health/test_sensor.py b/tests/components/google_health/test_sensor.py index c370f99af428..5f8f57a8eec1 100644 --- a/tests/components/google_health/test_sensor.py +++ b/tests/components/google_health/test_sensor.py @@ -9,7 +9,10 @@ from google_health_api.const import HealthApiScope from google_health_api.model import ( SLEEP, ListPairedDevicesResult, + NutrientQuantityRollup, + NutritionLogRollupValue, StageSummary, + WeightQuantityRollup, _ListPairedDevicesModel, ) import pytest @@ -22,7 +25,7 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.google_health.const import DOMAIN from homeassistant.components.google_health.coordinator import DEVICE_POLLING_INTERVAL -from homeassistant.const import STATE_UNKNOWN, Platform +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.unit_system import ( @@ -31,10 +34,12 @@ from homeassistant.util.unit_system import ( UnitSystem, ) -from .conftest import _list_fixture, paired_devices_fixture +from .conftest import _list_fixture, _rollup_fixture, paired_devices_fixture -@pytest.mark.usefixtures("mock_google_health_client") +@pytest.mark.usefixtures( + "entity_registry_enabled_by_default", "mock_google_health_client" +) async def test_all_entities( hass: HomeAssistant, snapshot: SnapshotAssertion, @@ -49,6 +54,35 @@ async def test_all_entities( await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) +@pytest.mark.usefixtures("mock_google_health_client") +async def test_sensor_disabled_by_default( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry: MockConfigEntry, + integration_setup: Callable[[], Awaitable[bool]], +) -> None: + """Test default enabled and disabled states for all sensor entities.""" + with patch("homeassistant.components.google_health._PLATFORMS", [Platform.SENSOR]): + assert await integration_setup() + + entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id) + expected_disabled = { + "sensor.google_health_dietary_fiber": er.RegistryEntryDisabler.INTEGRATION, + "sensor.google_health_sugar": er.RegistryEntryDisabler.INTEGRATION, + "sensor.google_health_sodium": er.RegistryEntryDisabler.INTEGRATION, + "sensor.google_health_calories_from_fat": er.RegistryEntryDisabler.INTEGRATION, + } + assert { + entry.entity_id: entry.disabled_by + for entry in entries + if entry.disabled_by is not None + } == expected_disabled + assert { + entry.entity_id for entry in entries if hass.states.get(entry.entity_id) is None + } == set(expected_disabled) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_sensor_empty_rollup( hass: HomeAssistant, mock_google_health_client: AsyncMock, @@ -65,33 +99,65 @@ async def test_sensor_empty_rollup( assert await integration_setup() - steps_state = hass.states.get("sensor.google_health_steps") - assert steps_state is not None - assert steps_state.state == "0" + expected_states = { + "sensor.google_health_steps": "0", + "sensor.google_health_distance": "0.0", + "sensor.google_health_active_calories": "0.0", + "sensor.google_health_total_calories": "0.0", + "sensor.google_health_floors": "0", + "sensor.google_health_water_intake": "0.0", + "sensor.google_health_calories_consumed": "0.0", + "sensor.google_health_total_fat": "0.0", + "sensor.google_health_total_carbohydrates": "0.0", + "sensor.google_health_protein": "0.0", + "sensor.google_health_dietary_fiber": "0.0", + "sensor.google_health_sugar": "0.0", + "sensor.google_health_sodium": "0.0", + "sensor.google_health_calories_from_fat": "0.0", + } + assert { + entity_id: getattr(hass.states.get(entity_id), "state", None) + for entity_id in expected_states + } == expected_states - distance_state = hass.states.get("sensor.google_health_distance") - assert distance_state is not None - assert distance_state.state == "0.0" - active_calories_state = hass.states.get("sensor.google_health_active_calories") - assert active_calories_state is not None - assert active_calories_state.state == "0.0" +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensor_nutrition_partial_rollup( + hass: HomeAssistant, + mock_google_health_client: AsyncMock, + integration_setup: Callable[[], Awaitable[bool]], +) -> None: + """Test nutrition sensors when rollup contains only a subset of fields and nutrients.""" + nutrition_rollup = _rollup_fixture( + "nutrition.json", NutritionLogRollupValue, "nutritionLog" + ) + assert nutrition_rollup is not None + nutrition_rollup.data = NutritionLogRollupValue( + nutrients=[ + NutrientQuantityRollup( + nutrient="PROTEIN", + quantity=WeightQuantityRollup(grams_sum=30.5), + ) + ] + ) + mock_google_health_client.nutrition_log.today.return_value = nutrition_rollup - total_calories_state = hass.states.get("sensor.google_health_total_calories") - assert total_calories_state is not None - assert total_calories_state.state == "0.0" + assert await integration_setup() - floors_state = hass.states.get("sensor.google_health_floors") - assert floors_state is not None - assert floors_state.state == "0" - - hydration_state = hass.states.get("sensor.google_health_water_intake") - assert hydration_state is not None - assert hydration_state.state == "0.0" - - calories_consumed_state = hass.states.get("sensor.google_health_calories_consumed") - assert calories_consumed_state is not None - assert calories_consumed_state.state == "0.0" + expected_states = { + "sensor.google_health_calories_consumed": "0.0", + "sensor.google_health_total_fat": "0.0", + "sensor.google_health_total_carbohydrates": "0.0", + "sensor.google_health_protein": "30.5", + "sensor.google_health_dietary_fiber": "0.0", + "sensor.google_health_sugar": "0.0", + "sensor.google_health_sodium": "0.0", + "sensor.google_health_calories_from_fat": "0.0", + } + assert { + entity_id: getattr(hass.states.get(entity_id), "state", None) + for entity_id in expected_states + } == expected_states async def test_sensor_empty_sleep( @@ -217,6 +283,7 @@ async def test_sensor_only_in_progress_sleep( "sensor.google_health_weight": (pytest.approx(80.0), "kg"), "sensor.google_health_distance": (pytest.approx(5.0), "km"), "sensor.google_health_water_intake": (pytest.approx(2.5), "L"), + "sensor.google_health_total_fat": (pytest.approx(65.0), "g"), }, id="metric", ), @@ -232,6 +299,7 @@ async def test_sensor_only_in_progress_sleep( pytest.approx(84.54, abs=1e-1), "fl. oz.", ), + "sensor.google_health_total_fat": (pytest.approx(65.0), "g"), }, id="us_customary", ), @@ -253,7 +321,7 @@ async def test_sensor_unit_conversions( state = hass.states.get(entity_id) assert state is not None assert float(state.state) == expected_state - assert state.attributes.get("unit_of_measurement") == expected_unit + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit @pytest.mark.parametrize(