Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn3400 committed Sep 20, 2024
1 parent f9ab496 commit 990af68
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 83 deletions.
3 changes: 2 additions & 1 deletion custom_components/calendar_merge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up State updates from a config entry."""

calendar_handler: CalendarHandler = CalendarHandler(
hass, entry, entry.options.copy()
hass,
entry,
)

coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
Expand Down
27 changes: 3 additions & 24 deletions custom_components/calendar_merge/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

# from homeassistant.util import dt as dt_util
from .calendar_handler import CalendarHandler
from .const import DOMAIN

Expand Down Expand Up @@ -43,8 +42,7 @@ def __init__(

self.hass: HomeAssistant = hass
self.entry: ConfigEntry = entry
self._event: CalendarEvent | None = None
# self.tmp_calendar_event: list = []
# self._event: CalendarEvent | None = None

self.coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
"coordinator"
Expand Down Expand Up @@ -82,20 +80,9 @@ def event(self) -> CalendarEvent | None:
"""Return the next upcoming event."""

if len(self.calendar_handler.events) == 0:
self._event = None
return None

event = self.calendar_handler.events[0]

self._event = CalendarEvent(
start=event.start,
end=event.end,
summary=event.summary,
description=event.description,
location=event.location,
)

return self._event
return self.calendar_handler.events[0].as_calender_event()

# ------------------------------------------------------
async def async_get_events(
Expand All @@ -119,15 +106,7 @@ async def async_get_events(
or check_start <= start_date < check_end
or check_start < end_date <= check_end
):
events.append(
CalendarEvent(
start=tmp_event.start,
end=tmp_event.end,
summary=tmp_event.summary,
description=tmp_event.description,
location=tmp_event.location,
)
)
events.append(tmp_event.as_calender_event())
return events

# ------------------------------------------------------
Expand Down
67 changes: 36 additions & 31 deletions custom_components/calendar_merge/calendar_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,24 @@ class CalendarAttrEvent:

def __init__(
self,
calendar: str,
start: datetime | date | str,
end: datetime | date | str,
summary: str,
description: str | None = None,
location: str | None = None,
calender_merge_event: CalendarMergeEvent,
) -> None:
"""Init."""

self.calendar: str = calendar
self.start: datetime | date = start
self.end: datetime | date = end
self.summary: str = summary
self.description: str | None = description if description is not None else ""
self.location: str | None = location if location is not None else ""
self.calendar: str = calender_merge_event.calendar
self.start: datetime | date = calender_merge_event.start
self.end: datetime | date = calender_merge_event.end
self.summary: str = calender_merge_event.summary
self.description: str | None = (
calender_merge_event.description
if calender_merge_event.description is not None
else ""
)
self.location: str | None = (
calender_merge_event.location
if calender_merge_event.location is not None
else ""
)


# ------------------------------------------------------
Expand All @@ -67,6 +70,7 @@ def __init__(
class CalendarMergeEvent(CalendarEvent):
"""Calendar merge event."""

# ------------------------------------------------------
def __init__(
self,
calendar: str,
Expand Down Expand Up @@ -123,6 +127,14 @@ def __init__(
self.formatted_event: str = ""
super().__post_init__()

# ------------------------------------------------------
def as_calender_event(self) -> CalendarEvent:
"""As calendar event parms."""
return CalendarEvent(
self.start, self.end, self.summary, self.description, self.location
)

# ------------------------------------------------------
def __eq__(self, other: CalendarMergeEvent) -> bool:
"""Eq."""
return (
Expand All @@ -145,15 +157,13 @@ def __init__(
self,
hass: HomeAssistant,
entry: ConfigEntry,
entry_options: dict[str, Any],
) -> None:
"""Init."""

self.hass: HomeAssistant = hass
self.entry: ConfigEntry = entry
self.entry_options: dict[str, Any] = entry_options
self.events: list[CalendarMergeEvent] = []
self.language: str = self.entry_options.get(
self.language: str = self.entry.options.get(
CONF_FORMAT_LANGUAGE, self.hass.config.language
)

Expand All @@ -163,7 +173,7 @@ def __init__(
self.supress_update_listener: bool = False

# ------------------------------------------------------
async def get_merge_calendar_events(
async def merge_calendar_events(
self,
calendar_entities: list[str],
force_update: bool = False,
Expand All @@ -182,7 +192,7 @@ async def get_merge_calendar_events(
"end_date_time": (
dt_util.now()
+ timedelta(
days=self.entry_options.get(CONF_DAYS_AHEAD, 30)
days=self.entry.options.get(CONF_DAYS_AHEAD, 30)
)
).isoformat(),
"start_date_time": dt_util.now().isoformat(),
Expand Down Expand Up @@ -211,11 +221,11 @@ async def get_merge_calendar_events(
)
)

if self.entry_options.get(CONF_REMOVE_RECURRING_EVENTS, True):
if self.entry.options.get(CONF_REMOVE_RECURRING_EVENTS, True):
self.remove_recurring_events()

self.events.sort(key=lambda x: x.start_datetime_local.isoformat())
self.events = self.events[: int(self.entry_options.get(CONF_MAX_EVENTS, 5))]
self.events = self.events[: int(self.entry.options.get(CONF_MAX_EVENTS, 5))]
self.next_update = datetime.now() + timedelta(minutes=5)

# ------------------------------------------------------
Expand Down Expand Up @@ -293,7 +303,7 @@ async def async_format_event(self, event_num: int) -> str | None:
get_locale(self.language).timeframes.get("now", "now").capitalize()
)

elif self.entry_options.get(CONF_SHOW_EVENT_AS_TIME_TO, False):
elif self.entry.options.get(CONF_SHOW_EVENT_AS_TIME_TO, False):
formatted_event_str: str = await self.hass.async_add_executor_job(
partial(
format_timedelta,
Expand All @@ -306,7 +316,7 @@ async def async_format_event(self, event_num: int) -> str | None:
else:
formatted_event_str = tmp_event.formatted_start
if (
self.entry_options.get(CONF_SHOW_END_DATE, False)
self.entry.options.get(CONF_SHOW_END_DATE, False)
and not tmp_event.all_day
):
formatted_event_str = (
Expand All @@ -315,7 +325,7 @@ async def async_format_event(self, event_num: int) -> str | None:

tmp_event.formatted_event_time = formatted_event_str

if self.entry_options.get(CONF_SHOW_SUMMARY, False):
if self.entry.options.get(CONF_SHOW_SUMMARY, False):
formatted_event_str = tmp_event.summary + " : " + formatted_event_str

tmp_event.formatted_event = formatted_event_str
Expand All @@ -339,17 +349,17 @@ def replace_markdown_tags(txt: str) -> str:
tmp_md: str = ""
values: dict[str, Any] = {}

if self.entry_options.get(CONF_MD_HEADER_TEMPLATE, "") != "":
if self.entry.options.get(CONF_MD_HEADER_TEMPLATE, "") != "":
value_template: Template | None = Template(
str(self.entry_options.get(CONF_MD_HEADER_TEMPLATE, "")),
str(self.entry.options.get(CONF_MD_HEADER_TEMPLATE, "")),
self.hass,
)

tmp_md = value_template.async_render({})

for item in self.events:
value_template: Template | None = Template(
str(self.entry_options.get(CONF_MD_ITEM_TEMPLATE, "")),
str(self.entry.options.get(CONF_MD_ITEM_TEMPLATE, "")),
self.hass,
)
values = {
Expand Down Expand Up @@ -384,12 +394,7 @@ def get_events_to_att(
) -> list[CalendarAttrEvent]:
"""Create list of events to attribute."""

return [
CalendarAttrEvent(
x.calendar, x.start, x.end, x.summary, x.description, x.location
)
for x in self.events
]
return [CalendarAttrEvent(event) for event in self.events]

# ------------------------------------------------------------------
def create_issue_template(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/calendar_merge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"arrow"
],
"ssdp": [],
"version": "1.0.3",
"version": "1.0.4",
"zeroconf": []
}
Loading

0 comments on commit 990af68

Please sign in to comment.