Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Dec 8, 2024
1 parent 099770e commit 012f3db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion custom_components/sector/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ async def _process_event_logs(self, logs, devices):
_LOGGER.debug("Grouped events by lock: %s", grouped_events)
return grouped_events

async def process_events(self):
@property
def process_events(self) -> dict:
"""Return processed event logs grouped by device."""
return self._event_logs
19 changes: 10 additions & 9 deletions custom_components/sector/event.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Event platform for Sector Alarm integration."""

import logging
from datetime import datetime, timezone
from datetime import datetime
from typing import Any

from homeassistant.components.event import EventEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util

from .coordinator import SectorAlarmConfigEntry, SectorDataUpdateCoordinator
from .entity import SectorAlarmBaseEntity
Expand Down Expand Up @@ -89,9 +90,10 @@ def event_types(self):
"""Return the list of event types this entity can handle."""
return ["lock", "unlock", "lock_failed"]

async def async_update(self):
@callback
def _async_handle_event(self):
"""Update entity based on the most recent event."""
grouped_events = await self.coordinator.process_events()
grouped_events = self.coordinator.process_events
_LOGGER.debug("SECTOR_EVENT: Processing events for device %s", self._serial_no)

if self._serial_no not in grouped_events:
Expand Down Expand Up @@ -120,6 +122,7 @@ async def async_update(self):
self._last_formatted_event = latest_log.get(
"formatted_event", f"{event_type.capitalize()} occurred"
)
event_time = event_time.replace(tzinfo=dt_util.UTC)
self._trigger_event(
self._last_event_type, {"timestamp": event_time.isoformat()}
)
Expand All @@ -135,7 +138,7 @@ async def async_update(self):
def _trigger_event(self, event_type, event_attributes):
"""Trigger an event update with the latest type."""
event_timestamp = event_attributes.get(
"timestamp", datetime.now(timezone.utc).isoformat()
"timestamp", datetime.now(dt_util.UTC).isoformat()
)
event_attributes["timestamp"] = event_timestamp
_LOGGER.debug(
Expand Down Expand Up @@ -195,7 +198,7 @@ def _format_timestamp(time_str: str) -> str:
try:
timestamp = datetime.fromisoformat(
time_str.replace("Z", "+00:00")
).astimezone(timezone.utc)
).astimezone(dt_util.UTC)
_LOGGER.debug(
"SECTOR_EVENT: Formatted timestamp: %s", timestamp.isoformat()
)
Expand All @@ -208,11 +211,9 @@ async def async_added_to_hass(self):
"""Set up continuous event processing once added to Home Assistant."""
await super().async_added_to_hass()

# Set up listener to call async_update whenever the coordinator updates
# Set up listener to call _async_handle_event whenever the coordinator updates
self.async_on_remove(
self.coordinator.async_add_listener(
lambda: self.hass.async_create_task(self.async_update())
)
self.coordinator.async_add_listener(self._async_handle_event)
)

_LOGGER.debug(
Expand Down

0 comments on commit 012f3db

Please sign in to comment.