From ace798c7afb90a868ddac91f88e317a51cfb60cd Mon Sep 17 00:00:00 2001 From: RogerSelwyn Date: Thu, 1 Jun 2023 12:55:32 +0100 Subject: [PATCH] Revert "Code split up" This reverts commit 06f72a17693114bf6ce979428e857dafaefad670. --- custom_components/o365/calendar.py | 2 +- custom_components/o365/utils/event.py | 113 ------------------------- custom_components/o365/utils/utils.py | 114 +++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 116 deletions(-) delete mode 100644 custom_components/o365/utils/event.py diff --git a/custom_components/o365/calendar.py b/custom_components/o365/calendar.py index 2fffb17..003ac56 100644 --- a/custom_components/o365/calendar.py +++ b/custom_components/o365/calendar.py @@ -71,7 +71,6 @@ CALENDAR_SERVICE_REMOVE_SCHEMA, CALENDAR_SERVICE_RESPOND_SCHEMA, ) -from .utils.event import add_call_data_to_event from .utils.filemgmt import ( build_config_file_path, build_token_filename, @@ -82,6 +81,7 @@ ) from .utils.permissions import get_permissions, validate_minimum_permission from .utils.utils import ( + add_call_data_to_event, clean_html, format_event_data, get_end_date, diff --git a/custom_components/o365/utils/event.py b/custom_components/o365/utils/event.py deleted file mode 100644 index 982e777..0000000 --- a/custom_components/o365/utils/event.py +++ /dev/null @@ -1,113 +0,0 @@ -"""Utilities processes.""" -from datetime import datetime - -from O365.calendar import Attendee # pylint: disable=no-name-in-module) - -from ..const import ( - ATTR_ATTENDEES, - ATTR_BODY, - ATTR_CATEGORIES, - ATTR_IS_ALL_DAY, - ATTR_LOCATION, - ATTR_RRULE, - ATTR_SENSITIVITY, - ATTR_SHOW_AS, - DAYS, - INDEXES, -) - - -def add_call_data_to_event(event, subject, start, end, **kwargs): - """Add the call data.""" - event.subject = _add_attribute(subject, event.subject) - event.body = _add_attribute(kwargs.get(ATTR_BODY, None), event.body) - event.location = _add_attribute(kwargs.get(ATTR_LOCATION, None), event.location) - event.categories = _add_attribute(kwargs.get(ATTR_CATEGORIES, []), event.categories) - event.show_as = _add_attribute(kwargs.get(ATTR_SHOW_AS, None), event.show_as) - event.start = _add_attribute(start, event.start) - event.end = _add_attribute(end, event.end) - event.sensitivity = _add_attribute( - kwargs.get(ATTR_SENSITIVITY, None), event.sensitivity - ) - _add_attendees(kwargs.get(ATTR_ATTENDEES, []), event) - _add_all_day(kwargs.get(ATTR_IS_ALL_DAY, False), event) - - if kwargs.get(ATTR_RRULE, None): - _rrule_processing(event, kwargs[ATTR_RRULE]) - return event - - -def _add_attribute(attribute, event_attribute): - return attribute or event_attribute - - -def _add_attendees(attendees, event): - if attendees: - event.attendees.clear() - event.attendees.add( - [ - Attendee(x["email"], attendee_type=x["type"], event=event) - for x in attendees - ] - ) - - -def _add_all_day(is_all_day, event): - if is_all_day is not None: - event.is_all_day = is_all_day - if event.is_all_day: - event.start = datetime( - event.start.year, event.start.month, event.start.day, 0, 0, 0 - ) - event.end = datetime( - event.end.year, event.end.month, event.end.day, 0, 0, 0 - ) - - -def _rrule_processing(event, rrule): - rules = {} - for item in rrule.split(";"): - keys = item.split("=") - rules[keys[0]] = keys[1] - - kwargs = {} - if "COUNT" in rules: - kwargs["occurrences"] = int(rules["COUNT"]) - if "UNTIL" in rules: - end = datetime.strptime(rules["UNTIL"], "%Y%m%dT%H%M%S") - end.replace(tzinfo=event.start.tzinfo) - kwargs["end"] = end - interval = int(rules["INTERVAL"]) if "INTERVAL" in rules else 1 - if "BYDAY" in rules: - days, index = _process_byday(rules["BYDAY"]) - kwargs["days_of_week"] = days - if index: - kwargs["index"] = index - - if rules["FREQ"] == "YEARLY": - kwargs["day_of_month"] = event.start.day - event.recurrence.set_yearly(interval, event.start.month, **kwargs) - - if rules["FREQ"] == "MONTHLY": - if "BYDAY" not in rules: - kwargs["day_of_month"] = event.start.day - event.recurrence.set_monthly(interval, **kwargs) - - if rules["FREQ"] == "WEEKLY": - kwargs["first_day_of_week"] = "sunday" - event.recurrence.set_weekly(interval, **kwargs) - - if rules["FREQ"] == "DAILY": - event.recurrence.set_daily(interval, **kwargs) - - -def _process_byday(byday): - days = [] - for item in byday.split(","): - if len(item) > 2: - days.append(DAYS[item[2:4]]) - index = INDEXES[item[:2]] - else: - days.append(DAYS[item[:2]]) - index = None - return days, index diff --git a/custom_components/o365/utils/utils.py b/custom_components/o365/utils/utils.py index fe16b25..cf0f8ea 100644 --- a/custom_components/o365/utils/utils.py +++ b/custom_components/o365/utils/utils.py @@ -4,8 +4,22 @@ from bs4 import BeautifulSoup from homeassistant.helpers.entity import async_generate_entity_id - -from ..const import DATETIME_FORMAT, SENSOR_ENTITY_ID_FORMAT +from O365.calendar import Attendee # pylint: disable=no-name-in-module) + +from ..const import ( + ATTR_ATTENDEES, + ATTR_BODY, + ATTR_CATEGORIES, + ATTR_IS_ALL_DAY, + ATTR_LOCATION, + ATTR_RRULE, + ATTR_SENSITIVITY, + ATTR_SHOW_AS, + DATETIME_FORMAT, + DAYS, + INDEXES, + SENSOR_ENTITY_ID_FORMAT, +) _LOGGER = logging.getLogger(__name__) @@ -101,6 +115,102 @@ def get_start_date(obj): return obj.start +def add_call_data_to_event(event, subject, start, end, **kwargs): + """Add the call data.""" + event.subject = _add_attribute(subject, event.subject) + event.body = _add_attribute(kwargs.get(ATTR_BODY, None), event.body) + event.location = _add_attribute(kwargs.get(ATTR_LOCATION, None), event.location) + event.categories = _add_attribute(kwargs.get(ATTR_CATEGORIES, []), event.categories) + event.show_as = _add_attribute(kwargs.get(ATTR_SHOW_AS, None), event.show_as) + event.start = _add_attribute(start, event.start) + event.end = _add_attribute(end, event.end) + event.sensitivity = _add_attribute( + kwargs.get(ATTR_SENSITIVITY, None), event.sensitivity + ) + _add_attendees(kwargs.get(ATTR_ATTENDEES, []), event) + _add_all_day(kwargs.get(ATTR_IS_ALL_DAY, False), event) + + if kwargs.get(ATTR_RRULE, None): + _rrule_processing(event, kwargs[ATTR_RRULE]) + return event + + +def _add_attribute(attribute, event_attribute): + return attribute or event_attribute + + +def _add_attendees(attendees, event): + if attendees: + event.attendees.clear() + event.attendees.add( + [ + Attendee(x["email"], attendee_type=x["type"], event=event) + for x in attendees + ] + ) + + +def _add_all_day(is_all_day, event): + if is_all_day is not None: + event.is_all_day = is_all_day + if event.is_all_day: + event.start = datetime( + event.start.year, event.start.month, event.start.day, 0, 0, 0 + ) + event.end = datetime( + event.end.year, event.end.month, event.end.day, 0, 0, 0 + ) + + +def _rrule_processing(event, rrule): + rules = {} + for item in rrule.split(";"): + keys = item.split("=") + rules[keys[0]] = keys[1] + + kwargs = {} + if "COUNT" in rules: + kwargs["occurrences"] = int(rules["COUNT"]) + if "UNTIL" in rules: + end = datetime.strptime(rules["UNTIL"], "%Y%m%dT%H%M%S") + end.replace(tzinfo=event.start.tzinfo) + kwargs["end"] = end + interval = int(rules["INTERVAL"]) if "INTERVAL" in rules else 1 + if "BYDAY" in rules: + days, index = _process_byday(rules["BYDAY"]) + kwargs["days_of_week"] = days + if index: + kwargs["index"] = index + + if rules["FREQ"] == "YEARLY": + kwargs["day_of_month"] = event.start.day + event.recurrence.set_yearly(interval, event.start.month, **kwargs) + + if rules["FREQ"] == "MONTHLY": + if "BYDAY" not in rules: + kwargs["day_of_month"] = event.start.day + event.recurrence.set_monthly(interval, **kwargs) + + if rules["FREQ"] == "WEEKLY": + kwargs["first_day_of_week"] = "sunday" + event.recurrence.set_weekly(interval, **kwargs) + + if rules["FREQ"] == "DAILY": + event.recurrence.set_daily(interval, **kwargs) + + +def _process_byday(byday): + days = [] + for item in byday.split(","): + if len(item) > 2: + days.append(DAYS[item[2:4]]) + index = INDEXES[item[:2]] + else: + days.append(DAYS[item[:2]]) + index = None + return days, index + + def build_entity_id(hass, name): """Build and entity ID.""" return async_generate_entity_id(