mirror of
https://github.com/cyberjunky/home-assistant-garmin_connect.git
synced 2026-01-09 12:57:58 -05:00
Compare commits
5 Commits
0.2.31-bet
...
0.2.31-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cf1b3a3bb | ||
|
|
84ad53891b | ||
|
|
4163be285f | ||
|
|
ee5df999a0 | ||
|
|
a179131a94 |
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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"])
|
||||
|
||||
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user