Skip to content
This repository was archived by the owner on Aug 27, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions custom_components/o365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ def setup(hass, config):
CONFIG_SCHEMA(conf)
credentials = (conf.get(CONF_CLIENT_ID), conf.get(CONF_CLIENT_SECRET))
alt_config = conf.get(CONF_ALT_CONFIG)
if not alt_config:
if alt_config:
callback_url = AUTH_CALLBACK_PATH_ALT

else:
try:
callback_url = f"{get_url(hass)}{AUTH_CALLBACK_PATH}"
except NameError:
callback_url = f"{hass.config.api.base_url}{AUTH_CALLBACK_PATH}"
else:
callback_url = AUTH_CALLBACK_PATH_ALT

account = Account(credentials, token_backend=TOKEN_BACKEND)
is_authenticated = account.is_authenticated
permissions = validate_permissions()
if not is_authenticated or not permissions:
if is_authenticated and permissions:
do_setup(hass, conf, account)

else:
Comment on lines -43 to +57
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function setup refactored with the following changes:

  • Simplify logical expression

url, state = account.con.get_authorization_url(
requested_scopes=SCOPE, redirect_uri=callback_url
)
Expand All @@ -65,9 +68,6 @@ def setup(hass, config):
else:
request_configuration(hass, conf, url, callback_view)
return True
else:
do_setup(hass, conf, account)

return True


Expand Down
3 changes: 1 addition & 2 deletions custom_components/o365/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ async def async_get_service(hass, config, discovery_info=None):
is_authenticated = account.is_authenticated
if not is_authenticated:
return
email_service = O365EmailService(account)
return email_service
return O365EmailService(account)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function async_get_service refactored with the following changes:

  • Inline variable that is only used once



class O365EmailService(BaseNotificationService):
Expand Down
9 changes: 5 additions & 4 deletions custom_components/o365/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ def clean_html(html):

def validate_permissions(token_path=DEFAULT_CACHE_PATH, token_filename="o365.token"):
full_token_path = os.path.join(token_path, token_filename)
if not os.path.exists(full_token_path) or not os.path.isfile(full_token_path):
if not (
os.path.exists(full_token_path) and os.path.isfile(full_token_path)
):
_LOGGER.warning(f"Could not loacte token at {full_token_path}")
return False
with open(full_token_path, "r", encoding="UTF-8") as fh:
raw = fh.read()
permissions = json.loads(raw)["scope"]
scope = [x for x in MINIMUM_REQUIRED_SCOPES]
all_permissions_granted = all([x in permissions for x in scope])
all_permissions_granted = all(x in permissions for x in scope)
Comment on lines -40 to +49
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_permissions refactored with the following changes:

  • Replace unneeded comprehension with generator
  • Simplify logical expression

if not all_permissions_granted:
_LOGGER.warning(f"All permissions granted: {all_permissions_granted}")
return all_permissions_granted
Expand Down Expand Up @@ -191,7 +193,7 @@ def load_calendars(path):

def get_calendar_info(hass, calendar, track_new_devices):
"""Convert data from O365 into DEVICE_SCHEMA."""
calendar_info = CALENDAR_DEVICE_SCHEMA(
return CALENDAR_DEVICE_SCHEMA(
Comment on lines -194 to +196
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_calendar_info refactored with the following changes:

  • Inline variable that is only used once

{
CONF_CAL_ID: calendar.calendar_id,
CONF_ENTITIES: [
Expand All @@ -203,7 +205,6 @@ def get_calendar_info(hass, calendar, track_new_devices):
],
}
)
return calendar_info


def update_calendar_file(path, calendar, hass, track_new_devices):
Expand Down