2019-04-03 15:40:03 +00:00
|
|
|
"""IMAP sensor support."""
|
2022-01-03 18:11:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-07-10 20:21:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2023-01-09 10:41:47 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
|
|
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-07-10 20:21:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2023-01-09 10:41:47 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
|
|
|
from homeassistant.helpers.entity import 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.issue_registry import IssueSeverity, async_create_issue
|
2022-01-03 18:11:50 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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-01-09 10:41:47 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CHARSET,
|
|
|
|
CONF_FOLDER,
|
|
|
|
CONF_SEARCH,
|
|
|
|
CONF_SERVER,
|
|
|
|
DEFAULT_PORT,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_SERVER): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
2019-10-27 12:07:44 +00:00
|
|
|
vol.Optional(CONF_CHARSET, default="utf-8"): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_FOLDER, default="INBOX"): cv.string,
|
|
|
|
vol.Optional(CONF_SEARCH, default="UnSeen UnDeleted"): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-07-10 20:21:53 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:11:50 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the IMAP platform."""
|
2023-01-09 10:41:47 +00:00
|
|
|
async_create_issue(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
"deprecated_yaml",
|
|
|
|
breaks_in_ha_version="2023.4.0",
|
|
|
|
is_fixable=False,
|
|
|
|
severity=IssueSeverity.WARNING,
|
|
|
|
translation_key="deprecated_yaml",
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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]
|
|
|
|
)
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-01-09 10:41:47 +00:00
|
|
|
async_add_entities([ImapSensor(coordinator)])
|
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
|
2016-07-10 20:21:53 +00:00
|
|
|
|
2023-03-17 21:45:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator,
|
|
|
|
) -> None:
|
2023-01-09 10:41:47 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
# To be removed when YAML import is removed
|
|
|
|
if CONF_NAME in coordinator.config_entry.data:
|
|
|
|
self._attr_name = coordinator.config_entry.data[CONF_NAME]
|
|
|
|
self._attr_has_entity_name = False
|
|
|
|
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
|