Skip to content

Commit

Permalink
maint: Break out generic constants from specific
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed May 31, 2024
1 parent 1e98504 commit ce15cb7
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 106 deletions.
4 changes: 1 addition & 3 deletions custom_components/ms365_calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import logging

from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from O365 import Account, FileSystemTokenBackend
from oauthlib.oauth2.rfc6749.errors import InvalidClientError
Expand All @@ -18,12 +17,11 @@
CONST_UTC_TIMEZONE,
TOKEN_FILE_MISSING,
)
from .const_calendar import PLATFORMS
from .helpers.config_entry import MS365ConfigEntry, MS365Data

_LOGGER = logging.getLogger(__name__)

PLATFORMS: list[Platform] = [Platform.CALENDAR]


async def async_setup_entry(hass: HomeAssistant, entry: MS365ConfigEntry):
"""Set up a config entry."""
Expand Down
8 changes: 5 additions & 3 deletions custom_components/ms365_calendar/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
from requests.exceptions import HTTPError, RetryError

from .const import (
CONF_ACCOUNT_NAME,
DOMAIN,
EVENT_HA_EVENT,
)
from .const_calendar import (
ATTR_ALL_DAY,
ATTR_COLOR,
ATTR_DATA,
ATTR_EVENT_ID,
ATTR_HEX_COLOR,
ATTR_OFFSET,
CONF_ACCOUNT_NAME,
CONF_CAL_ID,
CONF_DEVICE_ID,
CONF_ENABLE_UPDATE,
Expand All @@ -49,9 +53,7 @@
CONF_TRACK_NEW_CALENDAR,
CONST_GROUP,
DEFAULT_OFFSET,
DOMAIN,
EVENT_CREATE_CALENDAR_EVENT,
EVENT_HA_EVENT,
EVENT_MODIFY_CALENDAR_EVENT,
EVENT_MODIFY_CALENDAR_RECURRENCES,
EVENT_REMOVE_CALENDAR_EVENT,
Expand Down
10 changes: 6 additions & 4 deletions custom_components/ms365_calendar/classes/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

from ..const import (
CONF_ACCOUNT_NAME,
CONF_SHARED_MAILBOX,
PERM_OFFLINE_ACCESS,
PERM_SHARED,
PERM_USER_READ,
)
from ..const_calendar import (
CONF_BASIC_CALENDAR,
CONF_ENABLE_UPDATE,
CONF_GROUPS,
CONF_SHARED_MAILBOX,
PERM_CALENDARS_READ,
PERM_CALENDARS_READBASIC,
PERM_CALENDARS_READWRITE,
PERM_GROUP_READ_ALL,
PERM_GROUP_READWRITE_ALL,
PERM_OFFLINE_ACCESS,
PERM_SHARED,
PERM_USER_READ,
)
from .base_permissions import BasePermissions

Expand Down
18 changes: 10 additions & 8 deletions custom_components/ms365_calendar/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@
CONF_ACCOUNT_NAME,
CONF_ALT_AUTH_METHOD,
CONF_AUTH_URL,
CONF_BASIC_CALENDAR,
CONF_CALENDAR_LIST,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_FAILED_PERMISSIONS,
CONF_SHARED_MAILBOX,
CONF_URL,
CONST_UTC_TIMEZONE,
DOMAIN,
TOKEN_FILE_MISSING,
)
from .const_calendar import (
CONF_BASIC_CALENDAR,
CONF_CALENDAR_LIST,
CONF_DEVICE_ID,
CONF_ENABLE_UPDATE,
CONF_ENTITIES,
CONF_FAILED_PERMISSIONS,
CONF_GROUPS,
CONF_HOURS_BACKWARD_TO_GET,
CONF_HOURS_FORWARD_TO_GET,
CONF_MAX_RESULTS,
CONF_SHARED_MAILBOX,
CONF_TRACK,
CONF_TRACK_NEW_CALENDAR,
CONF_URL,
CONST_UTC_TIMEZONE,
DOMAIN,
TOKEN_FILE_MISSING,
YAML_CALENDARS_FILENAME,
)
from .helpers.config_entry import MS365ConfigEntry
Expand Down
82 changes: 2 additions & 80 deletions custom_components/ms365_calendar/const.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,33 @@
"""Constants."""

from enum import Enum


class EventResponse(Enum):
"""Event response."""

Accept = "accept" # pylint: disable=invalid-name
Tentative = "tentative" # pylint: disable=invalid-name
Decline = "decline" # pylint: disable=invalid-name


