Compare commits

...

5 Commits

Author SHA1 Message Date
Ron Klinkien
9cf1b3a3bb Migrate from old config flow to new config flow
Catch authentication errors and show them in the UI
Limit last activity to 5 in attributes
2025-04-19 10:38:01 +00:00
Ron Klinkien
84ad53891b Added new sensors to README.md 2025-04-18 18:31:07 +00:00
Ron Klinkien
4163be285f Added hydration sensors 2025-04-18 18:30:04 +00:00
Ron
ee5df999a0 Update README.md 2025-04-18 20:09:58 +02:00
Ron Klinkien
a179131a94 Added Age sensors
Only store last 10 activities in attribute
2025-04-18 18:06:12 +00:00
8 changed files with 134 additions and 22 deletions

View File

@@ -70,6 +70,15 @@ Next Alarm Time
Total Sleep Duration
HRV Status
Gear Sensors
Chronological Age
Fitness Age
Achievable Fitness Age
Previous Fitness Age
Hydration
Hydration Goal
Hydration Daily Average
Hydration Sweat Loss
Hydration Activity Intake
```
Disabled by default:

View File

@@ -15,9 +15,9 @@ from garminconnect import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
import binascii
from .const import (
DATA_COORDINATOR,
DAY_TO_NUMBER,
@@ -84,11 +84,15 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
async def async_login(self) -> bool:
"""Login to Garmin Connect."""
try:
# Check if the token exists in the entry data
if CONF_TOKEN not in self.entry.data:
raise KeyError("Token not found, migrating config entry")
await self.hass.async_add_executor_job(self.api.login, self.entry.data[CONF_TOKEN])
except GarminConnectAuthenticationError as err:
_LOGGER.error(
"Authentication error occurred during login: %s", err.response.text)
return False
raise ConfigEntryAuthFailed from err
except GarminConnectTooManyRequestsError as err:
_LOGGER.error(
"Too many request error occurred during login: %s", err)
@@ -101,7 +105,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
if err.response.status_code == 401:
_LOGGER.error(
"Authentication error occurred during login: %s", err.response.text)
return False
raise ConfigEntryAuthFailed from err
if err.response.status_code == 429:
_LOGGER.error(
"Too many requests error occurred during login: %s", err.response.text)
@@ -109,6 +113,10 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
_LOGGER.error(
"Unknown HTTP error occurred during login: %s", err)
return False
except KeyError as err:
_LOGGER.error(
"Found old config during login: %s", err)
raise ConfigEntryAuthFailed from err
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Unknown error occurred during login: %s", err)
@@ -233,10 +241,28 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
else:
_LOGGER.debug("No HRV data found")
# Fitness age data
fitnessage_data = await self.hass.async_add_executor_job(
self.api.get_fitnessage_data, today.isoformat()
)
if fitnessage_data:
_LOGGER.debug("Fitness age data fetched: %s", fitnessage_data)
else:
_LOGGER.debug("No fitness age data found")
# Hyrdation data
hydration_data = await self.hass.async_add_executor_job(
self.api.get_hydration_data, today.isoformat()
)
if hydration_data:
_LOGGER.debug("Hydration data fetched: %s", hydration_data)
else:
_LOGGER.debug("No hydration data found")
except GarminConnectAuthenticationError as err:
_LOGGER.error(
"Authentication error occurred during update: %s", err.response.text)
return False
raise ConfigEntryAuthFailed from err
except GarminConnectTooManyRequestsError as err:
_LOGGER.error(
"Too many request error occurred during update: %s", err)
@@ -249,7 +275,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
if err.response.status_code == 401:
_LOGGER.error(
"Authentication error occurred during update: %s", err.response.text)
return False
raise ConfigEntryAuthFailed from err
if err.response.status_code == 429:
_LOGGER.error(
"Too many requests error occurred during update: %s", err.response.text)
@@ -333,6 +359,8 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
"sleepScore": sleep_score,
"sleepTimeSeconds": sleep_time_seconds,
"hrvStatus": hrv_status,
**fitnessage_data,
**hydration_data,
}

View File

@@ -13,6 +13,7 @@ from garminconnect import (
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 .const import CONF_MFA, DOMAIN
@@ -57,8 +58,6 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
try:
self._login_result1, self._login_result2 = await self.hass.async_add_executor_job(self._api.login)
_LOGGER.debug(f"Login result1: {self._login_result1}")
_LOGGER.debug(f"Login result2: {self._login_result2}")
if self._login_result1 == "needs_mfa": # MFA is required
return await self.async_step_mfa()
@@ -89,12 +88,10 @@ class GarminConnectConfigFlowHandler(ConfigFlow, domain=DOMAIN):
async def _async_garmin_connect_mfa_login(self) -> ConfigFlowResult:
"""Handle multi-factor authentication (MFA) login with Garmin Connect."""
try:
await self.hass.async_add_executor_job(self._api.resume_login, self._login_result2, self._mfa_code)
oauth1, oauth2 = await self.hass.async_add_executor_job(self._api.resume_login, self._login_result2, self._mfa_code)
_LOGGER.info(f"Oauth1: {oauth1}, Oauth2: {oauth2}")
except GarminConnectAuthenticationError:
except garth.exc.GarthException as err:
_LOGGER.error(f"Error during MFA login: {err}")
return self.async_show_form(
step_id="mfa",
data_schema=vol.Schema(self.mfa_data_schema),

View File

@@ -4,7 +4,7 @@ from datetime import timedelta
from typing import NamedTuple
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfMass, UnitOfTime
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfMass, UnitOfTime, UnitOfVolume
DOMAIN = "garmin_connect"
DATA_COORDINATOR = "coordinator"
@@ -657,6 +657,78 @@ GARMIN_ENTITY_LIST = {
None,
True,
],
"chronologicalAge": [
"Chronological Age",
UnitOfTime.YEARS,
"mdi:calendar-heart",
None,
SensorStateClass.MEASUREMENT,
True,
],
"fitnessAge": [
"Fitness Age",
UnitOfTime.YEARS,
"mdi:calendar-heart",
None,
SensorStateClass.MEASUREMENT,
True,
],
"achievableFitnessAge": [
"Achievable Fitness Age",
UnitOfTime.YEARS,
"mdi:calendar-heart",
None,
SensorStateClass.MEASUREMENT,
True,
],
"previousFitnessAge": [
"Previous Fitness Age",
UnitOfTime.YEARS,
"mdi:calendar-heart",
None,
SensorStateClass.MEASUREMENT,
True,
],
"valueInML": [
"Hydration",
UnitOfVolume.MILLILITERS,
"mdi:water",
None,
SensorStateClass.MEASUREMENT,
True,
],
"goalInML": [
"Hydration Goal",
UnitOfVolume.MILLILITERS,
"mdi:water",
None,
SensorStateClass.MEASUREMENT,
True,
],
"dailyAverageInML": [
"Hydration Daily Average",
UnitOfVolume.MILLILITERS,
"mdi:water",
None,
SensorStateClass.MEASUREMENT,
True,
],
"sweatLossInML": [
"Hydration Sweat Loss",
UnitOfVolume.MILLILITERS,
"mdi:water",
None,
SensorStateClass.MEASUREMENT,
True,
],
"activityIntakeInML": [
"Hydration Activity Intake",
UnitOfVolume.MILLILITERS,
"mdi:water",
None,
SensorStateClass.MEASUREMENT,
True,
],
}
GEAR_ICONS = {

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.26"],
"version": "0.2.31-beta-01"
"version": "0.2.31-beta-04"
}

View File

@@ -239,13 +239,17 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
"last_synced": self.coordinator.data["lastSyncTimestampGMT"],
}
# Only keep the last 10 activities for performance reasons
if self._type == "lastActivities":
attributes["last_activities"] = self.coordinator.data[self._type]
activities = self.coordinator.data.get(self._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]}
# Only show the last 10 badges for performance reasons
# Only keep the last 10 badges for performance reasons
if self._type == "badges":
badges = self.coordinator.data.get(self._type, [])
sorted_badges = sorted(badges, key=lambda x: x["badgeEarnedDate"])

View File

@@ -25,7 +25,7 @@
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"too_many_requests": "Too many requests, retry later.",
"too_many_requests": "Too many requests, retry later",
"unknown": "[%key:common::config_flow::error::unknown%]",
"invalid_mfa_code": "Invalid MFA code"
},

View File

@@ -24,13 +24,15 @@
}
},
"abort": {
"already_configured": "Account is already configured"
"already_configured": "Account is already configured",
"reauth_successful": "Reauthentication successful"
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"too_many_requests": "Too many requests, retry later.",
"unknown": "Unexpected error"
"too_many_requests": "Too many requests, retry later",
"unknown": "Unexpected error",
"invalid_mfa_code": "Invalid MFA code"
}
}
}