From 4e01cbff3cc20623c23c7d8c753fb825f106aec0 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Tue, 23 Jun 2020 17:10:41 +0100 Subject: [PATCH] Refactored by Sourcery --- custom_components/o365/__init__.py | 16 ++++++++-------- custom_components/o365/notify.py | 3 +-- custom_components/o365/utils.py | 9 +++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/custom_components/o365/__init__.py b/custom_components/o365/__init__.py index 47c9284..e053427 100644 --- a/custom_components/o365/__init__.py +++ b/custom_components/o365/__init__.py @@ -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: url, state = account.con.get_authorization_url( requested_scopes=SCOPE, redirect_uri=callback_url ) @@ -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 diff --git a/custom_components/o365/notify.py b/custom_components/o365/notify.py index 0299862..b19885f 100644 --- a/custom_components/o365/notify.py +++ b/custom_components/o365/notify.py @@ -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) class O365EmailService(BaseNotificationService): diff --git a/custom_components/o365/utils.py b/custom_components/o365/utils.py index e68bd29..1f40ae0 100644 --- a/custom_components/o365/utils.py +++ b/custom_components/o365/utils.py @@ -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) if not all_permissions_granted: _LOGGER.warning(f"All permissions granted: {all_permissions_granted}") return all_permissions_granted @@ -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( { CONF_CAL_ID: calendar.calendar_id, CONF_ENTITIES: [ @@ -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):