2019-04-03 15:40:03 +00:00
|
|
|
"""IMAP sensor support."""
|
2022-01-03 18:11:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-06-15 22:44:58 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2023-03-20 14:52:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-03-27 06:33:46 +00:00
|
|
|
from homeassistant.const import CONF_USERNAME
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-01-09 10:41:47 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
2023-03-17 21:45:15 +00:00
|
|
|
from . import ImapPollingDataUpdateCoordinator, ImapPushDataUpdateCoordinator
|
2023-03-20 14:52:07 +00:00
|
|
|
from .const import DOMAIN
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-06-15 22:44:58 +00:00
|
|
|
IMAP_MAIL_COUNT_DESCRIPTION = SensorEntityDescription(
|
|
|
|
key="imap_mail_count",
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
suggested_display_precision=0,
|
|
|
|
)
|
|
|
|
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-01-09 10:41:47 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Imap sensor."""
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-03-17 21:45:15 +00:00
|
|
|
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator = (
|
|
|
|
hass.data[DOMAIN][entry.entry_id]
|
|
|
|
)
|
2023-06-15 22:44:58 +00:00
|
|
|
async_add_entities([ImapSensor(coordinator, IMAP_MAIL_COUNT_DESCRIPTION)])
|
2016-07-10 20:21:53 +00:00
|
|
|
|
|
|
|
|
2023-03-17 21:45:15 +00:00
|
|
|
class ImapSensor(
|
|
|
|
CoordinatorEntity[ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator],
|
|
|
|
SensorEntity,
|
|
|
|
):
|
2023-01-09 10:41:47 +00:00
|
|
|
"""Representation of an IMAP sensor."""
|
2017-09-26 07:26:26 +00:00
|
|
|
|
2023-01-09 10:41:47 +00:00
|
|
|
_attr_icon = "mdi:email-outline"
|
|
|
|
_attr_has_entity_name = True
|
2023-06-19 17:58:41 +00:00
|
|
|
_attr_name = None
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-03-17 21:45:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator,
|
2023-06-15 22:44:58 +00:00
|
|
|
description: SensorEntityDescription,
|
2023-03-17 21:45:15 +00:00
|
|
|
) -> None:
|
2023-01-09 10:41:47 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
2023-06-15 22:44:58 +00:00
|
|
|
self.entity_description = description
|
2023-01-09 10:41:47 +00:00
|
|
|
self._attr_unique_id = f"{coordinator.config_entry.entry_id}"
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
|
|
|
|
name=f"IMAP ({coordinator.config_entry.data[CONF_USERNAME]})",
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
)
|
2016-07-10 20:21:53 +00:00
|
|
|
|
|
|
|
@property
|
2023-03-17 21:45:15 +00:00
|
|
|
def native_value(self) -> int | None:
|
2023-01-09 10:41:47 +00:00
|
|
|
"""Return the number of emails found."""
|
|
|
|
return self.coordinator.data
|