Skip to content

Commit

Permalink
maint: Changes from testing of ms365_calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Oct 16, 2024
1 parent 28b523a commit a2de796
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 39 deletions.
87 changes: 68 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@

# Created by https://www.gitignore.io/api/python,visualstudiocode
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand All @@ -25,7 +20,6 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand All @@ -52,24 +46,46 @@ htmlcov/
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -78,24 +94,47 @@ target/
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# mkdocs documentation
/site

Expand All @@ -107,12 +146,22 @@ dmypy.json
# Pyre type checker
.pyre/

### VisualStudioCode ###
.vscode/*
# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

# End of https://www.gitignore.io/api/python,visualstudiocode
# Added
.DS_Store
.vscode/ltex.dictionary.en-GB.txt
.vscode/ltex.disabledRules.en-GB.txt
tests/whitelist.py
38 changes: 23 additions & 15 deletions custom_components/ms365_calendar/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ async def async_step_user(self, user_input=None):
self._alt_auth_method = user_input.get(CONF_ALT_AUTH_METHOD)
self._permissions = Permissions(self.hass, user_input)
self._permissions.token_filename = self._permissions.build_token_filename()
self._account, is_authenticated = await self.hass.async_add_executor_job(
(
self._account,
is_authenticated,
auth_error,
) = await self.hass.async_add_executor_job(
self._try_authentication,
self._permissions,
credentials,
main_resource,
)
if not is_authenticated or self._reconfigure:
if not auth_error and (not is_authenticated or self._reconfigure):
scope = self._permissions.requested_permissions
self._callback_url = get_callback_url(self.hass, self._alt_auth_method)
self._url, self._state = await self.hass.async_add_executor_job(
Expand All @@ -124,7 +128,10 @@ async def async_step_user(self, user_input=None):

return await self.async_step_request_default()

errors[CONF_ENTITY_NAME] = "already_configured"
if auth_error:
errors[CONF_ENTITY_NAME] = "error_authenticating"
else:
errors[CONF_ENTITY_NAME] = "already_configured"

data = self._config_schema or CONFIG_SCHEMA | CONFIG_SCHEMA_INTEGRATION
return self.async_show_form(
Expand All @@ -142,16 +149,13 @@ async def async_step_request_default(
if not errors:
return await self._async_create_update_entry()

failed_permissions = None
if self._failed_permissions:
failed_permissions = f"\n\n {', '.join(self._failed_permissions)}"
return self.async_show_form(
step_id="request_default",
data_schema=vol.Schema(REQUEST_AUTHORIZATION_DEFAULT_SCHEMA),
description_placeholders={
CONF_AUTH_URL: self._url,
CONF_ENTITY_NAME: self._entity_name,
CONF_FAILED_PERMISSIONS: failed_permissions,
CONF_FAILED_PERMISSIONS: self._failed_perms(),
},
errors=errors,
)
Expand All @@ -170,20 +174,23 @@ async def async_step_request_alt(
self._callback_view = MS365AuthCallbackView()
self.hass.http.register_view(self._callback_view)

failed_permissions = None
if self._failed_permissions:
failed_permissions = f"\n\nMissing - {', '.join(self._failed_permissions)}"

return self.async_show_form(
step_id="request_alt",
description_placeholders={
CONF_AUTH_URL: self._url,
CONF_ENTITY_NAME: self._entity_name,
CONF_FAILED_PERMISSIONS: failed_permissions,
CONF_FAILED_PERMISSIONS: self._failed_perms(),
},
errors=errors,
)

def _failed_perms(self):
return (
f"\n\nMissing - {', '.join(self._failed_permissions)}"
if self._failed_permissions
else None
)

async def _async_create_update_entry(self):
if self._reconfigure:
self.hass.config_entries.async_update_entry(
Expand Down Expand Up @@ -251,10 +258,11 @@ def _try_authentication(self, perms, credentials, main_resource):
main_resource=main_resource,
)
try:
return account, account.is_authenticated
return account, account.is_authenticated, False

except json.decoder.JSONDecodeError:
return account, False
except json.decoder.JSONDecodeError as err:
_LOGGER.error("Error authenticating - JSONDecodeError - %s", err)
return account, False, err

async def async_step_reconfigure(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
EVENT_HA_EVENT,
)
from ..helpers.config_entry import MS365ConfigEntry
from ..helpers.filemgmt import build_config_file_path
from ..helpers.utils import clean_html
from .const_integration import (
ATTR_ALL_DAY,
Expand Down Expand Up @@ -69,6 +68,7 @@
)
from .filemgmt_integration import (
async_update_calendar_file,
build_yaml_file_path,
build_yaml_filename,
load_yaml_file,
)
Expand Down Expand Up @@ -121,7 +121,7 @@ async def _async_setup_add_entities(
hass, account, async_add_entities, entry: MS365ConfigEntry, update_supported
):
yaml_filename = build_yaml_filename(entry, YAML_CALENDARS_FILENAME)
yaml_filepath = build_config_file_path(hass, yaml_filename)
yaml_filepath = build_yaml_file_path(hass, yaml_filename)
calendars = await hass.async_add_executor_job(
load_yaml_file, yaml_filepath, CONF_CAL_ID, YAML_CALENDAR_DEVICE_SCHEMA
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
CONF_SHARED_MAILBOX,
)
from ..helpers.config_entry import MS365ConfigEntry
from ..helpers.filemgmt import build_config_file_path
from ..helpers.utils import add_attribute_to_item
from .const_integration import (
CONF_BASIC_CALENDAR,
Expand All @@ -35,6 +34,7 @@
YAML_CALENDARS_FILENAME,
)
from .filemgmt_integration import (
build_yaml_file_path,
build_yaml_filename,
read_calendar_yaml_file,
write_calendar_yaml_file,
Expand Down Expand Up @@ -89,7 +89,7 @@ async def async_step_init(
) -> FlowResult:
"""Set up the option flow."""

self._yaml_filepath = build_config_file_path(self.hass, self._yaml_filename)
self._yaml_filepath = build_yaml_file_path(self.hass, self._yaml_filename)
self._calendars = await self.hass.async_add_executor_job(
read_calendar_yaml_file,
self._yaml_filepath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def async_update_calendar_file(
):
"""Update the calendar file."""
path = build_yaml_filename(entry, YAML_CALENDARS_FILENAME)
yaml_filepath = build_config_file_path(hass, path)
yaml_filepath = build_yaml_file_path(hass, path)
existing_calendars = await hass.async_add_executor_job(
load_yaml_file, yaml_filepath, CONF_CAL_ID, YAML_CALENDAR_DEVICE_SCHEMA
)
Expand All @@ -92,6 +92,11 @@ def build_yaml_filename(conf: MS365ConfigEntry, filename):
return filename.format(f"_{conf.data.get(CONF_ENTITY_NAME)}")


def build_yaml_file_path(hass, yaml_filename):
"""Create yaml path."""
return build_config_file_path(hass, yaml_filename)


def read_calendar_yaml_file(yaml_filepath):
"""Read the yaml file."""
with open(yaml_filepath, encoding="utf8") as file:
Expand Down
1 change: 1 addition & 0 deletions custom_components/ms365_calendar/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"error": {
"already_configured": "This account name has already been used. Use re-configure.",
"error_authenticating": "Error authenticating, JSONDecodeError, check log for errors",
"invalid_url": "Error, the originating url does not seem to be a valid microsoft redirect",
"permissions": "Minimum permissions not granted for account: {entity_name}",
"corrupted": "Token file is corrupted after successful authentication, check log for file system errors",
Expand Down

0 comments on commit a2de796

Please sign in to comment.