ATTR_ALL_DAY = "all_day"
ATTR_ATTENDEES = "attendees"
ATTR_BODY = "body"
ATTR_CATEGORIES = "categories"
ATTR_COLOR = "color"
ATTR_DATA = "data"
ATTR_EMAIL = "email"
ATTR_END = "end"
ATTR_EVENT_ID = "event_id"
ATTR_HEX_COLOR = "hex_color"
ATTR_IS_ALL_DAY = "is_all_day"
ATTR_LOCATION = "location"
ATTR_MESSAGE = "message"
ATTR_OFFSET = "offset_reached"
ATTR_RESPONSE = "response"
ATTR_RRULE = "rrule"
ATTR_SEND_RESPONSE = "send_response"
ATTR_SENSITIVITY = "sensitivity"
ATTR_SHOW_AS = "show_as"
ATTR_START = "start"
ATTR_SUBJECT = "subject"
ATTR_TYPE = "type"
AUTH_CALLBACK_NAME = "api:ms365"
AUTH_CALLBACK_PATH_ALT = "/api/ms365"
AUTH_CALLBACK_PATH_DEFAULT = (
"https://login.microsoftonline.com/common/oauth2/nativeclient"
)
CALENDAR_ENTITY_ID_FORMAT = "calendar.{}"

CONF_ACCOUNT_NAME = "account_name"
CONF_ALT_AUTH_METHOD = "alt_auth_method"
CONF_AUTH_URL = "auth_url"
CONF_BASIC_CALENDAR = "basic_calendar"
CONF_CAL_ID = "cal_id"
CONF_CALENDAR_LIST = "calendar_list"
CONF_CLIENT_ID = "client_id"
CONF_CLIENT_SECRET = "client_secret" # nosec
CONF_DEVICE_ID = "device_id"
CONF_ENABLE_UPDATE = "enable_update"
CONF_ENTITIES = "entities"
CONF_EXCLUDE = "exclude"
CONF_FAILED_PERMISSIONS = "failed_permissions"
CONF_GROUPS = "groups"
CONF_HOURS_BACKWARD_TO_GET = "start_offset"
CONF_HOURS_FORWARD_TO_GET = "end_offset"
CONF_MAX_RESULTS = "max_results"
CONF_SEARCH = "search"
CONF_SHARED_MAILBOX = "shared_mailbox"
CONF_TRACK = "track"
CONF_TRACK_NEW_CALENDAR = "track_new_calendar"
CONF_URL = "url"

CONST_GROUP = "group:"
CONST_UTC_TIMEZONE = "UTC"

DEFAULT_OFFSET = "!!"
DOMAIN = "ms365_calendar"

EVENT_HA_EVENT = "ha_event"

EVENT_CREATE_CALENDAR_EVENT = "create_calendar_event"
EVENT_MODIFY_CALENDAR_EVENT = "modify_calendar_event"
EVENT_MODIFY_CALENDAR_RECURRENCES = "modify_calendar_recurrences"
EVENT_REMOVE_CALENDAR_EVENT = "remove_calendar_event"
EVENT_REMOVE_CALENDAR_RECURRENCES = "remove_calendar_recurrences"
EVENT_RESPOND_CALENDAR_EVENT = "respond_calendar_event"

MS365_STORAGE = "ms365_storage"
MS365_STORAGE_TOKEN = ".MS365-token-cache"
PERM_CALENDARS_READ = "Calendars.Read"
PERM_CALENDARS_READBASIC = "Calendars.ReadBasic"
PERM_CALENDARS_READWRITE = "Calendars.ReadWrite"
PERM_GROUP_READ_ALL = "Group.Read.All"
PERM_GROUP_READWRITE_ALL = "Group.ReadWrite.All"

PERM_OFFLINE_ACCESS = "offline_access"
PERM_USER_READ = "User.Read"
PERM_SHARED = ".Shared"


TOKEN_FILENAME = "{0}{1}.token" # nosec
TOKEN_FILE_MISSING = "missing"
YAML_CALENDARS_FILENAME = "ms365_calendars{0}.yaml"

