Compare commits

...

78 Commits

Author SHA1 Message Date
Ron
891ddbce27 Bumped python-garminconnect version 2023-12-22 13:28:59 +01:00
Ron Klinkien
89222f0299 Small textual changes 2023-09-15 13:02:43 +02:00
Ron Klinkien
18304be84a Bumped python-garminconnect package to 0.2.3 to fix stability issues 2023-09-15 13:01:34 +02:00
Ron
ffd478cc60 Fixed typo 2023-07-12 13:23:13 +02:00
Ron
4e6b9310a1 Merge pull request #89 from jfparis/sleep_score
Pull sleepscore from Garmin interface
2023-07-12 12:58:23 +02:00
Jean-François Paris
e9fba7abbe Pull sleepscore from Garmin interface 2023-06-23 23:34:24 +01:00
Ron
17fa2bcc63 Update version 2023-05-22 10:17:21 +02:00
Ron
b26f36c41f Merge pull request #72 from ViPeR5000/patch-2
Update translation
2023-05-22 10:14:53 +02:00
Ron
845cce3bda Merge pull request #49 from ray0711/gear_sensor
Added gear sensors & gear service
2023-05-22 10:14:12 +02:00
Ron
15800b00d5 Merge pull request #80 from gorzelak/main
Adding last activities sensor
2023-05-22 10:11:49 +02:00
Ron
c9e9740137 Merge pull request #79 from pailloM/main
Remove pytz dependency in sensor.py
2023-05-22 10:10:23 +02:00
Krzysztof Gorzelak
b7f2a6e653 Adding last activities sensor 2023-05-05 10:09:46 +02:00
Paillomams
be150124b7 remove pytz dependency 2023-04-23 00:46:24 -04:00
Ron
28876a8709 Update manifest.json 2023-04-19 08:23:38 +02:00
ray0711
287a9a04c3 Merge branch 'main' of https://github.com/cyberjunky/home-assistant-garmin_connect into gear_sensor 2023-04-16 10:15:05 +02:00
Ron
de51403c43 Update manifest.json 2023-04-05 17:39:31 +02:00
Ron
36e5b7c6b8 Add support for China 2023-04-05 17:39:16 +02:00
Ron
9a6aea06c4 Delete .github/workflows directory 2023-03-28 12:18:31 +02:00
Ron
bc1ff88af2 Update hassfest.yml 2023-03-28 12:07:57 +02:00
Ron
48d738d1dc Update manifest.json 2023-03-28 11:44:38 +02:00
Rui Melo
d57b8e0223 update
fix word
2023-03-03 11:25:43 +00:00
Ron
0688f89be1 Fix manifest validation errors 2023-03-03 10:08:37 +01:00
Ron
985468d7a2 Update manifest.json 2023-03-03 09:52:56 +01:00
Ron
ff0ccb7e99 Update manifest.json 2023-03-03 09:52:34 +01:00
Ron
f59b41e7c4 Fixed errors for weight and stress sensors 2023-03-03 09:52:23 +01:00
Ron
0074b94723 Fixed syntax error 2023-02-03 09:14:27 +01:00
Ron
ee777314f9 Update manifest.json 2023-02-02 18:13:12 +01:00
Ron
58f5dd49db Merge pull request #62 from misa1515/patch-2
Create sk.json
2023-02-02 18:12:30 +01:00
Ron
7b66c4b7be Update manifest.json 2023-02-02 18:11:13 +01:00
Ron
1aa8cfb78f Use async_forward_entry_setups instead of async_setup_platforms 2023-02-02 18:10:58 +01:00
misa1515
a29be3127e Create sk.json
Created new translation to slovak language
2023-01-20 21:13:04 +01:00
Ron
ef91580157 Updated version 2023-01-10 09:05:17 +01:00
Ron
1c3e5a4385 Merge pull request #61 from arniebarni/main
Use local time zone for alarms
2023-01-10 08:33:02 +01:00
arniebarni
b6aebe1637 use local time zone for alarms 2023-01-09 19:47:11 +01:00
Ron
9cf15616ab Updated version 2023-01-09 13:37:22 +01:00
Ron
911ca663ab Fixed timestamp related sensors 2023-01-09 13:37:05 +01:00
Ron
645d3842bd Bumped version 2023-01-07 11:51:04 +01:00
Ron
04aa1e7093 Added SensorStateClass per sensor 2023-01-07 11:50:32 +01:00
Ron
f33c2803bc Add SensorStateClass setting per sensor 2023-01-07 11:50:02 +01:00
Ron
d118638429 Corrected units/device type for some body related sensors 2023-01-06 12:50:22 +01:00
Ron
3103929f5f Updated version and bumped garminconnect pkg version 2023-01-06 12:49:27 +01:00
Ron
842355d273 Added debug logging 2023-01-06 12:48:23 +01:00
Ron
e2728b6bc2 Update manifest.json 2023-01-04 20:08:21 +01:00
Ron
b75133a2d4 Fixed error with stress qualfier 2023-01-04 20:08:07 +01:00
Ron
ecbed70aee Update manifest.json 2023-01-04 19:43:06 +01:00
Ron
a761bc00a3 Update sensor.py 2023-01-04 19:42:46 +01:00
Ron
517bd5af5d Converted alarm time value to datetime 2023-01-04 19:42:20 +01:00
Ron
dc4e7301d8 Update manifest.json 2023-01-04 19:04:57 +01:00
Ron
e7ab9ef561 Update sensor.py 2023-01-04 19:04:04 +01:00
Ron
5438cd2d59 Updated version 2023-01-04 15:04:00 +01:00
Ron
4113550397 Implemented SensorDeviceClass 2023-01-04 15:03:23 +01:00
Ron
4b53fdb162 Implemented SensorDeviceClass 2023-01-04 15:02:47 +01:00
Raimund Huber
fa48f9f4e2 List default for actitivies on sensor, bugfixes 2022-12-08 20:10:58 +01:00
Raimund Huber
4f4d0b8425 Gear service to update default gear per activity on garmin connect 2022-12-06 22:02:51 +01:00
Raimund Huber
8760bc0a78 unreleased python-garminconnect version 2022-12-04 12:35:28 +01:00
Raimund Huber
8efd365163 Fixup exception for non-numeric sensor values e.g. stress_qualifier = UNKNOWN 2022-12-04 12:34:47 +01:00
Raimund Huber
cc6e0af4e5 add basic gear sensors 2022-12-04 12:03:10 +01:00
Ron Klinkien
6a1b5240d7 Update README.md 2022-05-24 21:19:04 +02:00
Ron Klinkien
5155e90c8a Corrected cal to kcal for Netto Galorie Goal 2022-05-24 20:04:55 +02:00
Ron Klinkien
1844b50877 Fixed unit of measurement legacy code 2022-05-24 20:01:16 +02:00
Ron Klinkien
0241207443 Merge pull request #37 from wrt54g/main
Update HACS URL
2022-05-03 08:38:36 +02:00
Sven
eaec42124b Update HACS URL 2022-05-03 08:08:11 +02:00
Ron Klinkien
8dea9bfa4b Return correct results 2022-04-22 11:44:40 +02:00
Ron Klinkien
f13f59ae46 Don't use timestamp for nextalarm sensor 2022-04-22 09:43:30 +02:00
Ron Klinkien
e958d2af89 Remove title of step user 2022-04-11 11:08:18 +02:00
Ron Klinkien
8beb401e41 Remove title of step user 2022-04-11 11:07:38 +02:00
Ron Klinkien
4cd2826643 Update README.md 2022-01-05 10:23:20 +01:00
Ron Klinkien
6a5a7fbd34 Added statistics to sensors
Updated sensor code to newer standard
2021-12-30 11:34:38 +01:00
Ron Klinkien
30fe26751c Update manifest.json 2021-12-29 21:55:46 +01:00
Ron Klinkien
05adddd8e5 Update config_flow.py 2021-12-29 21:54:35 +01:00
Ron Klinkien
3fd9388170 Update __init__.py 2021-12-29 21:54:16 +01:00
Ron Klinkien
dd7cfc021f Update manifest.json 2021-12-29 13:09:07 +01:00
Ron Klinkien
154190f9e9 Update hacs.json 2021-12-24 16:41:58 +01:00
Ron Klinkien
e2d3a61c0e Merge pull request #22 from obbers/main
Remvove country from hacs.json
2021-12-24 16:41:21 +01:00
Ron Klinkien
47717f019c Bumped garminconnect-ha 2021-12-24 13:38:00 +01:00
Ron Klinkien
03f576207f Bumped garminconnect-ha to 0.1.16
Renamed bodyMass to boneMass
2021-12-24 12:37:16 +01:00
Gary Sinclair
db334c0761 Merge branch 'cyberjunky:main' into main 2021-12-23 14:35:08 -06:00
Gary Sinclair
7ce7bd2277 Remove Country from hacs.json 2021-12-08 08:40:03 -06:00
14 changed files with 532 additions and 138 deletions

