Skip to content

Commit

Permalink
fix: Fix issue of o365 library accessing token within the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Aug 16, 2024
1 parent 5096ada commit 1fae1a7
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions custom_components/ms365_todo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Main initialisation code."""

import functools as ft
import logging

from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -37,8 +36,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: MS365ConfigEntry):
perms = Permissions(hass, entry.data)
permissions, failed_permissions = await perms.async_check_authorizations() # pylint: disable=unused-variable
if permissions is True:
account, is_authenticated = await _async_try_authentication(
hass, perms, credentials, main_resource
account, is_authenticated = await hass.async_add_executor_job(
_try_authentication, perms, credentials, main_resource
)
else:
is_authenticated = False
Expand Down Expand Up @@ -82,24 +81,19 @@ async def async_reload_entry(hass: HomeAssistant, entry: MS365ConfigEntry) -> No
await hass.config_entries.async_reload(entry.entry_id)


async def _async_try_authentication(hass, perms, credentials, main_resource):
def _try_authentication(perms, credentials, main_resource):
_LOGGER.debug("Setup token")
token_backend = await hass.async_add_executor_job(
ft.partial(
FileSystemTokenBackend,
token_path=perms.token_path,
token_filename=perms.token_filename,
)
token_backend = FileSystemTokenBackend(
token_path=perms.token_path,
token_filename=perms.token_filename,
)

_LOGGER.debug("Setup account")
account = await hass.async_add_executor_job(
ft.partial(
Account,
credentials,
token_backend=token_backend,
timezone=CONST_UTC_TIMEZONE,
main_resource=main_resource,
)
account = Account(
credentials,
token_backend=token_backend,
timezone=CONST_UTC_TIMEZONE,
main_resource=main_resource,
)

return account, account.is_authenticated
Expand Down

0 comments on commit 1fae1a7

Please sign in to comment.