DAYS = {
"MO": "monday",
"TU": "tuesday",
"WE": "wednesday",
"TH": "thursday",
"FR": "friday",
"SA": "saturday",
"SU": "sunday",
}
INDEXES = {
"+1": "first",
"+2": "second",
"+3": "third",
"+4": "fourth",
"-1": "last",
}
93 changes: 93 additions & 0 deletions custom_components/ms365_calendar/const_calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Calendar constants."""

from enum import Enum

from homeassistant.const import Platform


class EventResponse(Enum):
"""Event response."""

Accept = "accept" # pylint: disable=invalid-name
Tentative = "tentative" # pylint: disable=invalid-name
Decline = "decline" # pylint: disable=invalid-name


PLATFORMS: list[Platform] = [Platform.CALENDAR]

ATTR_ALL_DAY = "all_day"
ATTR_ATTENDEES = "attendees"
ATTR_BODY = "body"
ATTR_CATEGORIES = "categories"
ATTR_COLOR = "color"
ATTR_DATA = "data"
ATTR_EMAIL = "email"
ATTR_END = "end"
ATTR_EVENT_ID = "event_id"
ATTR_HEX_COLOR = "hex_color"
ATTR_IS_ALL_DAY = "is_all_day"
ATTR_LOCATION = "location"
ATTR_MESSAGE = "message"
ATTR_OFFSET = "offset_reached"
ATTR_RESPONSE = "response"
ATTR_RRULE = "rrule"
ATTR_SEND_RESPONSE = "send_response"
ATTR_SENSITIVITY = "sensitivity"
ATTR_SHOW_AS = "show_as"
ATTR_START = "start"
ATTR_SUBJECT = "subject"
ATTR_TYPE = "type"

CALENDAR_ENTITY_ID_FORMAT = "calendar.{}"

CONF_BASIC_CALENDAR = "basic_calendar"
CONF_CAL_ID = "cal_id"
CONF_CALENDAR_LIST = "calendar_list"
CONF_DEVICE_ID = "device_id"
CONF_ENABLE_UPDATE = "enable_update"
CONF_ENTITIES = "entities"
CONF_EXCLUDE = "exclude"
CONF_GROUPS = "groups"
CONF_HOURS_BACKWARD_TO_GET = "start_offset"
CONF_HOURS_FORWARD_TO_GET = "end_offset"
CONF_MAX_RESULTS = "max_results"
CONF_SEARCH = "search"
CONF_TRACK = "track"
CONF_TRACK_NEW_CALENDAR = "track_new_calendar"

CONST_GROUP = "group:"

DAYS = {
"MO": "monday",
"TU": "tuesday",
"WE": "wednesday",
"TH": "thursday",
"FR": "friday",
"SA": "saturday",
"SU": "sunday",
}

DEFAULT_OFFSET = "!!"

EVENT_CREATE_CALENDAR_EVENT = "create_calendar_event"
EVENT_MODIFY_CALENDAR_EVENT = "modify_calendar_event"
EVENT_MODIFY_CALENDAR_RECURRENCES = "modify_calendar_recurrences"
EVENT_REMOVE_CALENDAR_EVENT = "remove_calendar_event"
EVENT_REMOVE_CALENDAR_RECURRENCES = "remove_calendar_recurrences"
EVENT_RESPOND_CALENDAR_EVENT = "respond_calendar_event"

INDEXES = {
"+1": "first",
"+2": "second",
"+3": "third",
"+4": "fourth",
"-1": "last",
}

PERM_CALENDARS_READ = "Calendars.Read"
PERM_CALENDARS_READBASIC = "Calendars.ReadBasic"
PERM_CALENDARS_READWRITE = "Calendars.ReadWrite"
PERM_GROUP_READ_ALL = "Group.Read.All"
PERM_GROUP_READWRITE_ALL = "Group.ReadWrite.All"

YAML_CALENDARS_FILENAME = "ms365_calendars{0}.yaml"
2 changes: 1 addition & 1 deletion custom_components/ms365_calendar/helpers/calendar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from O365.calendar import Attendee # pylint: disable=no-name-in-module)

from ..const import (
from ..const_calendar import (
ATTR_ATTENDEES,
ATTR_BODY,
ATTR_CATEGORIES,
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ms365_calendar/helpers/filemgmt_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from ..const import (
CONF_ACCOUNT_NAME,
)
from ..const_calendar import (
CONF_CAL_ID,
CONF_DEVICE_ID,
CONF_ENTITIES,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ms365_calendar/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bs4 import BeautifulSoup
from homeassistant.util import slugify

from ..const import CALENDAR_ENTITY_ID_FORMAT
from ..const_calendar import CALENDAR_ENTITY_ID_FORMAT

_LOGGER = logging.getLogger(__name__)

Expand Down
14 changes: 8 additions & 6 deletions custom_components/ms365_calendar/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
)

from .const import (
CONF_ACCOUNT_NAME,
CONF_ALT_AUTH_METHOD,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_SHARED_MAILBOX,
CONF_URL,
)
from .const_calendar import (
ATTR_ATTENDEES,
ATTR_BODY,
ATTR_CATEGORIES,
Expand All @@ -32,12 +40,8 @@
ATTR_START,
ATTR_SUBJECT,
ATTR_TYPE,
CONF_ACCOUNT_NAME,
CONF_ALT_AUTH_METHOD,
CONF_BASIC_CALENDAR,
CONF_CAL_ID,
CONF_CLIENT_ID,
CONF_CLIENT_SECRET,
CONF_DEVICE_ID,
CONF_ENABLE_UPDATE,
CONF_ENTITIES,
Expand All @@ -47,9 +51,7 @@
CONF_HOURS_FORWARD_TO_GET,
CONF_MAX_RESULTS,
CONF_SEARCH,
CONF_SHARED_MAILBOX,
CONF_TRACK,
CONF_URL,
EventResponse,
)

Expand Down

0 comments on commit ce15cb7

Please sign in to comment.