Skip to content

Commit

Permalink
Merge pull request #141 from YorkshireIoT/140-refresh-sensor-values-o…
Browse files Browse the repository at this point in the history
…n-startup

140 refresh sensor values on integration startup
  • Loading branch information
YorkshireIoT authored Nov 22, 2023
2 parents 6fbe42b + 65b8d23 commit d90099d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions custom_components/google_fit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 7 additions & 3 deletions custom_components/google_fit/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from .api import AsyncConfigEntryAuth, GoogleFitParse
from .api_types import (
FitService,
FitnessData,
FitnessObject,
FitnessDataPoint,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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

0 comments on commit d90099d

Please sign in to comment.