Skip to content

Commit

Permalink
Merge pull request #14 from AfonsoFGarcia/fix-loop-errors
Browse files Browse the repository at this point in the history
Fix timeout errors when running within hass loop
  • Loading branch information
AfonsoFGarcia authored Oct 4, 2024
2 parents 0880edc + 417dc6a commit f7e4794
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
44 changes: 28 additions & 16 deletions custom_components/bluecon/ConfigFolderNotificationInfoStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,37 @@ def __init__(self, hass: HomeAssistant):
self.__persistentIdsStore = Store(hass=hass, version=1, key=f"{DOMAIN}.PERSISTENT_IDS")

async def retrieveCredentials(self) -> dict[str, dict[str, Any]] | None:
return asyncio.run_coroutine_threadsafe(
self.__credentialsStore.async_load(), self.__hass.loop
).result()
if asyncio.get_running_loop() == self.__hass.loop:
return self.__credentialsStore.async_load()
else:
return asyncio.run_coroutine_threadsafe(
self.__credentialsStore.async_load(), self.__hass.loop
).result(timeout=2)

async def storeCredentials(self, credentials: dict[str, dict[str, Any]]):
asyncio.run_coroutine_threadsafe(
self.__credentialsStore.async_save(credentials), self.__hass.loop
).result()
if asyncio.get_running_loop() == self.__hass.loop:
self.__credentialsStore.async_save(credentials)
else:
asyncio.run_coroutine_threadsafe(
self.__credentialsStore.async_save(credentials), self.__hass.loop
).result(timeout=2)

async def retrievePersistentIds(self) -> list[str] | None:
return asyncio.run_coroutine_threadsafe(
self.__persistentIdsStore.async_load(), self.__hass.loop
).result()
if asyncio.get_running_loop() == self.__hass.loop:
return self.__persistentIdsStore.async_load()
else:
return asyncio.run_coroutine_threadsafe(
self.__persistentIdsStore.async_load(), self.__hass.loop
).result(timeout=2)

async def storePersistentId(self, persistentId: str):
persistentIds = asyncio.run_coroutine_threadsafe(
self.retrievePersistentIds(), self.__hass.loop
).result() or []
persistentId.append(persistentId)
asyncio.run_coroutine_threadsafe(
self.__persistentIdsStore.async_save(persistentIds), self.__hass.loop
).result()
persistentIds = await self.retrievePersistentIds()
if type(persistentIds) is not list:
persistentIds = []
persistentIds.append(persistentId)
if asyncio.get_running_loop() == self.__hass.loop:
self.__persistentIdsStore.async_save(persistentIds)
else:
asyncio.run_coroutine_threadsafe(
self.__persistentIdsStore.async_save(persistentIds), self.__hass.loop
).result(timeout=2)
20 changes: 14 additions & 6 deletions custom_components/bluecon/ConfigFolderOAuthTokenStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ def __init__(self, hass: HomeAssistant):
self.__oAuthTokenStore = Store(hass=hass, version=1, key=f"{DOMAIN}.OAUTH_TOKEN")

async def retrieveOAuthToken(self) -> OAuthToken:
return OAuthToken.fromJson(asyncio.run_coroutine_threadsafe(
self.__oAuthTokenStore.async_load(), self.__hass.loop
).result())
if asyncio.get_running_loop() == self.__hass.loop:
json = await self.__oAuthTokenStore.async_load()
else:
json = asyncio.run_coroutine_threadsafe(
self.__oAuthTokenStore.async_load(), self.__hass.loop
).result(timeout=2)
return OAuthToken.fromJson(json)

async def storeOAuthToken(self, oAuthToken: OAuthToken):
asyncio.run_coroutine_threadsafe(
self.__oAuthTokenStore.async_save(oAuthToken.toJson()), self.__hass.loop
).result()
json = oAuthToken.toJson()
if asyncio.get_running_loop() == self.__hass.loop:
await self.__oAuthTokenStore.async_save(json)
else:
asyncio.run_coroutine_threadsafe(
self.__oAuthTokenStore.async_save(oAuthToken.toJson()), self.__hass.loop
).result(timeout=2)
2 changes: 1 addition & 1 deletion custom_components/bluecon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def notification_callback(notification: INotification):
)

if entry.data.get(CONF_SENDER_ID, None) is not None and entry.data.get(CONF_API_KEY, None) is not None and entry.data.get(CONF_PROJECT_ID, None) is not None and entry.data.get(CONF_APP_ID, None) is not None and entry.data.get(CONF_PACKAGE_NAME, None) is not None:
await hass.async_add_executor_job(bluecon.startNotificationListener)
bluecon.startNotificationListener()

@callback
async def cleanup(event):
Expand Down

0 comments on commit f7e4794

Please sign in to comment.