92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
"""Sensor platform for Notification Store."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_NAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers import entity_registry as er
|
|
from homeassistant.util import slugify
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import (
|
|
ATTR_COUNT,
|
|
ATTR_LATEST,
|
|
ATTR_LATEST_UNREAD,
|
|
ATTR_NOTIFICATIONS,
|
|
ATTR_TOTAL,
|
|
DEFAULT_NAME,
|
|
DEFAULT_SENSOR_NAME,
|
|
DOMAIN,
|
|
)
|
|
from .coordinator import NotificationStoreCoordinator
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the Notification Store sensor."""
|
|
coordinator: NotificationStoreCoordinator = hass.data[DOMAIN]["coordinators"][entry.entry_id]
|
|
|
|
device_name = entry.data.get(CONF_NAME, DEFAULT_NAME)
|
|
sensor_name = DEFAULT_SENSOR_NAME
|
|
unique_id = f"{entry.entry_id}_notifications"
|
|
|
|
registry = er.async_get(hass)
|
|
current_entity_id = registry.async_get_entity_id("sensor", DOMAIN, unique_id)
|
|
target_entity_id = f"sensor.{slugify(device_name)}_{slugify(sensor_name)}"
|
|
|
|
if (
|
|
current_entity_id
|
|
and current_entity_id != target_entity_id
|
|
and not registry.async_is_registered(target_entity_id)
|
|
):
|
|
registry.async_update_entity(current_entity_id, new_entity_id=target_entity_id)
|
|
|
|
async_add_entities([NotificationStoreSensor(coordinator, entry, unique_id, sensor_name)])
|
|
|
|
|
|
class NotificationStoreSensor(CoordinatorEntity[NotificationStoreCoordinator], SensorEntity):
|
|
"""Sensor exposing unread notification count."""
|
|
|
|
_attr_icon = "mdi:bell-ring-outline"
|
|
_attr_has_entity_name = True
|
|
|
|
def __init__(self, coordinator: NotificationStoreCoordinator, entry: ConfigEntry, unique_id: str, sensor_name: str) -> None:
|
|
"""Initialize the sensor."""
|
|
super().__init__(coordinator)
|
|
self.entry = entry
|
|
self._attr_unique_id = unique_id
|
|
self._attr_name = sensor_name
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
name=entry.title,
|
|
manufacturer="Torsten Brendgen",
|
|
model="Notification Store",
|
|
)
|
|
|
|
@property
|
|
def native_value(self) -> int:
|
|
"""Return unread notification count."""
|
|
return len(self.coordinator.unread_notifications)
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
"""Return notification attributes."""
|
|
notifications = self.coordinator.notifications
|
|
unread = self.coordinator.unread_notifications
|
|
return {
|
|
ATTR_COUNT: len(unread),
|
|
ATTR_TOTAL: len(notifications),
|
|
ATTR_NOTIFICATIONS: unread,
|
|
ATTR_LATEST: notifications[0] if notifications else None,
|
|
ATTR_LATEST_UNREAD: unread[0] if unread else None,
|
|
}
|