Compare commits

...

8 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
07c9e3ea86 Fix docstring indentation to follow Google-style format
Co-authored-by: cyberjunky <5447161+cyberjunky@users.noreply.github.com>
2025-11-06 15:21:06 +00:00
copilot-swe-agent[bot]
62da8905c8 Initial plan 2025-11-06 15:14:35 +00:00
Ron Klinkien
a34dd7abfc Fix KeyError for userProfileId and add reload support
- Fix KeyError when userProfileId is missing from summary after HA updates
- Check if userProfileId exists before accessing it in 3 locations
- Remove duplicate get_gear() call, reuse data from first fetch
- Add async_reload_entry() function for reload without restart
- Update README with reload instructions
- Improves integration reliability and user experience
2025-11-06 15:49:58 +01:00
Ron Klinkien
c73065a7b4 Better migration 2025-11-05 08:56:22 +00:00
Ron Klinkien
b1e8953393 Fix enable/disable of entitites 2025-11-04 21:09:32 +00:00
Ron Klinkien
a2aebe7790 Sections 2025-11-04 20:49:28 +00:00
Ron Klinkien
2899837881 New options 2025-11-04 21:08:04 +01:00
Ron Klinkien
1776cb4037 More robust config flow when integration is reinstalled 2025-11-04 18:03:35 +00:00
11 changed files with 1838 additions and 236 deletions

View File

@@ -25,6 +25,19 @@ After successful set up a standard set of sensors are enabled. You can enable mo
The integration will fetch new data every 5 minutes, make sure your devices sync to the Garmin Connect website.
### Reload Without Restart
This integration supports **reloading without restarting Home Assistant**:
- Go to **Settings****Devices & Services****Garmin Connect**
- Click the **three dots (⋮)****Reload**
This is useful after:
- Updating the integration via HACS
- Changing configuration options
- Troubleshooting issues
No need to restart your entire Home Assistant instance!
## Available Sensors
Not every sensor holds meaningful values, it depends on the tracking and health devices you use, or the apps you have connected.

112
SENSOR_GROUPS.md Normal file
View File

@@ -0,0 +1,112 @@
# Sensor Groups Feature
## Overview
The Garmin Connect integration now supports organizing sensors into logical groups that can be enabled or disabled together. This helps reduce clutter in your Home Assistant instance by only showing the sensors you actually use.
## Available Sensor Groups
### Enabled by Default
- **Activity & Steps**: Step counts, distance, floors climbed, and daily goals
- **Calories & Nutrition**: Calorie tracking including active, burned, consumed, and remaining
- **Heart Rate**: Heart rate monitoring including resting, min/max, and HRV status
- **Stress**: Stress levels and duration tracking
- **Sleep**: Sleep duration, quality scores, and sleep stages
- **Body Battery**: Garmin Body Battery energy monitoring
- **Hydration**: Water intake tracking and hydration goals
- **Intensity & Activity Time**: Active time, sedentary time, and intensity minutes
- **Health Monitoring**: SpO2 (blood oxygen), respiration rate, and altitude
- **Fitness & Performance**: Fitness age, endurance score, and metabolic age
### Disabled by Default
- **Body Composition**: Weight, BMI, body fat, muscle mass, and bone mass (requires compatible scale)
- **Activity Tracking**: Recent activities, badges, points, and gamification
- **Advanced Sensors**: Additional detailed metrics and alternative measurements
## How to Configure
### Initial Setup
When you first install the integration, all default sensor groups are enabled automatically. This ensures backward compatibility with previous versions.
### Changing Sensor Groups
1. Go to **Settings****Devices & Services**
2. Find the **Garmin Connect** integration
3. Click **Configure** (or the three dots menu → **Configure**)
4. Select which sensor groups you want to enable
5. Click **Submit**
6. **Reload the integration** for changes to take effect
### Per-Sensor Control
Even within enabled sensor groups, you can still disable individual sensors:
1. Go to **Settings****Devices & Services**
2. Find the **Garmin Connect** integration
3. Click on the device
4. Find the sensor you want to disable
5. Click on it and toggle **Enable entity** off
## Backward Compatibility
- **Existing installations**: If you upgrade from a previous version, all default sensor groups will be automatically enabled, maintaining your current setup
- **Configuration-less**: If you never configure sensor groups, all default groups remain enabled
- **Individual control**: The `entity_registry_enabled_default` setting on individual sensors still works within enabled groups
## Benefits
1. **Reduced Clutter**: Only see sensors relevant to your use case
2. **Performance**: Fewer entities to process in Home Assistant
3. **Organization**: Sensors are logically grouped for easier management
4. **Flexibility**: Enable/disable entire categories or individual sensors
5. **Scalability**: Easy to add new sensor categories in the future
## Examples
### Minimal Setup (Basic Activity Tracking)
Enable only:
- Activity & Steps
- Heart Rate
- Sleep
### Comprehensive Health Monitoring
Enable:
- All default groups
- Body Composition (if you have a compatible scale)
- Advanced Sensors (for detailed analysis)
### Athlete/Training Focus
Enable:
- Activity & Steps
- Heart Rate
- Stress
- Body Battery
- Intensity & Activity Time
- Fitness & Performance
## Technical Details
### Storage
Sensor group preferences are stored in the integration's options, separate from the main configuration. This allows:
- Easy reconfiguration without re-authentication
- No impact on existing authentication tokens
- Clean separation of concerns
### Implementation
- Sensor descriptions are organized into tuples by category
- A mapping dictionary links group IDs to sensor tuples
- The `get_sensors_for_groups()` function dynamically builds the sensor list
- Backward compatibility is ensured by checking for `None` options
## Future Enhancements
Potential future additions:
- Export/import sensor group configurations
- Preset configurations for different use cases
- Dynamic group enabling based on detected Garmin device capabilities
- Statistics about which groups are most commonly enabled

