55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""Coordinator for the Notification Store integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
import logging
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from .const import DOMAIN, NAME
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
from .store import NotificationStore
|
|
|
|
|
|
class NotificationStoreCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|
"""Coordinate notification store updates."""
|
|
|
|
def __init__(
|
|
self,
|
|
hass: HomeAssistant,
|
|
store: NotificationStore,
|
|
update_interval: timedelta,
|
|
) -> None:
|
|
"""Initialize the coordinator."""
|
|
super().__init__(
|
|
hass,
|
|
logger=_LOGGER,
|
|
name=NAME,
|
|
update_interval=update_interval,
|
|
always_update=False,
|
|
)
|
|
self.store = store
|
|
|
|
async def _async_update_data(self) -> dict[str, Any]:
|
|
"""Fetch data from the notification store."""
|
|
return await self.store.async_load()
|
|
|
|
@property
|
|
def notifications(self) -> list[dict[str, Any]]:
|
|
"""Return all notifications."""
|
|
return list((self.data or {}).get("notifications", []))
|
|
|
|
@property
|
|
def unread_notifications(self) -> list[dict[str, Any]]:
|
|
"""Return unread notifications."""
|
|
return [
|
|
item
|
|
for item in self.notifications
|
|
if item.get("status", "unread") == "unread"
|
|
]
|