From e9fba7abbec33cc7a62a3fbe0c1374459f7dc2a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Franc=CC=A7ois=20Paris?= Date: Fri, 23 Jun 2023 23:34:24 +0100 Subject: [PATCH] Pull sleepscore from Garmin interface --- custom_components/garmin_connect/__init__.py | 8 ++++++++ custom_components/garmin_connect/const.py | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/custom_components/garmin_connect/__init__.py b/custom_components/garmin_connect/__init__.py index ae18964..8dab2cd 100644 --- a/custom_components/garmin_connect/__init__.py +++ b/custom_components/garmin_connect/__init__.py @@ -133,6 +133,8 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): 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, @@ -144,6 +146,11 @@ 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 { @@ -154,6 +161,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): "gear_stats": gear_stats, "activity_types": activity_types, "gear_defaults": gear_defaults, + "sleepScore": sleep_score, } async def set_active_gear(self, entity, service_data): diff --git a/custom_components/garmin_connect/const.py b/custom_components/garmin_connect/const.py index a49cd18..6b6bda6 100644 --- a/custom_components/garmin_connect/const.py +++ b/custom_components/garmin_connect/const.py @@ -401,7 +401,15 @@ GARMIN_ENTITY_LIST = { "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] + "lastActivities": ["Last Activities", None, "mdi:numeric", SensorStateClass.TOTAL, None, False], + "sleepScore": [ + "Sleeep Score", + None, + "mdi:sleep", + SensorStateClass.TOTAL, + SensorStateClass.MEASUREMENT, + True, + ] }