View File

@@ -5,20 +5,20 @@ from collections.abc import Awaitable
from datetime import datetime, timedelta
import logging
from zoneinfo import ZoneInfo
import requests
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
GarminConnectTooManyRequestsError,
)
import requests
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import requests
from .const import (
DATA_COORDINATOR,
DAY_TO_NUMBER,
@@ -39,10 +39,15 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"Migrating Garmin Connect config entry from version %s", entry.version)
if entry.version == 1:
# Check if we need to migrate (old entries have username/password, new ones have token)
if CONF_TOKEN not in entry.data and CONF_USERNAME in entry.data and CONF_PASSWORD in entry.data:
# Scenario 1: Has USERNAME + PASSWORD but no TOKEN (old auth method)
# Migrate to: ID + TOKEN
if (
CONF_TOKEN not in entry.data
and CONF_USERNAME in entry.data
and CONF_PASSWORD in entry.data
):
_LOGGER.info(
"Migrating Garmin Connect config entry to token-based authentication")
"Migrating Garmin Connect config entry from username/password to token-based authentication")
username = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
@@ -76,10 +81,54 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(
"Failed to migrate Garmin Connect config entry. "
"Please re-add the integration. Error: %s", err
"Please re-add the integration. Error: %s",
err,
)
return False
# Scenario 2: Has USERNAME + TOKEN but no ID (partially migrated)
# Migrate to: ID + TOKEN (remove USERNAME)
elif (
CONF_ID not in entry.data
and CONF_USERNAME in entry.data
and CONF_TOKEN in entry.data
):
_LOGGER.info(
"Migrating Garmin Connect config entry: converting USERNAME to ID")
username = entry.data[CONF_USERNAME]
# Create new data with ID instead of USERNAME
new_data = {
CONF_ID: username,
CONF_TOKEN: entry.data[CONF_TOKEN],
}
# Update the config entry
hass.config_entries.async_update_entry(entry, data=new_data)
_LOGGER.info(
"Successfully migrated Garmin Connect config entry from USERNAME to ID")
return True
# Scenario 3: Missing both TOKEN and credentials (incomplete/corrupted)
# Add placeholder ID to allow reauth flow
elif CONF_TOKEN not in entry.data:
if CONF_ID not in entry.data:
_LOGGER.info(
"Config entry missing CONF_ID, adding placeholder for reauth flow")
new_data = {
**entry.data,
CONF_ID: entry.entry_id, # Use entry_id as fallback
}
hass.config_entries.async_update_entry(entry, data=new_data)
_LOGGER.info(
"Garmin Connect config entry is incomplete (missing token). "
"Reauthentication will be required to complete setup."
)
return True
return True
@@ -98,9 +147,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# Register update listener to reload integration when options change
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
return True
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload the config entry when options change."""
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
@@ -109,6 +166,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
return unload_ok
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
"""Garmin Connect Data Update Coordinator."""
@@ -152,14 +215,17 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
try:
# Check if the token exists in the entry data
if CONF_TOKEN not in self.entry.data:
_LOGGER.error(
"Token not found in config entry. This may be an old config entry that needs migration. "
"Please remove and re-add the Garmin Connect integration."
_LOGGER.info(
"Token not found in config entry. Reauthentication required."
)
raise ConfigEntryAuthFailed(
"Token not found, please re-add the integration")
"Token not found in config entry. Please reauthenticate."
)
await self.hass.async_add_executor_job(self.api.login, self.entry.data[CONF_TOKEN])
except ConfigEntryAuthFailed:
# Re-raise ConfigEntryAuthFailed without catching it in the generic handler
raise
except GarminConnectAuthenticationError as err:
_LOGGER.error(
"Authentication error occurred during login: %s", err.response.text)
@@ -170,8 +236,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
return False
except GarminConnectConnectionError as err:
_LOGGER.error(
"Connection error occurred during Garmin Connect login request: %s", err
)
"Connection error occurred during Garmin Connect login request: %s", err)
raise ConfigEntryNotReady from err
except requests.exceptions.HTTPError as err:
if err.response.status_code == 401:
@@ -180,14 +245,13 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
raise ConfigEntryAuthFailed from err
if err.response.status_code == 429:
_LOGGER.error(
"Too many requests error occurred during login: %s", err.response.text)
"Too many requests error occurred during login: %s", err.response.text
)
return False
_LOGGER.error(
"Unknown HTTP error occurred during login: %s", err)
_LOGGER.error("Unknown HTTP error occurred during login: %s", err)
return False
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Unknown error occurred during login: %s", err)
_LOGGER.exception("Unknown error occurred during login: %s", err)
return False
return True
@@ -332,10 +396,14 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
# Gear data
try:
gear = await self.hass.async_add_executor_job(
self.api.get_gear, summary[Gear.USERPROFILE_ID]
)
_LOGGER.debug("Gear data fetched: %s", gear)
# Check if userProfileId exists in summary before fetching gear data
if Gear.USERPROFILE_ID in summary:
gear = await self.hass.async_add_executor_job(
self.api.get_gear, summary[Gear.USERPROFILE_ID]
)
_LOGGER.debug("Gear data fetched: %s", gear)
else:
_LOGGER.debug("No userProfileId found in summary, skipping gear data fetch")
# Fitness age data
fitnessage_data = await self.hass.async_add_executor_job(
@@ -364,8 +432,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
"Too many request error occurred during update: %s", err)
return {}
except GarminConnectConnectionError as err:
_LOGGER.error(
"Connection error occurred during update: %s", err)
_LOGGER.error("Connection error occurred during update: %s", err)
raise ConfigEntryNotReady from err
except requests.exceptions.HTTPError as err:
if err.response.status_code == 401:
@@ -374,50 +441,49 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
raise ConfigEntryAuthFailed from err
if err.response.status_code == 429:
_LOGGER.error(
"Too many requests error occurred during update: %s", err.response.text)
"Too many requests error occurred during update: %s", err.response.text
)
return {}
_LOGGER.error(
"Unknown HTTP error occurred during update: %s", err)
_LOGGER.error("Unknown HTTP error occurred during update: %s", err)
return False
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Unknown error occurred during update: %s", err)
_LOGGER.exception("Unknown error occurred during update: %s", err)
return {}
try:
# Gear data like shoes, bike, etc.
gear = await self.hass.async_add_executor_job(
self.api.get_gear, summary[Gear.USERPROFILE_ID]
)
# Use gear data from the first fetch if available
if gear:
_LOGGER.debug("Gear data fetched: %s", gear)
else:
_LOGGER.debug("No gear data found")
# Gear stats data like distance, time, etc.
tasks: list[Awaitable] = [
self.hass.async_add_executor_job(
self.api.get_gear_stats, gear_item[Gear.UUID])
for gear_item in gear
]
gear_stats = await asyncio.gather(*tasks)
if gear_stats:
_LOGGER.debug("Gear statistics data fetched: %s", gear_stats)
else:
_LOGGER.debug("No gear statistics data found")
# Gear stats data like distance, time, etc.
tasks: list[Awaitable] = [
self.hass.async_add_executor_job(
self.api.get_gear_stats, gear_item[Gear.UUID]
)
for gear_item in gear
]
gear_stats = await asyncio.gather(*tasks)
if gear_stats:
_LOGGER.debug("Gear statistics data fetched: %s", gear_stats)
# Gear defaults data like shoe, bike, etc.
if Gear.USERPROFILE_ID in summary:
gear_defaults = await self.hass.async_add_executor_job(
self.api.get_gear_defaults, summary[Gear.USERPROFILE_ID]
)
if gear_defaults:
_LOGGER.debug("Gear defaults data fetched: %s", gear_defaults)
else:
_LOGGER.debug("No gear defaults data found")
else:
_LOGGER.debug("No userProfileId found in summary, skipping gear defaults fetch")
else:
_LOGGER.debug("No gear statistics data found")
# Gear defaults data like shoe, bike, etc.
gear_defaults = await self.hass.async_add_executor_job(
self.api.get_gear_defaults, summary[Gear.USERPROFILE_ID]
)
if gear_defaults:
_LOGGER.debug("Gear defaults data fetched: %s", gear_defaults)
_LOGGER.debug("No gear data available, skipping gear stats and defaults fetch")
else:
_LOGGER.debug("No gear defaults data found")
except GarminConnectAuthenticationError as err:
_LOGGER.error(
"Authentication error occurred while fetching Gear data: %s", err.response.text)
"Authentication error occurred while fetching Gear data: %s", err.response.text
)
raise ConfigEntryAuthFailed from err
except GarminConnectTooManyRequestsError as err:
_LOGGER.error(
@@ -430,13 +496,15 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
except requests.exceptions.HTTPError as err:
if err.response.status_code == 401:
_LOGGER.error(
"Authentication error while fetching Gear data: %s", err.response.text)
"Authentication error while fetching Gear data: %s", err.response.text
)
elif err.response.status_code == 404:
_LOGGER.error(
"URL not found error while fetching Gear data: %s", err.response.text)
elif err.response.status_code == 429:
_LOGGER.error(
"Too many requests error while fetching Gear data: %s", err.response.text)
"Too many requests error while fetching Gear data: %s", err.response.text
)
else:
_LOGGER.error(
"Unknown HTTP error occurred while fetching Gear data: %s", err)

View File

@@ -1,21 +1,30 @@
"""Config flow for Garmin Connect integration."""
import logging
from collections.abc import Mapping
import logging
from typing import Any, cast
import requests
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
GarminConnectTooManyRequestsError,
)
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_ID, CONF_TOKEN, CONF_PASSWORD, CONF_USERNAME
import voluptuous as vol
import garth
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
import requests
import voluptuous as vol
from .const import CONF_MFA, DOMAIN
from .const import CONF_MFA, CONF_SENSOR_GROUPS, DOMAIN
from .sensor_descriptions import SENSOR_GROUPS, get_default_enabled_groups
_LOGGER = logging.getLogger(__name__)
@@ -25,10 +34,16 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
VERSION = 1
@staticmethod
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Get the options flow for this handler."""
return GarminConnectOptionsFlowHandler(config_entry)
def __init__(self) -> None:
"""
Initialize schemas and internal state for the Garmin Connect configuration flow handler.
Sets up validation schemas for user credentials and MFA input, and initializes variables for API client, login results, MFA code, credentials, and region detection.
"""
self.data_schema = {
@@ -50,12 +65,12 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
async def _async_garmin_connect_login(self, step_id: str) -> ConfigFlowResult:
"""
Authenticate the user with Garmin Connect and handle login errors or multi-factor authentication requirements.
If the user is located in China, configures the API client for the region. Initiates the login process and, if multi-factor authentication is needed, transitions to the MFA step. Handles specific authentication and connection errors, returning appropriate error messages to the user. On successful authentication, proceeds to create or update the configuration entry.
Parameters:
step_id (str): The current step identifier in the configuration flow.
Returns:
ConfigFlowResult: The result of the configuration flow step, which may be a form with errors, a transition to MFA, or entry creation.
"""
@@ -66,11 +81,14 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
if country == "CN":
self._in_china = True
self._api = Garmin(email=self._username,
password=self._password, return_on_mfa=True, is_cn=self._in_china)
self._api = Garmin(
email=self._username, password=self._password, return_on_mfa=True, is_cn=self._in_china
)
try:
self._login_result1, self._login_result2 = await self.hass.async_add_executor_job(self._api.login)
self._login_result1, self._login_result2 = await self.hass.async_add_executor_job(
self._api.login
)
if self._login_result1 == "needs_mfa": # MFA is required
return await self.async_step_mfa()
@@ -102,11 +120,13 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
async def _async_garmin_connect_mfa_login(self) -> ConfigFlowResult:
"""
Complete the Garmin Connect authentication process using the stored multi-factor authentication (MFA) code.
If the MFA code is invalid or an error occurs, prompts the user to re-enter the code. On successful authentication, creates or updates the configuration entry.
"""
try:
await self.hass.async_add_executor_job(self._api.resume_login, self._login_result2, self._mfa_code)
await self.hass.async_add_executor_job(
self._api.resume_login, self._login_result2, self._mfa_code
)
except garth.exc.GarthException as err:
_LOGGER.error("Error during MFA login: %s", err)
@@ -121,68 +141,58 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
async def _async_create_entry(self) -> ConfigFlowResult:
"""
Create or update the configuration entry for the Garmin Connect integration using the current user's credentials and API token.
If an entry with the same username exists, its data is updated and the entry is reloaded; otherwise, a new entry is created with the username as the unique ID and the serialized API token.
"""
config_data = {
CONF_ID: self._username,
CONF_USERNAME: self._username,
CONF_TOKEN: self._api.garth.dumps(),
}
existing_entry = await self.async_set_unique_id(self._username)
if existing_entry:
return self.async_update_reload_and_abort(existing_entry, data=config_data)
self.hass.config_entries.async_update_entry(existing_entry, data=config_data)
await self.hass.config_entries.async_reload(existing_entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_create_entry(
title=cast(str, self._username), data=config_data
)
return self.async_create_entry(title=cast(str, self._username), data=config_data)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
"""
Handle the initial user step of the configuration flow.
If no input is provided, displays a form to collect username and password. If credentials are submitted, stores them and attempts authentication with Garmin Connect.
"""
Handle the initial user step of the configuration flow.
If no input is provided, displays a form to collect username and password. If credentials are submitted, stores them and attempts authentication with Garmin Connect.
"""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=vol.Schema(self.data_schema)
)
return self.async_show_form(step_id="user", data_schema=vol.Schema(self.data_schema))
self._username = user_input[CONF_USERNAME]
self._password = user_input[CONF_PASSWORD]
return await self._async_garmin_connect_login(step_id="user")
async def async_step_mfa(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
async def async_step_mfa(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
"""
Handle the multi-factor authentication (MFA) step in the configuration flow.
If user input is not provided, displays a form to collect the MFA code. If input is provided, stores the MFA code and proceeds with MFA authentication.
"""
if user_input is None:
return self.async_show_form(
step_id="mfa", data_schema=vol.Schema(self.mfa_data_schema)
)
return self.async_show_form(step_id="mfa", data_schema=vol.Schema(self.mfa_data_schema))
self._mfa_code = user_input[CONF_MFA]
_LOGGER.debug("MFA code received")
return await self._async_garmin_connect_mfa_login()
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> ConfigFlowResult:
"""
Start the reauthorization process using existing configuration entry data.
Extracts the username from the entry data and advances to the reauthorization confirmation step.
Extracts the username from the entry data (using CONF_ID if CONF_USERNAME is not available for migrated entries) and advances to the reauthorization confirmation step.
"""
self._username = entry_data[CONF_USERNAME]
# For backward compatibility: try CONF_USERNAME first, fall back to CONF_ID
self._username = entry_data.get(CONF_USERNAME) or entry_data.get(CONF_ID)
return await self.async_step_reauth_confirm()
@@ -191,7 +201,7 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult:
"""
Prompt the user to re-enter their username and password to confirm reauthorization of the Garmin Connect integration.
If credentials are provided, attempts to log in and complete the reauthorization process.
"""
if user_input is None:
@@ -209,3 +219,59 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
self._password = user_input[CONF_PASSWORD]
return await self._async_garmin_connect_login(step_id="reauth_confirm")
class GarminConnectOptionsFlowHandler(OptionsFlow):
"""Handle options flow for Garmin Connect integration."""
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage the sensor group options."""
if user_input is not None:
# Convert list to set for storage
enabled_groups = set(user_input.get(CONF_SENSOR_GROUPS, []))
return self.async_create_entry(
title="",
data={CONF_SENSOR_GROUPS: list(enabled_groups)},
)
# Get currently enabled groups from options, or use defaults for backward compatibility
current_options = self.config_entry.options.get(CONF_SENSOR_GROUPS)
if current_options is None:
# First time setup or upgraded from version without options
enabled_groups = get_default_enabled_groups()
else:
enabled_groups = set(current_options)
# Build the multi-select schema with descriptions
options_schema = vol.Schema(
{
vol.Optional(
CONF_SENSOR_GROUPS,
default=list(enabled_groups),
): cv.multi_select(
{
group_id: f"{group.name} - {group.description}"
for group_id, group in SENSOR_GROUPS.items()
}
),
}
)
return self.async_show_form(
step_id="init",
data_schema=options_schema,
description_placeholders={
"info": (
"Select which sensor groups to enable. "
"Individual sensors within enabled groups can still be "
"disabled in the entity settings. "
"Changes will be applied after reloading the integration."
)
},
)

View File

@@ -4,12 +4,19 @@ from datetime import timedelta
from typing import NamedTuple
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfMass, UnitOfTime, UnitOfVolume
from homeassistant.const import (
PERCENTAGE,
UnitOfLength,
UnitOfMass,
UnitOfTime,
UnitOfVolume,
)
DOMAIN = "garmin_connect"
DATA_COORDINATOR = "coordinator"
DEFAULT_UPDATE_INTERVAL = timedelta(minutes=5)
CONF_MFA = "mfa_code"
CONF_SENSOR_GROUPS = "sensor_groups"
DAY_TO_NUMBER = {
"Mo": 1,
"M": 1,

View File

@@ -0,0 +1,23 @@
"""Base entity for Garmin Connect integration."""
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
class GarminConnectEntity(CoordinatorEntity):
"""Base entity for Garmin Connect."""
_attr_has_entity_name = True
def __init__(self, coordinator, unique_id: str) -> None:
"""Initialize Garmin Connect entity."""
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name="Garmin Connect",
manufacturer="Garmin",
model="Garmin Connect",
entry_type=None,
)

View File

@@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/cyberjunky/home-assistant-garmin_connect/issues",
"requirements": ["garminconnect>=0.2.31"],
"version": "0.2.33"
"version": "0.2.35"
}

View File

@@ -7,8 +7,6 @@ import logging
from numbers import Number
from zoneinfo import ZoneInfo
import voluptuous as vol
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@@ -24,56 +22,67 @@ from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
import voluptuous as vol
from .const import (
CONF_SENSOR_GROUPS,
DATA_COORDINATOR,
DOMAIN as GARMIN_DOMAIN,
GARMIN_ENTITY_LIST,
GEAR_ICONS,
Gear,
ServiceSetting,
)
from .entity import GarminConnectEntity
from .sensor_descriptions import (
ALL_SENSOR_DESCRIPTIONS,
get_default_enabled_groups,
get_sensors_for_groups,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
) -> None:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities) -> None:
"""Set up Garmin Connect sensor based on a config entry."""
coordinator: DataUpdateCoordinator = hass.data[GARMIN_DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
coordinator: DataUpdateCoordinator = hass.data[GARMIN_DOMAIN][entry.entry_id][DATA_COORDINATOR]
unique_id = entry.data[CONF_ID]
entities = []
for (
sensor_type,
(name, unit, icon, device_class, state_class, enabled_by_default),
) in GARMIN_ENTITY_LIST.items():
# Get enabled sensor groups from options, or use defaults for backward compatibility
enabled_groups_list = entry.options.get(CONF_SENSOR_GROUPS)
if enabled_groups_list is None:
# Backward compatibility: if no options set, enable all default groups
enabled_groups = get_default_enabled_groups()
else:
# Convert list back to set
enabled_groups = set(enabled_groups_list)
# Get sensor descriptions based on enabled groups
sensor_descriptions = get_sensors_for_groups(enabled_groups)
_LOGGER.debug(
"Setting up sensors with enabled groups: %s (%d sensors)",
enabled_groups,
len(sensor_descriptions),
)
# Add main sensors using entity descriptions
for description in sensor_descriptions:
_LOGGER.debug(
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
sensor_type,
name,
unit,
icon,
device_class,
state_class,
enabled_by_default,
"Registering entity: %s (%s)",
description.key,
description.translation_key,
)
entities.append(
GarminConnectSensor(
coordinator,
unique_id,
sensor_type,
name,
unit,
icon,
device_class,
state_class,
enabled_by_default,
description,
)
)
# Add gear sensors
if "gear" in coordinator.data:
for gear_item in coordinator.data["gear"]:
name = gear_item["displayName"]
@@ -86,15 +95,10 @@ async def async_setup_entry(
enabled_by_default = True
_LOGGER.debug(
"Registering entity: %s, %s, %s, %s, %s, %s, %s, %s",
"Registering gear entity: %s, %s, %s",
sensor_type,
name,
unit,
icon,
uuid,
device_class,
state_class,
enabled_by_default,
)
entities.append(
GarminConnectGearSensor(
@@ -159,73 +163,54 @@ async def async_setup_entry(
)
class GarminConnectSensor(CoordinatorEntity, SensorEntity):
class GarminConnectSensor(GarminConnectEntity, SensorEntity):
"""Representation of a Garmin Connect Sensor."""
_attr_has_entity_name = True
def __init__(
self,
coordinator,
unique_id,
sensor_type,
name,
unit,
icon,
device_class,
state_class,
enabled_default: bool = True,
coordinator: DataUpdateCoordinator,
unique_id: str,
description,
):
"""Initialize a Garmin Connect sensor."""
super().__init__(coordinator)
self._unique_id = unique_id
self._type = sensor_type
self._device_class = device_class
self._state_class = state_class
self._enabled_default = enabled_default
self._attr_name = name
self._attr_device_class = self._device_class
self._attr_icon = icon
self._attr_native_unit_of_measurement = unit
self._attr_unique_id = f"{self._unique_id}_{self._type}"
self._attr_state_class = state_class
super().__init__(coordinator, unique_id)
self.entity_description = description
self._attr_unique_id = f"{unique_id}_{description.key}"
@property
def native_value(self):
"""
Return the current value of the sensor, applying type-specific formatting and conversions.
Return the current value of the sensor.
For activity and badge sensors, returns the count. For last activity, returns the activity name. HRV status and stress qualifier values are capitalized. Duration and seconds values are converted from seconds to minutes, and mass values from grams to kilograms. For alarms, returns the next active alarm if available. Timestamp values are converted to timezone-aware datetime objects. Returns None if data is unavailable.
Uses the entity description's value_fn if provided, otherwise applies
type-specific formatting and conversions for backward compatibility.
"""
if not self.coordinator.data:
return None
value = self.coordinator.data.get(self._type)
# Use custom value function if provided in description
if self.entity_description.value_fn:
return self.entity_description.value_fn(self.coordinator.data)
# Fallback to legacy value extraction
value = self.coordinator.data.get(self.entity_description.key)
if value is None:
return None
if self._type == "lastActivities" or self._type == "badges":
value = len(self.coordinator.data[self._type])
# Legacy type-specific handling
sensor_type = self.entity_description.key
if self._type == "lastActivity":
value = self.coordinator.data[self._type]["activityName"]
if sensor_type == "lastActivities" or sensor_type == "badges":
value = len(self.coordinator.data[sensor_type])
elif self._type == "hrvStatus":
value = self.coordinator.data[self._type]["status"].capitalize()
elif sensor_type == "lastActivity":
value = self.coordinator.data[sensor_type]["activityName"]
elif self._type == "enduranceScore":
value = self.coordinator.data[self._type]["overallScore"]
elif sensor_type == "enduranceScore":
value = self.coordinator.data[sensor_type]["overallScore"]
elif "Duration" in self._type or "Seconds" in self._type:
value = round(value // 60, 2)
elif "Mass" in self._type or self._type == "weight":
value = round(value / 1000, 2)
elif self._type == "nextAlarm":
active_alarms = self.coordinator.data[self._type]
elif sensor_type == "nextAlarm":
active_alarms = self.coordinator.data[sensor_type]
if active_alarms:
_LOGGER.debug("Active alarms: %s", active_alarms)
_LOGGER.debug("Next alarm: %s", active_alarms[0])
@@ -233,14 +218,13 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
else:
value = None
elif self._type == "stressQualifier":
value = value.capitalize()
if self._device_class == SensorDeviceClass.TIMESTAMP:
# Handle timestamp device class
if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP:
if value:
value = datetime.datetime.fromisoformat(value).replace(
tzinfo=ZoneInfo(self.coordinator.time_zone)
)
return round(value, 2) if isinstance(value, Number) else value
@property
@@ -248,67 +232,54 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
"""
Return additional state attributes for the sensor entity.
Includes the last sync timestamp and, depending on the sensor type, recent activities (up to 5), badges (up to 10), alarms, or HRV status details (excluding the status string). Returns an empty dictionary if no coordinator data is available.
Uses the entity description's attributes_fn if provided, otherwise
returns sensor-specific attributes for backward compatibility.
"""
if not self.coordinator.data:
return {}
# Use custom attributes function if provided in description
if self.entity_description.attributes_fn:
return self.entity_description.attributes_fn(self.coordinator.data)
# Base attributes
attributes = {
"last_synced": self.coordinator.data["lastSyncTimestampGMT"],
}
sensor_type = self.entity_description.key
# Only keep the last 5 activities for performance reasons
if self._type == "lastActivities":
activities = self.coordinator.data.get(self._type, [])
sorted_activities = sorted(
activities, key=lambda x: x["activityId"])
if sensor_type == "lastActivities":
activities = self.coordinator.data.get(sensor_type, [])
sorted_activities = sorted(activities, key=lambda x: x["activityId"])
attributes["last_activities"] = sorted_activities[-5:]
if self._type == "lastActivity":
attributes = {**attributes, **self.coordinator.data[self._type]}
elif sensor_type == "lastActivity":
attributes = {**attributes, **self.coordinator.data[sensor_type]}
# Only keep the last 10 badges for performance reasons
if self._type == "badges":
badges = self.coordinator.data.get(self._type, [])
elif sensor_type == "badges":
badges = self.coordinator.data.get(sensor_type, [])
sorted_badges = sorted(badges, key=lambda x: x["badgeEarnedDate"])
attributes["badges"] = sorted_badges[-10:]
if self._type == "nextAlarm":
attributes["next_alarms"] = self.coordinator.data[self._type]
elif sensor_type == "nextAlarm":
attributes["next_alarms"] = self.coordinator.data[sensor_type]
if self._type == "hrvStatus":
attributes = {**attributes, **self.coordinator.data[self._type]}
del attributes["status"]
if self._type == "enduranceScore":
attributes = {**attributes, **self.coordinator.data[self._type]}
elif sensor_type == "enduranceScore":
attributes = {**attributes, **self.coordinator.data[sensor_type]}
del attributes["overallScore"]
return attributes
@property
def device_info(self) -> DeviceInfo:
"""Return device information."""
return DeviceInfo(
identifiers={(GARMIN_DOMAIN, self._unique_id)},
name="Garmin Connect",
manufacturer="Garmin",
model="Garmin Connect",
entry_type=None,
)
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._enabled_default
@property
def available(self) -> bool:
"""Return True if entity is available."""
return (
super().available
and self.coordinator.data
and self._type in self.coordinator.data
and self.entity_description.key in self.coordinator.data
)
async def add_body_composition(self, **kwargs):
@@ -336,9 +307,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
"""Check for login."""
if not await self.coordinator.async_login():
raise IntegrationError(
"Failed to login to Garmin Connect, unable to update"
)
raise IntegrationError("Failed to login to Garmin Connect, unable to update")
"""Record a weigh in/body composition."""
await self.hass.async_add_executor_job(
@@ -380,9 +349,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
"""Check for login."""
if not await self.coordinator.async_login():
raise IntegrationError(
"Failed to login to Garmin Connect, unable to update"
)
raise IntegrationError("Failed to login to Garmin Connect, unable to update")
"""Record a blood pressure measurement."""
await self.hass.async_add_executor_job(
@@ -455,9 +422,7 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
stats = self._stats()
gear_defaults = self._gear_defaults()
activity_types = self.coordinator.data["activityTypes"]
default_for_activity = self._activity_names_for_gear_defaults(
gear_defaults, activity_types
)
default_for_activity = self._activity_names_for_gear_defaults(gear_defaults, activity_types)
if not self.coordinator.data or not gear or not stats:
return {}
@@ -485,9 +450,7 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
def _activity_names_for_gear_defaults(self, gear_defaults, activity_types):
"""Get activity names for gear defaults."""
activity_type_ids = [d["activityTypePk"] for d in gear_defaults]
return [
a["typeKey"] for a in activity_types if a["typeId"] in activity_type_ids
]
return [a["typeKey"] for a in activity_types if a["typeId"] in activity_type_ids]
@property
def device_info(self) -> DeviceInfo:
@@ -552,9 +515,7 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
"""Check for login."""
if not await self.coordinator.async_login():
raise IntegrationError(
"Failed to login to Garmin Connect, unable to update"
)
raise IntegrationError("Failed to login to Garmin Connect, unable to update")
"""Update Garmin Gear settings."""
activity_type_id = next(

File diff suppressed because it is too large Load Diff

View File

@@ -33,5 +33,16 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
},
"options": {
"step": {
"init": {
"title": "Configure Sensor Groups",
"description": "Select which sensor groups to enable. Individual sensors within enabled groups can still be disabled in the entity settings. Changes will be applied after reloading the integration.",
"data": {
"sensor_groups": "Enabled Sensor Groups"
}
}
}
}
}

View File

@@ -34,5 +34,308 @@
"unknown": "Unexpected error",
"invalid_mfa_code": "Invalid MFA code"
}
},
"entity": {
"sensor": {
"total_steps": {
"name": "Total steps"
},
"daily_step_goal": {
"name": "Daily step goal"
},
"total_distance": {
"name": "Total distance"
},
"floors_ascended": {
"name": "Floors ascended"
},
"floors_descended": {
"name": "Floors descended"
},
"floors_ascended_goal": {
"name": "Floors ascended goal"
},
"floors_ascended_meters": {
"name": "Floors ascended (meters)"
},
"floors_descended_meters": {
"name": "Floors descended (meters)"
},
"total_calories": {
"name": "Total calories"
},
"active_calories": {
"name": "Active calories"
},
"bmr_calories": {
"name": "BMR calories"
},
"burned_calories": {
"name": "Burned calories"
},
"consumed_calories": {
"name": "Consumed calories"
},
"remaining_calories": {
"name": "Remaining calories"
},
"resting_heart_rate": {
"name": "Resting heart rate"
},
"max_heart_rate": {
"name": "Max heart rate"
},
"min_heart_rate": {
"name": "Min heart rate"
},
"last_7_days_avg_resting_heart_rate": {
"name": "Last 7 days avg resting heart rate"
},
"min_avg_heart_rate": {
"name": "Min avg heart rate"
},
"max_avg_heart_rate": {
"name": "Max avg heart rate"
},
"abnormal_hr_alerts": {
"name": "Abnormal HR alerts"
},
"hrv_status": {
"name": "HRV status"
},
"avg_stress_level": {
"name": "Avg stress level"
},
"max_stress_level": {
"name": "Max stress level"
},
"stress_qualifier": {
"name": "Stress qualifier"
},
"total_stress_duration": {
"name": "Total stress duration"
},
"rest_stress_duration": {
"name": "Rest stress duration"
},
"activity_stress_duration": {
"name": "Activity stress duration"
},
"low_stress_duration": {
"name": "Low stress duration"
},
"medium_stress_duration": {
"name": "Medium stress duration"
},
"high_stress_duration": {
"name": "High stress duration"
},
"uncategorized_stress_duration": {
"name": "Uncategorized stress duration"
},
"stress_duration": {
"name": "Stress duration"
},
"stress_percentage": {
"name": "Stress percentage"
},
"rest_stress_percentage": {
"name": "Rest stress percentage"
},
"activity_stress_percentage": {
"name": "Activity stress percentage"
},
"uncategorized_stress_percentage": {
"name": "Uncategorized stress percentage"
},
"low_stress_percentage": {
"name": "Low stress percentage"
},
"medium_stress_percentage": {
"name": "Medium stress percentage"
},
"high_stress_percentage": {
"name": "High stress percentage"
},
"sleeping_time": {
"name": "Sleeping time"
},
"total_sleep_duration": {
"name": "Total sleep duration"
},
"sleep_duration": {
"name": "Sleep duration"
},
"awake_duration": {
"name": "Awake duration"
},
"sleep_score": {
"name": "Sleep score"
},
"body_battery_most_recent": {
"name": "Body battery (most recent)"
},
"body_battery_highest": {
"name": "Body battery (highest)"
},
"body_battery_lowest": {
"name": "Body battery (lowest)"
},
"body_battery_charged": {
"name": "Body battery charged"
},
"body_battery_drained": {
"name": "Body battery drained"
},
"weight": {
"name": "Weight"
},
"bmi": {
"name": "BMI"
},
"body_fat": {
"name": "Body fat"
},
"body_water": {
"name": "Body water"
},
"bone_mass": {
"name": "Bone mass"
},
"muscle_mass": {
"name": "Muscle mass"
},
"physique_rating": {
"name": "Physique rating"
},
"visceral_fat": {
"name": "Visceral fat"
},
"metabolic_age": {
"name": "Metabolic age"
},
"hydration": {
"name": "Hydration"
},
"hydration_goal": {
"name": "Hydration goal"
},
"hydration_daily_average": {
"name": "Hydration daily average"
},
"hydration_sweat_loss": {
"name": "Hydration sweat loss"
},
"hydration_activity_intake": {
"name": "Hydration activity intake"
},
"active_time": {
"name": "Active time"
},
"highly_active_time": {
"name": "Highly active time"
},
"sedentary_time": {
"name": "Sedentary time"
},
"moderate_intensity": {
"name": "Moderate intensity"
},
"vigorous_intensity": {
"name": "Vigorous intensity"
},
"intensity_goal": {
"name": "Intensity goal"
},
"avg_spo2": {
"name": "Avg SpO2"
},
"lowest_spo2": {
"name": "Lowest SpO2"
},
"latest_spo2": {
"name": "Latest SpO2"
},
"latest_spo2_time": {
"name": "Latest SpO2 time"
},
"highest_respiration": {
"name": "Highest respiration"
},
"lowest_respiration": {
"name": "Lowest respiration"
},
"latest_respiration": {
"name": "Latest respiration"
},
"latest_respiration_time": {
"name": "Latest respiration time"
},
"avg_altitude": {
"name": "Avg altitude"
},
"chronological_age": {
"name": "Chronological age"
},
"fitness_age": {
"name": "Fitness age"
},
"achievable_fitness_age": {
"name": "Achievable fitness age"
},
"previous_fitness_age": {
"name": "Previous fitness age"
},
"endurance_score": {
"name": "Endurance score"
},
"next_alarm": {
"name": "Next alarm"
},
"last_activity": {
"name": "Last activity"
},
"last_activities": {
"name": "Last activities"
},
"badges": {
"name": "Badges"
},
"user_points": {
"name": "User points"
},
"user_level": {
"name": "User level"
},
"wellness_start_time": {
"name": "Wellness start time"
},
"wellness_end_time": {
"name": "Wellness end time"
},
"wellness_description": {
"name": "Wellness description"
},
"wellness_distance": {
"name": "Wellness distance"
},
"wellness_active_calories": {
"name": "Wellness active calories"
},
"wellness_calories": {
"name": "Wellness calories"
}
}
},
"options": {
"step": {
"init": {
"title": "Configure Sensor Groups",
"description": "Select which sensor groups to enable. Individual sensors within enabled groups can still be disabled in the entity settings. Changes will be applied after reloading the integration.",
"data": {
"sensor_groups": "Enabled Sensor Groups"
}
}
}
}
}