Skip to content

Commit

Permalink
maint: Minor code tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Sep 11, 2023
1 parent b29493e commit 04a7c8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
10 changes: 5 additions & 5 deletions custom_components/o365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
CONST_CONFIG_TYPE_LIST,
CONST_PRIMARY,
CONST_UTC_TIMEZONE,
DEFAULT_CACHE_PATH,
DOMAIN,
LEGACY_ACCOUNT_NAME,
O365_STORAGE_TOKEN,
TOKEN_FILE_MISSING,
TOKEN_FILENAME,
YAML_CALENDARS,
Expand Down Expand Up @@ -136,9 +136,9 @@ async def _async_setup_account(hass, account_conf, conf_type):
if not _validate_shared_schema(account_name, main_resource, account_conf):
return

token_path = build_config_file_path(hass, DEFAULT_CACHE_PATH)
token_path = build_config_file_path(hass, O365_STORAGE_TOKEN)
token_file = build_token_filename(account_conf, conf_type)
check_file_location(hass, DEFAULT_CACHE_PATH, token_path)
check_file_location(hass, O365_STORAGE_TOKEN, token_path)
token_backend = await hass.async_add_executor_job(
ft.partial(
FileSystemTokenBackend, token_path=token_path, token_filename=token_file
Expand Down Expand Up @@ -222,8 +222,8 @@ def _validate_shared_schema(account_name, main_account, config):
def _copy_token_file(hass, account_name):
old_file = TOKEN_FILENAME.format("")
new_file = TOKEN_FILENAME.format(f"_{account_name}")
old_filepath = build_config_file_path(hass, f"{DEFAULT_CACHE_PATH}/{old_file}")
new_filepath = build_config_file_path(hass, f"{DEFAULT_CACHE_PATH}/{new_file}")
old_filepath = build_config_file_path(hass, f"{O365_STORAGE_TOKEN}/{old_file}")
new_filepath = build_config_file_path(hass, f"{O365_STORAGE_TOKEN}/{new_file}")
if os.path.exists(old_filepath):
shutil.copy(src=old_filepath, dst=new_filepath)

Expand Down
32 changes: 16 additions & 16 deletions custom_components/o365/classes/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CONF_TODO_SENSORS,
CONF_TRACK,
CONST_GROUP,
DEFAULT_CACHE_PATH,
O365_STORAGE_TOKEN,
PERM_CALENDARS_READ,
PERM_CALENDARS_READBASIC,
PERM_CALENDARS_READWRITE,
Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(self, hass, config, conf_type):
self._minimum_permissions = []
self._requested_permissions = []
self._filename = build_token_filename(config, self._conf_type)
self._token_path = DEFAULT_CACHE_PATH
self._token_path = O365_STORAGE_TOKEN
self._permissions = []

@property
Expand Down Expand Up @@ -111,23 +111,10 @@ def requested_permissions(self):
def permissions(self):
"""Return the permission set."""
if not self._permissions:
self._permissions = self.get_permissions()
self._permissions = self._get_permissions()

return self._permissions

def get_permissions(self):
"""Get the permissions from the token file."""
config_path = build_config_file_path(self._hass, self._token_path)
full_token_path = os.path.join(config_path, self._filename)
if not os.path.exists(full_token_path) or not os.path.isfile(full_token_path):
_LOGGER.warning("Could not locate token at %s", full_token_path)
return TOKEN_FILE_MISSING
with open(full_token_path, "r", encoding="UTF-8") as file_handle:
raw = file_handle.read()
permissions = json.loads(raw)["scope"]

return permissions

def validate_permissions(self):
"""Validate the permissions."""
if self.permissions == TOKEN_FILE_MISSING:
Expand Down Expand Up @@ -157,6 +144,19 @@ def validate_minimum_permission(self, minimum_perm):
alternate_perm in self.permissions for alternate_perm in minimum_perm[1]
)

def _get_permissions(self):
"""Get the permissions from the token file."""
config_path = build_config_file_path(self._hass, self._token_path)
full_token_path = os.path.join(config_path, self._filename)
if not os.path.exists(full_token_path) or not os.path.isfile(full_token_path):
_LOGGER.warning("Could not locate token at %s", full_token_path)
return TOKEN_FILE_MISSING
with open(full_token_path, "r", encoding="UTF-8") as file_handle:
raw = file_handle.read()
permissions = json.loads(raw)["scope"]

return permissions

def _build_email_min_permissions(self):
email_sensors = self._config.get(CONF_EMAIL_SENSORS, [])
query_sensors = self._config.get(CONF_QUERY_SENSORS, [])
Expand Down
10 changes: 3 additions & 7 deletions custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ class EventResponse(Enum):
AUTH_CALLBACK_PATH_DEFAULT = (
"https://login.microsoftonline.com/common/oauth2/nativeclient"
)
CALENDAR_DOMAIN = "calendar"
CALENDAR_ENTITY_ID_FORMAT = CALENDAR_DOMAIN + ".{}"
CALENDAR_ENTITY_ID_FORMAT = "calendar.{}"
CONF_ACCOUNT = "account"
CONF_ACCOUNTS = "accounts"
CONF_ACCOUNT_CONF = "account_conf"
Expand Down Expand Up @@ -121,8 +120,6 @@ class EventResponse(Enum):
CONST_PRIMARY = "$o365-primary$"
CONST_UTC_TIMEZONE = "UTC"
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
DEFAULT_CACHE_PATH = ".O365-token-cache"
DEFAULT_NAME = "O365"
DEFAULT_OFFSET = "!!"
DOMAIN = "o365"

Expand All @@ -142,14 +139,14 @@ class EventResponse(Enum):

LEGACY_ACCOUNT_NAME = "converted"
O365_STORAGE = "o365_storage"
O365_STORAGE_TOKEN = ".O365-token-cache"
PERM_CALENDARS_READ = "Calendars.Read"
PERM_CALENDARS_READBASIC = "Calendars.ReadBasic"
PERM_CALENDARS_READ_SHARED = "Calendars.Read.Shared"
PERM_CALENDARS_READWRITE = "Calendars.ReadWrite"
PERM_CALENDARS_READWRITE_SHARED = "Calendars.ReadWrite.Shared"
PERM_CHAT_READ = "Chat.Read"
PERM_CHAT_READWRITE = "Chat.ReadWrite"
PERM_CHAT_READ = "Chat.Read"
PERM_GROUP_READ_ALL = "Group.Read.All"
PERM_GROUP_READWRITE_ALL = "Group.ReadWrite.All"
PERM_MAILBOX_SETTINGS = "MailboxSettings.ReadWrite"
Expand Down Expand Up @@ -198,8 +195,7 @@ class EventResponse(Enum):
PERM_SHARED = ".Shared"

SENSOR_AUTO_REPLY = "auto_reply"
SENSOR_DOMAIN = "sensor"
SENSOR_ENTITY_ID_FORMAT = SENSOR_DOMAIN + ".{}"
SENSOR_ENTITY_ID_FORMAT = "sensor.{}"
SENSOR_MAIL = "inbox"
SENSOR_TEAMS_STATUS = "teams_status"
SENSOR_TEAMS_CHAT = "teams_chat"
Expand Down

0 comments on commit 04a7c8b

Please sign in to comment.