Skip to content

Commit

Permalink
Code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Nov 13, 2022
1 parent fd2d642 commit 2067cf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions custom_components/o365/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def __init__(self, conf, mail_folder):
self._query = self._mail_folder.new_query()
self._query.order_by("receivedDateTime", ascending=False)

self._build_query()

def _build_query(self):
if (
self._body_contains is not None
or self._subject_contains is not None
Expand Down
21 changes: 15 additions & 6 deletions custom_components/o365/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ def build_minimum_permissions(hass, config, conf_type):
if len(todo_sensors) > 0 and todo_sensors.get(CONF_ENABLED, False):
minimum_permissions.append(PERM_MINIMUM_TASKS)

yaml_filename = build_yaml_filename(config, conf_type)
calendars = load_calendars(build_config_file_path(hass, yaml_filename))
for calendar in calendars:
if calendar.startswith(CONST_GROUP):
minimum_permissions.append(PERM_MINIMUM_GROUP)
break
if group_permissions_required(hass, config, conf_type):
minimum_permissions.append(PERM_MINIMUM_GROUP)

return minimum_permissions


Expand Down Expand Up @@ -141,6 +138,18 @@ def build_requested_permissions(config):
return scope


def group_permissions_required(hass, config, conf_type):
"""Return if group permissions are required."""
yaml_filename = build_yaml_filename(config, conf_type)
calendars = load_calendars(build_config_file_path(hass, yaml_filename))
for cal_id, calendar in calendars.items():
if cal_id.startswith(CONST_GROUP):
for entity in calendar.get(CONF_ENTITIES):
if entity[CONF_TRACK]:
return True
return False


def validate_permissions(
hass, minimum_permissions, token_path=DEFAULT_CACHE_PATH, filename=TOKEN_FILENAME
):
Expand Down

0 comments on commit 2067cf7

Please sign in to comment.