From ad5f0426a13928497a4bbb7fbac81e64854e2f34 Mon Sep 17 00:00:00 2001 From: YorkshireIoT Date: Wed, 22 Nov 2023 12:20:15 +0000 Subject: [PATCH 1/2] Coordinator should return fetched data for first refresh --- custom_components/google_fit/coordinator.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/google_fit/coordinator.py b/custom_components/google_fit/coordinator.py index 45045de..d5a1098 100644 --- a/custom_components/google_fit/coordinator.py +++ b/custom_components/google_fit/coordinator.py @@ -16,7 +16,6 @@ from .api import AsyncConfigEntryAuth, GoogleFitParse from .api_types import ( - FitService, FitnessData, FitnessObject, FitnessDataPoint, @@ -97,12 +96,15 @@ def _get_interval(self, interval_period: int = 0) -> str: now = int(datetime.today().timestamp() * NANOSECONDS_SECONDS_CONVERSION) return f"{start}-{now}" - async def _async_update_data(self) -> FitService | None: + async def _async_update_data(self) -> FitnessData | None: """Update data via library.""" LOGGER.debug( "Fetching data for account %s", self._auth.oauth_session.config_entry.unique_id, ) + + # Start by initialising data to None + self.fitness_data = None try: async with async_timeout.timeout(30): service = await self._auth.get_resource(self.hass) @@ -175,8 +177,8 @@ def _get_session(activity_id: int) -> FitnessSessionResponse: f"Unknown sensor type for {entity.data_key}. Got: {type(entity)}" ) + # Update globally stored data with fetched and parsed data self.fitness_data = parser.fit_data - except HttpError as err: if 400 <= err.status_code < 500: raise ConfigEntryAuthFailed( @@ -185,3 +187,5 @@ def _get_session(activity_id: int) -> FitnessSessionResponse: raise err except Exception as err: raise UpdateFailed(f"Error communicating with API: {err}") from err + + return self.fitness_data From 65b8d2362da123306062ae04fd09c05e9d35b439 Mon Sep 17 00:00:00 2001 From: YorkshireIoT Date: Wed, 22 Nov 2023 12:20:42 +0000 Subject: [PATCH 2/2] After integration init, attempt to immediately fetch data --- custom_components/google_fit/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/custom_components/google_fit/__init__.py b/custom_components/google_fit/__init__.py index 6cd31f8..69a48c8 100644 --- a/custom_components/google_fit/__init__.py +++ b/custom_components/google_fit/__init__.py @@ -49,6 +49,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setup(entry, PLATFORM) entry.async_on_unload(entry.add_update_listener(update_listener)) + # Attempt to retrieve values immediately, not waiting for first + # time interval to pass + await coordinator.async_config_entry_first_refresh() + return True