View File

@@ -1,15 +0,0 @@
name: Validate with hassfest
on:
push:
pull_request:
schedule:
- cron: "0 0 * * *"
jobs:
validate:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- uses: home-assistant/actions/hassfest@master

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2021 Ron Klinkien
Copyright (c) 2023 Ron Klinkien
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,16 +1,19 @@
[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg)](https://github.com/custom-components/hacs) [![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/cyberjunkynl/)
[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg)](https://github.com/hacs/integration) [![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/cyberjunkynl/)
# Garmin Connect
The Garmin Connect integration allows you to expose data from Garmin Connect to Home Assistant.
NOTE: This integration doesn't support 2FA on Garmin Connect yet (support is coming), so if you have enabled it -and want to keep it- this integration doesn't work, it will try to login repeatedly and generate lots of 2FA codes via email.
The change of adding support for it is unlikely since the Garmin Connect API is closed source, and will not be open for open-sourced projects.
## Installation
### HACS - Recommended
- Have [HACS](https://hacs.xyz) installed, this will allow you to easily manage and track updates.
- Add https://github.com/cyberjunky/home-assistant-garmin_connect to custom repositories in HACS
- Inside HACS click 'Explore & download repositories'
- Search for 'Garmin Connect'.
- Click Install below the found integration.
- Click on found integration.
- Click Download this repository with HACS.
- Restart Home-Assistant.
- Follow configuration steps below.

View File

@@ -1,8 +1,11 @@
"""The Garmin Connect integration."""
from datetime import date
from datetime import timedelta
import logging
import asyncio
from collections.abc import Awaitable
from garminconnect_ha import (
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
@@ -12,10 +15,16 @@ from garminconnect_ha import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryNotReady, IntegrationError
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DATA_COORDINATOR, DEFAULT_UPDATE_INTERVAL, DOMAIN
from .const import (
DATA_COORDINATOR,
DEFAULT_UPDATE_INTERVAL,
DOMAIN,
GEAR,
SERVICE_SETTING,
)
_LOGGER = logging.getLogger(__name__)
@@ -33,11 +42,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {
DATA_COORDINATOR: coordinator,
}
hass.data[DOMAIN][entry.entry_id] = {DATA_COORDINATOR: coordinator}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
@@ -57,8 +64,13 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
"""Initialize the Garmin Connect hub."""
self.entry = entry
self.hass = hass
self.in_china = False
self._api = Garmin(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD])
country = self.hass.config.country
if country == "CN":
self.in_china = True
self._api = Garmin(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], self.in_china)
super().__init__(
hass, _LOGGER, name=DOMAIN, update_interval=DEFAULT_UPDATE_INTERVAL
@@ -93,10 +105,37 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
summary = await self.hass.async_add_executor_job(
self._api.get_user_summary, date.today().isoformat()
)
_LOGGER.debug(summary)
body = await self.hass.async_add_executor_job(
self._api.get_body_composition, date.today().isoformat()
)
activities = await self.hass.async_add_executor_job(
self._api.get_activities_by_date, (date.today()-timedelta(days=7)).isoformat(), (date.today()+timedelta(days=1)).isoformat()
)
_LOGGER.debug(body)
alarms = await self.hass.async_add_executor_job(self._api.get_device_alarms)
_LOGGER.debug(alarms)
gear = await self.hass.async_add_executor_job(
self._api.get_gear, summary[GEAR.USERPROFILE_ID]
)
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)
activity_types = await self.hass.async_add_executor_job(
self._api.get_activity_types
)
gear_defaults = await self.hass.async_add_executor_job(
self._api.get_gear_defaults, summary[GEAR.USERPROFILE_ID]
)
sleep_data = await self.hass.async_add_executor_job(
self._api.get_sleep_data, date.today().isoformat())
except (
GarminConnectAuthenticationError,
GarminConnectTooManyRequestsError,
@@ -107,8 +146,64 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator):
raise UpdateFailed(error) from error
return {}
sleep_score = None
try:
sleep_score = sleep_data["dailySleepDTO"]["sleepScores"]["overall"]["value"]
except KeyError:
_LOGGER.debug("sleepScore was absent")
summary['lastActivities'] = activities
return {
**summary,
**body["totalAverage"],
"nextAlarm": alarms,
"gear": gear,
"gear_stats": gear_stats,
"activity_types": activity_types,
"gear_defaults": gear_defaults,
"sleepScore": sleep_score,
}
async def set_active_gear(self, entity, service_data):
"""Update Garmin Gear settings"""
if not await self.async_login():
raise IntegrationError(
"Failed to login to Garmin Connect, unable to update"
)
setting = service_data.data["setting"]
activity_type_id = next(
filter(
lambda a: a[GEAR.TYPE_KEY] == service_data.data["activity_type"],
self.data["activity_types"],
)
)[GEAR.TYPE_ID]
if setting != SERVICE_SETTING.ONLY_THIS_AS_DEFAULT:
await self.hass.async_add_executor_job(
self._api.set_gear_default,
activity_type_id,
entity.uuid,
setting == SERVICE_SETTING.DEFAULT,
)
else:
old_default_state = await self.hass.async_add_executor_job(
self._api.get_gear_defaults, self.data[GEAR.USERPROFILE_ID]
)
to_deactivate = list(
filter(
lambda o: o[GEAR.ACTIVITY_TYPE_PK] == activity_type_id
and o[GEAR.UUID] != entity.uuid,
old_default_state,
)
)
for active_gear in to_deactivate:
await self.hass.async_add_executor_job(
self._api.set_gear_default,
activity_type_id,
active_gear[GEAR.UUID],
False,
)
await self.hass.async_add_executor_job(
self._api.set_gear_default, activity_type_id, entity.uuid, True
)

View File

@@ -1,7 +1,7 @@
"""Config flow for Garmin Connect integration."""
import logging
from garminconnect_ha import (
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,

View File

@@ -1,33 +1,38 @@
"""Constants for the Garmin Connect integration."""
from datetime import timedelta
from enum import Enum
from typing import NamedTuple
from homeassistant.const import (
DEVICE_CLASS_TIMESTAMP,
LENGTH_METERS,
MASS_KILOGRAMS,
PERCENTAGE,
TIME_MINUTES,
TIME_YEARS,
)
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorStateClass,
)
DOMAIN = "garmin_connect"
ATTRIBUTION = "connect.garmin.com"
DATA_COORDINATOR = "coordinator"
DEFAULT_UPDATE_INTERVAL = timedelta(minutes=5)
GARMIN_ENTITY_LIST = {
"totalSteps": ["Total Steps", "steps", "mdi:walk", None, True],
"dailyStepGoal": ["Daily Step Goal", "steps", "mdi:walk", None, True],
"totalKilocalories": ["Total KiloCalories", "kcal", "mdi:food", None, True],
"activeKilocalories": ["Active KiloCalories", "kcal", "mdi:food", None, True],
"bmrKilocalories": ["BMR KiloCalories", "kcal", "mdi:food", None, True],
"consumedKilocalories": ["Consumed KiloCalories", "kcal", "mdi:food", None, False],
"burnedKilocalories": ["Burned KiloCalories", "kcal", "mdi:food", None, True],
"totalSteps": ["Total Steps", "steps", "mdi:walk", None, SensorStateClass.TOTAL, True],
"dailyStepGoal": ["Daily Step Goal", "steps", "mdi:walk", None, SensorStateClass.TOTAL, True],
"totalKilocalories": ["Total KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, True],
"activeKilocalories": ["Active KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, True],
"bmrKilocalories": ["BMR KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, True],
"consumedKilocalories": ["Consumed KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, False],
"burnedKilocalories": ["Burned KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, True],
"remainingKilocalories": [
"Remaining KiloCalories",
"kcal",
"mdi:food",
None,
SensorStateClass.TOTAL,
False,
],
"netRemainingKilocalories": [
@@ -35,36 +40,41 @@ GARMIN_ENTITY_LIST = {
"kcal",
"mdi:food",
None,
SensorStateClass.TOTAL,
False,
],
"netCalorieGoal": ["Net Calorie Goal", "cal", "mdi:food", None, False],
"netCalorieGoal": ["Net Calorie Goal", "kcal", "mdi:food", None, SensorStateClass.TOTAL, False],
"totalDistanceMeters": [
"Total Distance Mtr",
LENGTH_METERS,
"mdi:walk",
None,
SensorDeviceClass.DISTANCE,
SensorStateClass.TOTAL,
True,
],
"wellnessStartTimeLocal": [
"Wellness Start Time",
None,
"mdi:clock",
DEVICE_CLASS_TIMESTAMP,
SensorDeviceClass.TIMESTAMP,
None,
False,
],
"wellnessEndTimeLocal": [
"Wellness End Time",
None,
"mdi:clock",
DEVICE_CLASS_TIMESTAMP,
SensorDeviceClass.TIMESTAMP,
None,
False,
],
"wellnessDescription": ["Wellness Description", "", "mdi:clock", None, False],
"wellnessDescription": ["Wellness Description", "", "mdi:clock", None, SensorStateClass.TOTAL, False],
"wellnessDistanceMeters": [
"Wellness Distance Mtr",
LENGTH_METERS,
"mdi:walk",
None,
SensorDeviceClass.DISTANCE,
SensorStateClass.TOTAL,
False,
],
"wellnessActiveKilocalories": [
@@ -72,66 +82,74 @@ GARMIN_ENTITY_LIST = {
"kcal",
"mdi:food",
None,
SensorStateClass.TOTAL,
False,
],
"wellnessKilocalories": ["Wellness KiloCalories", "kcal", "mdi:food", None, False],
"wellnessKilocalories": ["Wellness KiloCalories", "kcal", "mdi:food", None, SensorStateClass.TOTAL, False],
"highlyActiveSeconds": [
"Highly Active Time",
TIME_MINUTES,
"mdi:fire",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
False,
],
"activeSeconds": ["Active Time", TIME_MINUTES, "mdi:fire", None, True],
"sedentarySeconds": ["Sedentary Time", TIME_MINUTES, "mdi:seat", None, True],
"sleepingSeconds": ["Sleeping Time", TIME_MINUTES, "mdi:sleep", None, True],
"activeSeconds": ["Active Time", TIME_MINUTES, "mdi:fire", None, SensorStateClass.TOTAL, True],
"sedentarySeconds": ["Sedentary Time", TIME_MINUTES, "mdi:seat", None, SensorStateClass.TOTAL, True],
"sleepingSeconds": ["Sleeping Time", TIME_MINUTES, "mdi:sleep", None, SensorStateClass.TOTAL, True],
"measurableAwakeDuration": [
"Awake Duration",
TIME_MINUTES,
"mdi:sleep",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"measurableAsleepDuration": [
"Sleep Duration",
TIME_MINUTES,
"mdi:sleep",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"floorsAscendedInMeters": [
"Floors Ascended Mtr",
LENGTH_METERS,
"mdi:stairs",
None,
SensorDeviceClass.DISTANCE,
SensorStateClass.TOTAL,
False,
],
"floorsDescendedInMeters": [
"Floors Descended Mtr",
LENGTH_METERS,
"mdi:stairs",
None,
SensorDeviceClass.DISTANCE,
SensorStateClass.TOTAL,
False,
],
"floorsAscended": ["Floors Ascended", "floors", "mdi:stairs", None, True],
"floorsDescended": ["Floors Descended", "floors", "mdi:stairs", None, True],
"floorsAscended": ["Floors Ascended", "floors", "mdi:stairs", None, SensorStateClass.TOTAL, True],
"floorsDescended": ["Floors Descended", "floors", "mdi:stairs", None, SensorStateClass.TOTAL, True],
"userFloorsAscendedGoal": [
"Floors Ascended Goal",
"floors",
"mdi:stairs",
None,
SensorStateClass.TOTAL,
True,
],
"minHeartRate": ["Min Heart Rate", "bpm", "mdi:heart-pulse", None, True],
"maxHeartRate": ["Max Heart Rate", "bpm", "mdi:heart-pulse", None, True],
"restingHeartRate": ["Resting Heart Rate", "bpm", "mdi:heart-pulse", None, True],
"minAvgHeartRate": ["Min Avg Heart Rate", "bpm", "mdi:heart-pulse", None, False],
"maxAvgHeartRate": ["Max Avg Heart Rate", "bpm", "mdi:heart-pulse", None, False],
"minHeartRate": ["Min Heart Rate", "bpm", "mdi:heart-pulse", None, SensorStateClass.TOTAL, True],
"maxHeartRate": ["Max Heart Rate", "bpm", "mdi:heart-pulse", None, SensorStateClass.TOTAL, True],
"restingHeartRate": ["Resting Heart Rate", "bpm", "mdi:heart-pulse", None, SensorStateClass.TOTAL, True],
"minAvgHeartRate": ["Min Avg Heart Rate", "bpm", "mdi:heart-pulse", None, SensorStateClass.TOTAL, False],
"maxAvgHeartRate": ["Max Avg Heart Rate", "bpm", "mdi:heart-pulse", None, SensorStateClass.TOTAL, False],
"abnormalHeartRateAlertsCount": [
"Abnormal HR Counts",
None,
"mdi:heart-pulse",
None,
SensorStateClass.TOTAL,
False,
],
"lastSevenDaysAvgRestingHeartRate": [
@@ -139,52 +157,59 @@ GARMIN_ENTITY_LIST = {
"bpm",
"mdi:heart-pulse",
None,
SensorStateClass.TOTAL,
False,
],
"averageStressLevel": ["Avg Stress Level", "lvl", "mdi:flash-alert", None, True],
"maxStressLevel": ["Max Stress Level", "lvl", "mdi:flash-alert", None, True],
"stressQualifier": ["Stress Qualifier", None, "mdi:flash-alert", None, False],
"stressDuration": ["Stress Duration", TIME_MINUTES, "mdi:flash-alert", None, False],
"averageStressLevel": ["Avg Stress Level", "lvl", "mdi:flash-alert", None, SensorStateClass.TOTAL, True],
"maxStressLevel": ["Max Stress Level", "lvl", "mdi:flash-alert", None, SensorStateClass.TOTAL, True],
"stressQualifier": ["Stress Qualifier", None, "mdi:flash-alert", None, None, False],
"stressDuration": ["Stress Duration", TIME_MINUTES, "mdi:flash-alert", None, SensorStateClass.TOTAL, False],
"restStressDuration": [
"Rest Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"activityStressDuration": [
"Activity Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"uncategorizedStressDuration": [
"Uncat. Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"totalStressDuration": [
"Total Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"lowStressDuration": [
"Low Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"mediumStressDuration": [
"Medium Stress Duration",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
True,
],
"highStressDuration": [
@@ -192,6 +217,7 @@ GARMIN_ENTITY_LIST = {
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
True,
],
"stressPercentage": [
@@ -199,6 +225,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"restStressPercentage": [
@@ -206,6 +233,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"activityStressPercentage": [
@@ -213,6 +241,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"uncategorizedStressPercentage": [
@@ -220,6 +249,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"lowStressPercentage": [
@@ -227,6 +257,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"mediumStressPercentage": [
@@ -234,6 +265,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"highStressPercentage": [
@@ -241,20 +273,23 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:flash-alert",
None,
SensorStateClass.TOTAL,
False,
],
"moderateIntensityMinutes": [
"Moderate Intensity",
TIME_MINUTES,
"mdi:flash-alert",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
False,
],
"vigorousIntensityMinutes": [
"Vigorous Intensity",
TIME_MINUTES,
"mdi:run-fast",
None,
SensorDeviceClass.DURATION,
SensorStateClass.TOTAL,
False,
],
"intensityMinutesGoal": [
@@ -262,6 +297,7 @@ GARMIN_ENTITY_LIST = {
TIME_MINUTES,
"mdi:run-fast",
None,
SensorStateClass.TOTAL,
False,
],
"bodyBatteryChargedValue": [
@@ -269,6 +305,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:battery-charging-100",
None,
SensorStateClass.TOTAL,
True,
],
"bodyBatteryDrainedValue": [
@@ -276,6 +313,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:battery-alert-variant-outline",
None,
SensorStateClass.TOTAL,
True,
],
"bodyBatteryHighestValue": [
@@ -283,6 +321,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:battery-heart",
None,
SensorStateClass.TOTAL,
True,
],
"bodyBatteryLowestValue": [
@@ -290,6 +329,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:battery-heart-outline",
None,
SensorStateClass.TOTAL,
True,
],
"bodyBatteryMostRecentValue": [
@@ -297,16 +337,18 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:battery-positive",
None,
SensorStateClass.TOTAL,
True,
],
"averageSpo2": ["Average SPO2", PERCENTAGE, "mdi:diabetes", None, True],
"lowestSpo2": ["Lowest SPO2", PERCENTAGE, "mdi:diabetes", None, True],
"latestSpo2": ["Latest SPO2", PERCENTAGE, "mdi:diabetes", None, True],
"averageSpo2": ["Average SPO2", PERCENTAGE, "mdi:diabetes", None, SensorStateClass.TOTAL, True],
"lowestSpo2": ["Lowest SPO2", PERCENTAGE, "mdi:diabetes", None, SensorStateClass.TOTAL, True],
"latestSpo2": ["Latest SPO2", PERCENTAGE, "mdi:diabetes", None, SensorStateClass.TOTAL, True],
"latestSpo2ReadingTimeLocal": [
"Latest SPO2 Time",
None,
"mdi:diabetes",
DEVICE_CLASS_TIMESTAMP,
SensorDeviceClass.TIMESTAMP,
None,
False,
],
"averageMonitoringEnvironmentAltitude": [
@@ -314,6 +356,7 @@ GARMIN_ENTITY_LIST = {
PERCENTAGE,
"mdi:image-filter-hdr",
None,
SensorStateClass.TOTAL,
False,
],
"highestRespirationValue": [
@@ -321,6 +364,7 @@ GARMIN_ENTITY_LIST = {
"brpm",
"mdi:progress-clock",
None,
SensorStateClass.TOTAL,
False,
],
"lowestRespirationValue": [
@@ -328,6 +372,7 @@ GARMIN_ENTITY_LIST = {
"brpm",
"mdi:progress-clock",
None,
SensorStateClass.TOTAL,
False,
],
"latestRespirationValue": [
@@ -335,23 +380,58 @@ GARMIN_ENTITY_LIST = {
"brpm",
"mdi:progress-clock",
None,
SensorStateClass.TOTAL,
False,
],
"latestRespirationTimeGMT": [
"Latest Respiration Update",
None,
"mdi:progress-clock",
DEVICE_CLASS_TIMESTAMP,
SensorDeviceClass.TIMESTAMP,
None,
False,
],
"weight": ["Weight", MASS_KILOGRAMS, "mdi:weight-kilogram", None, False],
"bmi": ["BMI", "bmi", "mdi:food", None, False],
"bodyFat": ["Body Fat", PERCENTAGE, "mdi:food", None, False],
"bodyWater": ["Body Water", PERCENTAGE, "mdi:water-percent", None, False],
"bodyMass": ["Body Mass", MASS_KILOGRAMS, "mdi:food", None, False],
"muscleMass": ["Muscle Mass", MASS_KILOGRAMS, "mdi:dumbbell", None, False],
"physiqueRating": ["Physique Rating", None, "mdi:numeric", None, False],
"visceralFat": ["Visceral Fat", PERCENTAGE, "mdi:food", None, False],
"metabolicAge": ["Metabolic Age", TIME_YEARS, "mdi:calendar-heart", None, False],
"nextAlarm": ["Next Alarm Time", None, "mdi:alarm", DEVICE_CLASS_TIMESTAMP, True],
"weight": ["Weight", MASS_KILOGRAMS, "mdi:weight-kilogram", SensorDeviceClass.WEIGHT, SensorStateClass.MEASUREMENT, False],
"bmi": ["BMI", "bmi", "mdi:food", None, SensorStateClass.TOTAL, False],
"bodyFat": ["Body Fat", PERCENTAGE, "mdi:food", None, SensorStateClass.TOTAL, False],
"bodyWater": ["Body Water", PERCENTAGE, "mdi:water-percent", None, SensorStateClass.TOTAL, False],
"boneMass": ["Bone Mass", MASS_KILOGRAMS, "mdi:bone", SensorDeviceClass.WEIGHT, SensorStateClass.MEASUREMENT, False],
"muscleMass": ["Muscle Mass", MASS_KILOGRAMS, "mdi:dumbbell", SensorDeviceClass.WEIGHT, SensorStateClass.MEASUREMENT, False],
"physiqueRating": ["Physique Rating", None, "mdi:numeric", None, SensorStateClass.TOTAL, False],
"visceralFat": ["Visceral Fat", PERCENTAGE, "mdi:food", None, SensorStateClass.TOTAL, False],
"metabolicAge": ["Metabolic Age", TIME_YEARS, "mdi:calendar-heart", None, SensorStateClass.TOTAL, False],
"nextAlarm": ["Next Alarm Time", None, "mdi:alarm", SensorDeviceClass.TIMESTAMP, None, True],
"lastActivities": ["Last Activities", None, "mdi:numeric", SensorStateClass.TOTAL, None, False],
"sleepScore": [
"Sleep Score",
None,
"mdi:sleep",
SensorStateClass.TOTAL,
SensorStateClass.MEASUREMENT,
True,
]
}
GEAR_ICONS = {
"Shoes": "mdi:shoe-sneaker",
"Bike": "mdi:bike",
"Other": "mdi:basketball",
"Golf Clubs": "mdi:golf",
}
class SERVICE_SETTING(NamedTuple):
"""Options for the service settings, see services.yaml"""
ONLY_THIS_AS_DEFAULT = "set this as default, unset others"
DEFAULT = "set as default"
UNSET_DEFAULT = "unset default"
class GEAR(NamedTuple):
UUID = "uuid"
TYPE_KEY = "typeKey"
TYPE_ID = "typeId"
USERPROFILE_ID = "userProfileId"
ACTIVITY_TYPE_PK = "activityTypePk"

View File

@@ -1,11 +1,11 @@
{
"domain": "garmin_connect",
"name": "Garmin Connect",
"documentation": "https://github.com/cyberjunky/home-assistant-garmin_connect",
"issue_tracker": "https://github.com/cyberjunky/home-assistant-garmin_connect/issues",
"requirements": ["garminconnect_ha==0.1.15"],
"codeowners": ["@cyberjunky"],
"config_flow": true,
"documentation": "https://github.com/cyberjunky/home-assistant-garmin_connect",
"issue_tracker": "https://github.com/cyberjunky/home-assistant-garmin_connect/issues",
"iot_class": "cloud_polling",
"version": "0.1.3"
"requirements": ["garminconnect==0.2.12", "tzlocal"],
"version": "0.2.18"
}

View File

@@ -2,23 +2,41 @@
from __future__ import annotations
import logging
import voluptuous as vol
from numbers import Number
from homeassistant.components.sensor import SensorEntity
import datetime
from tzlocal import get_localzone
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION, CONF_ID, DEVICE_CLASS_TIMESTAMP
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_ID,
DEVICE_CLASS_TIMESTAMP,
LENGTH_KILOMETERS,
)
from homeassistant.const import ATTR_ATTRIBUTION, CONF_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .alarm_util import calculate_next_active_alarms
from .const import (
ATTRIBUTION,
DATA_COORDINATOR,
DOMAIN as GARMIN_DOMAIN,
GARMIN_ENTITY_LIST,
GEAR,
GEAR_ICONS,
)
_LOGGER = logging.getLogger(__name__)
@@ -36,16 +54,17 @@ async def async_setup_entry(
entities = []
for (
sensor_type,
(name, unit, icon, device_class, enabled_by_default),
(name, unit, icon, device_class, state_class, enabled_by_default),
) in GARMIN_ENTITY_LIST.items():
_LOGGER.debug(
"Registering entity: %s, %s, %s, %s, %s, %s",
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
sensor_type,
name,
unit,
icon,
device_class,
state_class,
enabled_by_default,
)
entities.append(
@@ -57,11 +76,39 @@ async def async_setup_entry(
unit,
icon,
device_class,
state_class,
enabled_by_default,
)
)
if "gear" in coordinator.data:
for gear_item in coordinator.data["gear"]:
entities.append(
GarminConnectGearSensor(
coordinator,
unique_id,
gear_item[GEAR.UUID],
gear_item["gearTypeName"],
gear_item["displayName"],
None,
True,
)
)
async_add_entities(entities)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
"set_active_gear", ENTITY_SERVICE_SCHEMA, coordinator.set_active_gear
)
ENTITY_SERVICE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): str,
vol.Required("activity_type"): str,
vol.Required("setting"): str,
}
)
class GarminConnectSensor(CoordinatorEntity, SensorEntity):
@@ -76,6 +123,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
unit,
icon,
device_class,
state_class,
enabled_default: bool = True,
):
"""Initialize a Garmin Connect sensor."""
@@ -83,25 +131,24 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
self._unique_id = unique_id
self._type = sensor_type
self._name = name
self._unit = unit
self._icon = icon
self._device_class = device_class
self._state_class = state_class
self._enabled_default = enabled_default
@property
def name(self):
"""Return the name of the sensor."""
return self._name
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
@property
def icon(self):
"""Return the icon to use in the frontend."""
return self._icon
@property
def state(self):
def native_value(self):
"""Return the state of the sensor."""
if self._type == "lastActivities":
return len(self.coordinator.data[self._type])
if not self.coordinator.data or not self.coordinator.data[self._type]:
return None
@@ -115,24 +162,22 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
self.coordinator.data[self._type]
)
if active_alarms:
value = active_alarms[0]
date_time_obj = datetime.datetime.strptime(active_alarms[0], "%Y-%m-%dT%H:%M:%S")
tz = get_localzone()
timezone_date_time_obj = date_time_obj.replace(tzinfo=tz)
return timezone_date_time_obj
else:
value = None
return None
elif self._type == "stressQualifier":
return value
if self._device_class == DEVICE_CLASS_TIMESTAMP:
return value
if self._device_class == SensorDeviceClass.TIMESTAMP:
date_time_obj = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
tz = get_localzone()
timezone_date_time_obj = date_time_obj.replace(tzinfo=tz)
return timezone_date_time_obj
return round(value, 2)
@property
def unique_id(self) -> str:
"""Return the unique ID for this sensor."""
return f"{self._unique_id}_{self._type}"
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit
return round(value, 2) if isinstance(value, Number) else value
@property
def extra_state_attributes(self):
@@ -141,10 +186,12 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
return {}
attributes = {
"source": self.coordinator.data["source"],
"last_synced": self.coordinator.data["lastSyncTimestampGMT"],
ATTR_ATTRIBUTION: ATTRIBUTION,
}
if self._type == "lastActivities":
attributes["last_Activities"] = self.coordinator.data[self._type]
if self._type == "nextAlarm":
attributes["next_alarms"] = calculate_next_active_alarms(
self.coordinator.data[self._type]
@@ -175,7 +222,133 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
and self._type in self.coordinator.data
)
class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Garmin Connect Sensor."""
def __init__(
self,
coordinator,
unique_id,
uuid,
sensor_type,
name,
device_class: None,
enabled_default: bool = True,
):
"""Initialize a Garmin Connect sensor."""
super().__init__(coordinator)
self._unique_id = unique_id
self._type = sensor_type
self._uuid = uuid
self._device_class = device_class
self._enabled_default = enabled_default
self._attr_name = name
self._attr_device_class = self._device_class
self._attr_icon = GEAR_ICONS[sensor_type]
self._attr_native_unit_of_measurement = LENGTH_KILOMETERS
self._attr_unique_id = f"{self._unique_id}_{self._uuid}"
self._attr_state_class = SensorStateClass.TOTAL
self._attr_device_class = "garmin_gear"
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._device_class
def uuid(self):
"""Return the entity uuid"""
return self._uuid
@property
def native_value(self):
"""Return the state of the sensor."""
if not self.coordinator.data or not self._stats():
return None
value = self._stats()["totalDistance"]
return round(value / 1000, 2)
@property
def extra_state_attributes(self):
"""Return attributes for sensor."""
gear = self._gear()
stats = self._stats()
gear_defaults = self._gear_defaults()
activity_types = self.coordinator.data["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 {}
attributes = {
"last_synced": self.coordinator.data["lastSyncTimestampGMT"],
"total_activities": stats["totalActivities"],
"create_date": stats["createDate"],
"update_date": stats["updateDate"],
"date_begin": gear["dateBegin"],
"date_end": gear["dateEnd"],
"gear_make_name": gear["gearMakeName"],
"gear_model_name": gear["gearModelName"],
"gear_status_name": gear["gearStatusName"],
"custom_make_model": gear["customMakeModel"],
"maximum_meters": gear["maximumMeters"],
}
attributes["default_for_activity"] = (
", ".join(default_for_activity) if default_for_activity else "None"
)
return attributes
def _activity_names_for_gear_defaults(self, gear_defaults, activity_types):
return list(
map(
lambda b: b["typeKey"],
filter(
lambda a: a["typeId"]
in map(lambda d: d["activityTypePk"], gear_defaults),
activity_types,
),
)
)
@property
def device_info(self) -> DeviceInfo:
"""Return device information."""
return {
"identifiers": {(GARMIN_DOMAIN, self._unique_id)},
"name": "Garmin Connect",
"manufacturer": "Garmin Connect",
}
@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._gear()
def _stats(self):
"""Get gear statistics from garmin"""
for gear_stats_item in self.coordinator.data["gear_stats"]:
if gear_stats_item[GEAR.UUID] == self._uuid:
return gear_stats_item
def _gear(self):
"""Get gear from garmin"""
for gear_item in self.coordinator.data["gear"]:
if gear_item[GEAR.UUID] == self._uuid:
return gear_item
def _gear_defaults(self):
"""Get gear defaults"""
return list(
filter(
lambda d: d[GEAR.UUID] == self.uuid and d["defaultGear"] is True,
self.coordinator.data["gear_defaults"],
)
)

View File

@@ -0,0 +1,39 @@
set_active_gear:
name: Set active gear for activity
# target:
# entity:
# integration: "garmin_connect"
fields:
activity_type:
required: true
name: activity type
description: garmin activity type
example: running
default: running
selector:
select:
options:
- running
- cycling
- hiking
- other
- walking
- swimming
setting:
required: true
name: setting
description: gear setting to apply
default: set this as default, unset others
selector:
select:
options:
- set this as default, unset others
- set as default
- unset default
entity_id:
description: entity
required: true
selector:
entity:
integration: garmin_connect
device_class: garmin_gear

View File

@@ -15,8 +15,7 @@
"password": "[%key:common::config_flow::data::password%]",
"username": "[%key:common::config_flow::data::username%]"
},
"description": "Enter your credentials.",
"title": "Garmin Connect"
"description": "Enter your credentials."
}
}
}

View File

@@ -15,9 +15,8 @@
"password": "Password",
"username": "Username"
},
"description": "Enter your credentials.",
"title": "Garmin Connect"
"description": "Enter your credentials."
}
}
}
}
}

View File

@@ -6,7 +6,7 @@
"error": {
"cannot_connect": "Falha na liga\u00e7\u00e3o",
"invalid_auth": "Autentica\u00e7\u00e3o inv\u00e1lida",
"unknown": "Erro inesperado"
"unknown": "Erro desconhecido"
},
"step": {
"user": {
@@ -19,4 +19,4 @@
}
}
}
}
}

View File

@@ -0,0 +1,23 @@
{
"config": {
"abort": {
"already_configured": "Účet je už nakonfigurovaný"
},
"error": {
"cannot_connect": "Nepodarilo sa pripojiť",
"invalid_auth": "Neplatné overenie",
"too_many_requests": "Príliš veľa žiadostí, skúste to neskôr.",
"unknown": "Neočakávaná chyba"
},
"step": {
"user": {
"data": {
"password": "Heslo",
"username": "Užívateľské meno"
},
"description": "Zadajte svoje poverenia.",
"title": "Garmin Connect"
}
}
}
}

View File

@@ -1,7 +1,5 @@
{
"name": "Garmin Connect",
"country": "NL",
"render_readme": true,
"domains": ["sensor"]
}
}