Skip to content

Commit

Permalink
Tidy up file storage location
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Nov 10, 2022
1 parent c16495f commit 6dbf28a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions custom_components/o365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
build_minimum_permissions,
build_requested_permissions,
build_token_filename,
check_file_location,
validate_permissions,
)

Expand Down Expand Up @@ -79,6 +80,7 @@ async def _async_setup_account(hass, account_conf, conf_type):

token_path = build_config_file_path(hass, DEFAULT_CACHE_PATH)
token_file = build_token_filename(account_conf, conf_type)
check_file_location(hass, DEFAULT_CACHE_PATH, token_path)
token_backend = await hass.async_add_executor_job(
ft.partial(
FileSystemTokenBackend, token_path=token_path, token_filename=token_file
Expand Down
5 changes: 4 additions & 1 deletion custom_components/o365/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
build_config_file_path,
build_token_filename,
build_yaml_filename,
check_file_location,
clean_html,
format_event_data,
get_permissions,
Expand Down Expand Up @@ -84,7 +85,9 @@ async def async_setup_platform(

def _setup_add_entities(hass, account, add_entities, conf):
yaml_filename = build_yaml_filename(conf)
calendars = load_calendars(build_config_file_path(hass, yaml_filename))
yaml_filepath = build_config_file_path(hass, yaml_filename)
check_file_location(hass, yaml_filename, yaml_filepath)
calendars = load_calendars(yaml_filepath)
cal_ids = {}

for cal_id, calendar in calendars.items():
Expand Down
1 change: 1 addition & 0 deletions custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class EventResponse(Enum):
DOMAIN = "o365"
ICON = "mdi:office"
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
O365_STORAGE = "o365_storage"
DEFAULT_OFFSET = "!!"
PERM_CALENDARS_READ = "Calendars.Read"
PERM_CALENDARS_READ_SHARED = "Calendars.Read.Shared"
Expand Down
19 changes: 16 additions & 3 deletions custom_components/o365/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import shutil
import zipfile
from datetime import datetime
from pathlib import Path
Expand Down Expand Up @@ -33,6 +34,7 @@
DATETIME_FORMAT,
DEFAULT_CACHE_PATH,
DOMAIN,
O365_STORAGE,
PERM_CALENDARS_READ,
PERM_CALENDARS_READWRITE,
PERM_CHAT_READ,
Expand Down Expand Up @@ -331,11 +333,11 @@ def update_calendar_file(path, calendar, hass, track_new_devices):
out.close()


def build_config_file_path(hass, filename):
"""Create filename in config path."""
def build_config_file_path(hass, filepath):
"""Create config path."""
root = hass.config.config_dir

return os.path.join(root, filename)
return os.path.join(root, O365_STORAGE, filepath)


def build_token_filename(conf, conf_type):
Expand All @@ -357,3 +359,14 @@ def build_yaml_filename(conf, conf_type=None):
else ""
)
return YAML_CALENDARS.format(DOMAIN, config_file)


def check_file_location(hass, filepath, newpath):
"""Check if file has been moved. If not move it. This function to be removed 2023/05/30."""
root = hass.config.config_dir
oldpath = os.path.join(
root,
filepath,
)
if os.path.exists(oldpath):
shutil.move(oldpath, newpath)

0 comments on commit 6dbf28a

Please sign in to comment.