Skip to content

Commit

Permalink
fix: Capture errors from fetching events so as not to provide empty d…
Browse files Browse the repository at this point in the history
…ata attributes
  • Loading branch information
RogerSelwyn committed Aug 2, 2024
1 parent e4fffd3 commit 34d24bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion custom_components/ms365_calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: MS365ConfigEntry):
)

if is_authenticated and permissions and permissions != TOKEN_FILE_MISSING:
_LOGGER.debug("do setup")
_LOGGER.debug("Do setup")
check_token = await _async_check_token(hass, account, entity_name)
if check_token:
coordinator, sensors, platforms = await async_do_setup(hass, entry, account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def __init__(
| CalendarEntityFeature.UPDATE_EVENT
)
self._max_results = entity.get(CONF_MAX_RESULTS)
self._error = None

def _init_data(self, account, calendar_id, entity):
search = entity.get(CONF_SEARCH)
Expand Down Expand Up @@ -265,20 +266,30 @@ async def async_get_events(self, hass, start_date, end_date):
async def async_update(self):
"""Do the update."""
# Get today's event for HA Core.
await self.data.async_update(self.hass, self._max_results)
event = deepcopy(self.data.event)
try:
await self.data.async_update(self.hass, self._max_results)
event = deepcopy(self.data.event)
except (HTTPError, RetryError, ConnectionError) as err:
self._log_error("Error getting calendar events for day", err)
return

if event:
event.summary, offset = extract_offset(event.summary, DEFAULT_OFFSET)
start = MS365CalendarData.to_datetime(event.start)
self._offset_reached = is_offset_reached(start, offset)

# Get events for extra attributes.
results = await self.data.async_ms365_get_events(
self.hass,
dt_util.utcnow() + timedelta(hours=self._start_offset),
dt_util.utcnow() + timedelta(hours=self._end_offset),
self._max_results,
)
try:
results = await self.data.async_ms365_get_events(
self.hass,
dt_util.utcnow() + timedelta(hours=self._start_offset),
dt_util.utcnow() + timedelta(hours=self._end_offset),
self._max_results,
)
except (HTTPError, RetryError, ConnectionError) as err:
self._log_error("Error getting calendar events for data", err)
return
self._error = False

if results is not None:
self._data_attribute = [format_event_data(x) for x in results]
Expand Down Expand Up @@ -383,6 +394,13 @@ async def async_modify_calendar_event(
event_id, EVENT_MODIFY_CALENDAR_EVENT, subject, start, end, **kwargs
)

def _log_error(self, error, err):
if not self._error:
_LOGGER.warning("%s - %s", error, err)
self._error = True
else:
_LOGGER.debug("Repeat error - %s - %s", error, err)

async def _async_update_calendar_event(
self, event_id, ha_event, subject, start, end, **kwargs
):
Expand Down Expand Up @@ -520,7 +538,7 @@ async def _async_get_calendar(self, hass):
)
return True
except (HTTPError, RetryError, ConnectionError) as err:
_LOGGER.warning("Error getting calendar events - %s", err)
_LOGGER.warning("Error getting calendar - %s", err)
return False

async def async_ms365_get_events(self, hass, start_date, end_date, limit=999):
Expand Down Expand Up @@ -593,18 +611,15 @@ async def _async_calendar_schedule_get_events(
# As at March 2023 not contains is not supported by Graph API
# if self._exclude is not None:
# query.chain("and").on_attribute("subject").negate().contains(self._exclude)
try:
return await hass.async_add_executor_job(
ft.partial(
calendar_schedule.get_events,
limit=limit,
query=query,
include_recurring=True,
)

return await hass.async_add_executor_job(
ft.partial(
calendar_schedule.get_events,
limit=limit,
query=query,
include_recurring=True,
)
except (HTTPError, RetryError, ConnectionError) as err:
_LOGGER.warning("Error getting calendar events - %s", err)
return None
)

async def async_get_events(self, hass, start_date, end_date):
"""Get the via async."""
Expand Down

0 comments on commit 34d24bc

Please sign in to comment.