Skip to content

Commit

Permalink
Revert "Code split up"
Browse files Browse the repository at this point in the history
This reverts commit 06f72a1.
  • Loading branch information
RogerSelwyn committed Jun 1, 2023
1 parent 06f72a1 commit ace798c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 116 deletions.
2 changes: 1 addition & 1 deletion custom_components/o365/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
113 changes: 0 additions & 113 deletions custom_components/o365/utils/event.py

This file was deleted.

114 changes: 112 additions & 2 deletions custom_components/o365/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ace798c

Please sign in to comment.