From 252f67ff7fc29a5f684b3a05b499b60b9701212e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Franc=CC=A7ois=20Paris?= Date: Tue, 22 Oct 2024 12:27:02 +0100 Subject: [PATCH] Add a sensor to retreive HRV status when available --- custom_components/garmin_connect/__init__.py | 13 +++++++++++++ custom_components/garmin_connect/const.py | 11 +++++++++-- custom_components/garmin_connect/sensor.py | 9 ++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/custom_components/garmin_connect/__init__.py b/custom_components/garmin_connect/__init__.py index a383536..c756ee2 100644 --- a/custom_components/garmin_connect/__init__.py +++ b/custom_components/garmin_connect/__init__.py @@ -112,6 +112,8 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): activity_types = {} sleep_data = {} sleep_score = None + hrv_data = {} + hrvStatus = None try: summary = await self.hass.async_add_executor_job( @@ -147,6 +149,10 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): sleep_data = await self.hass.async_add_executor_job( self._api.get_sleep_data, date.today().isoformat()) _LOGGER.debug(f"Sleep data: {sleep_data}") + + hrv_data = await self.hass.async_add_executor_job( + self._api.get_hrv_data, date.today().isoformat()) + _LOGGER.debug(f"hrv data: {hrv_data}") except ( GarminConnectAuthenticationError, GarminConnectTooManyRequestsError, @@ -185,6 +191,12 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): except KeyError: _LOGGER.debug("Sleep score data is not available") + try: + hrvStatus = hrv_data["hrvSummary"] + _LOGGER.debug(f"HRV status: {hrvStatus} ") + except KeyError: + _LOGGER.debug("HRV data is not available") + return { **summary, **body["totalAverage"], @@ -194,6 +206,7 @@ class GarminConnectDataUpdateCoordinator(DataUpdateCoordinator): "activity_types": activity_types, "gear_defaults": gear_defaults, "sleepScore": sleep_score, + "hrvStatus": hrvStatus, } 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 2381010..acf05d3 100644 --- a/custom_components/garmin_connect/const.py +++ b/custom_components/garmin_connect/const.py @@ -408,8 +408,15 @@ GARMIN_ENTITY_LIST = { SensorStateClass.TOTAL, SensorStateClass.MEASUREMENT, True, - ] - + ], + "hrvStatus": [ + "HRV Status", + None, + "mdi:heart-pulse", + None, + None, + True, + ], } GEAR_ICONS = { diff --git a/custom_components/garmin_connect/sensor.py b/custom_components/garmin_connect/sensor.py index 831eb52..b8ee745 100644 --- a/custom_components/garmin_connect/sensor.py +++ b/custom_components/garmin_connect/sensor.py @@ -172,6 +172,9 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity): if self._type == "badges": return len(self.coordinator.data[self._type]) + if self._type == "hrvStatus": + return self.coordinator.data[self._type]["status"] + if not self.coordinator.data or not self.coordinator.data[self._type]: return None @@ -223,6 +226,10 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity): self.coordinator.data[self._type] ) + if self._type == "hrvStatus": + attributes = {**attributes, **self.coordinator.data[self._type]} + del attributes["status"] + return attributes @property @@ -377,4 +384,4 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity): lambda d: d[GEAR.UUID] == self.uuid and d["defaultGear"] is True, self.coordinator.data["gear_defaults"], ) - ) \ No newline at end of file + )