Skip to content

Commit

Permalink
Improve debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
watkins-matt committed Aug 17, 2024
1 parent 152ce22 commit 4d3114e
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion custom_components/google_keep_sync/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ def __init__(
)
self.api = api
self.config_entry = entry
_LOGGER.debug("GoogleKeepSyncCoordinator initialized")

# Define the update method for the coordinator
async def _async_update_data(self) -> list[GKeepList]:
"""Fetch data from API."""
try:
# save lists prior to syncing
_LOGGER.debug("Starting data update process")

# Save lists prior to syncing
original_lists = await self._parse_gkeep_data_dict()
_LOGGER.debug("Parsed original lists: %d lists found", len(original_lists))

# Sync data with Google Keep
lists_to_sync = self.config_entry.data.get("lists_to_sync", [])
Expand All @@ -55,25 +59,39 @@ async def _async_update_data(self) -> list[GKeepList]:
"list_item_case", ListCase.NO_CHANGE
)

_LOGGER.debug(
"Syncing data with Google Keep. Lists to sync: %d, Auto sort: %s, Change case: %s",
len(lists_to_sync),
auto_sort,
change_case,
)

result = await self.api.async_sync_data(
lists_to_sync, auto_sort, change_case
)
_LOGGER.debug("Data sync completed. Received %d lists", len(result))

# save lists after syncing
updated_lists = await self._parse_gkeep_data_dict()
_LOGGER.debug("Parsed updated lists: %d lists found", len(updated_lists))

# compare both list for changes, and fire event for changes
new_items = await self._get_new_items_added(
original_lists,
updated_lists,
)
_LOGGER.debug("Found %d new items", len(new_items))

await self._notify_new_items(new_items)

return result
except Exception as error:
_LOGGER.error("Error communicating with API: %s", error, exc_info=True)
raise UpdateFailed(f"Error communicating with API: {error}") from error

async def _parse_gkeep_data_dict(self) -> dict[str, TodoList]:
"""Parse unchecked gkeep data to a dictionary, with the list id as the key."""
_LOGGER.debug("Parsing Google Keep data")
all_keep_lists = {}

# for each list
Expand All @@ -85,6 +103,13 @@ async def _parse_gkeep_data_dict(self) -> dict[str, TodoList]:
if not item.checked
}
all_keep_lists[keep_list.id] = TodoList(name=keep_list.title, items=items)
_LOGGER.debug(
"Parsed list '%s': %d unchecked items", keep_list.title, len(items)
)

_LOGGER.debug(
"Finished parsing Google Keep data: %d lists", len(all_keep_lists)
)
return all_keep_lists

async def _get_new_items_added(
Expand All @@ -100,6 +125,7 @@ async def _get_new_items_added(
:param updated_lists: The updated todo list data.
# :param on_new_item: Callback function to execute for each new item found.
"""
_LOGGER.debug("Comparing original and updated lists to find new items")
new_items = []
# for each list
for updated_list_id, updated_list in updated_lists.items():
Expand Down Expand Up @@ -135,10 +161,13 @@ async def _get_new_items_added(
updated_list_item.summary,
list_entity_id,
)

_LOGGER.debug("Finished comparing lists. Found %d new items", len(new_items))
return new_items

async def _notify_new_items(self, new_items: list[TodoItemData]) -> None:
"""Emit add_item service call event for new remote Todo items."""
_LOGGER.debug("Notifying about %d new items", len(new_items))
for new_item in new_items:
event_data = {
"domain": "todo",
Expand All @@ -151,3 +180,10 @@ async def _notify_new_items(self, new_items: list[TodoItemData]) -> None:
self.hass.bus.async_fire(
EVENT_CALL_SERVICE, event_data, origin=EventOrigin.remote
)
_LOGGER.debug(
"Fired add_item event for item: '%s' in entity: '%s'",
new_item.item,
new_item.entity_id,
)

_LOGGER.debug("Finished notifying about new items")

0 comments on commit 4d3114e

Please sign in to comment.