Add advanced imap option to set custom event max message size (#93163)
This commit is contained in:
parent
4b67839e19
commit
5bc825a8ab
6 changed files with 150 additions and 4 deletions
|
@ -24,11 +24,14 @@ from homeassistant.util.ssl import SSLCipherList
|
|||
from .const import (
|
||||
CONF_CHARSET,
|
||||
CONF_FOLDER,
|
||||
CONF_MAX_MESSAGE_SIZE,
|
||||
CONF_SEARCH,
|
||||
CONF_SERVER,
|
||||
CONF_SSL_CIPHER_LIST,
|
||||
DEFAULT_MAX_MESSAGE_SIZE,
|
||||
DEFAULT_PORT,
|
||||
DOMAIN,
|
||||
MAX_MESSAGE_SIZE_LIMIT,
|
||||
)
|
||||
from .coordinator import connect_to_server
|
||||
from .errors import InvalidAuth, InvalidFolder
|
||||
|
@ -55,7 +58,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
CONFIG_SCHEMA_ADVANCED = {
|
||||
vol.Optional(
|
||||
CONF_SSL_CIPHER_LIST, default=SSLCipherList.PYTHON_DEFAULT
|
||||
): CIPHER_SELECTOR
|
||||
): CIPHER_SELECTOR,
|
||||
}
|
||||
|
||||
OPTIONS_SCHEMA = vol.Schema(
|
||||
|
@ -65,6 +68,13 @@ OPTIONS_SCHEMA = vol.Schema(
|
|||
}
|
||||
)
|
||||
|
||||
OPTIONS_SCHEMA_ADVANCED = {
|
||||
vol.Optional(CONF_MAX_MESSAGE_SIZE, default=DEFAULT_MAX_MESSAGE_SIZE): vol.All(
|
||||
cv.positive_int,
|
||||
vol.Range(min=DEFAULT_MAX_MESSAGE_SIZE, max=MAX_MESSAGE_SIZE_LIMIT),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
async def validate_input(user_input: dict[str, Any]) -> dict[str, str]:
|
||||
"""Validate user input."""
|
||||
|
@ -233,6 +243,9 @@ class OptionsFlow(config_entries.OptionsFlowWithConfigEntry):
|
|||
)
|
||||
return self.async_create_entry(data={})
|
||||
|
||||
schema = self.add_suggested_values_to_schema(OPTIONS_SCHEMA, entry_data)
|
||||
schema = OPTIONS_SCHEMA
|
||||
if self.show_advanced_options:
|
||||
schema = schema.extend(OPTIONS_SCHEMA_ADVANCED)
|
||||
schema = self.add_suggested_values_to_schema(schema, entry_data)
|
||||
|
||||
return self.async_show_form(step_id="init", data_schema=schema, errors=errors)
|
||||
|
|
|
@ -8,6 +8,11 @@ CONF_SERVER: Final = "server"
|
|||
CONF_FOLDER: Final = "folder"
|
||||
CONF_SEARCH: Final = "search"
|
||||
CONF_CHARSET: Final = "charset"
|
||||
CONF_MAX_MESSAGE_SIZE = "max_message_size"
|
||||
CONF_SSL_CIPHER_LIST: Final = "ssl_cipher_list"
|
||||
|
||||
DEFAULT_PORT: Final = 993
|
||||
|
||||
DEFAULT_MAX_MESSAGE_SIZE = 2048
|
||||
|
||||
MAX_MESSAGE_SIZE_LIMIT = 30000
|
||||
|
|
|
@ -20,15 +20,18 @@ from homeassistant.const import (
|
|||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryError
|
||||
from homeassistant.helpers.json import json_bytes
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.util.ssl import SSLCipherList, client_context
|
||||
|
||||
from .const import (
|
||||
CONF_CHARSET,
|
||||
CONF_FOLDER,
|
||||
CONF_MAX_MESSAGE_SIZE,
|
||||
CONF_SEARCH,
|
||||
CONF_SERVER,
|
||||
CONF_SSL_CIPHER_LIST,
|
||||
DEFAULT_MAX_MESSAGE_SIZE,
|
||||
DOMAIN,
|
||||
)
|
||||
from .errors import InvalidAuth, InvalidFolder
|
||||
|
@ -38,6 +41,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||
BACKOFF_TIME = 10
|
||||
|
||||
EVENT_IMAP = "imap_content"
|
||||
MAX_EVENT_DATA_BYTES = 32168
|
||||
|
||||
|
||||
async def connect_to_server(data: Mapping[str, Any]) -> IMAP4_SSL:
|
||||
|
@ -177,11 +181,26 @@ class ImapDataUpdateCoordinator(DataUpdateCoordinator[int | None]):
|
|||
"search": self.config_entry.data[CONF_SEARCH],
|
||||
"folder": self.config_entry.data[CONF_FOLDER],
|
||||
"date": message.date,
|
||||
"text": message.text[:2048],
|
||||
"text": message.text[
|
||||
: self.config_entry.data.get(
|
||||
CONF_MAX_MESSAGE_SIZE, DEFAULT_MAX_MESSAGE_SIZE
|
||||
)
|
||||
],
|
||||
"sender": message.sender,
|
||||
"subject": message.subject,
|
||||
"headers": message.headers,
|
||||
}
|
||||
if (size := len(json_bytes(data))) > MAX_EVENT_DATA_BYTES:
|
||||
_LOGGER.warning(
|
||||
"Custom imap_content event skipped, size (%s) exceeds "
|
||||
"the maximal event size (%s), sender: %s, subject: %s",
|
||||
size,
|
||||
MAX_EVENT_DATA_BYTES,
|
||||
message.sender,
|
||||
message.subject,
|
||||
)
|
||||
return
|
||||
|
||||
self.hass.bus.fire(EVENT_IMAP, data)
|
||||
_LOGGER.debug(
|
||||
"Message processed, sender: %s, subject: %s",
|
||||
|
|
|
@ -39,7 +39,8 @@
|
|||
"init": {
|
||||
"data": {
|
||||
"folder": "[%key:component::imap::config::step::user::data::folder%]",
|
||||
"search": "[%key:component::imap::config::step::user::data::search%]"
|
||||
"search": "[%key:component::imap::config::step::user::data::search%]",
|
||||
"max_message_size": "Max message size (2048 < size < 30000)"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue