From b593646ca2c073929cdc7025fd59391df149ea4c Mon Sep 17 00:00:00 2001 From: RogerSelwyn Date: Fri, 20 May 2022 16:29:52 +0100 Subject: [PATCH] Add filtering on body https://github.com/RogerSelwyn/O365-HomeAssistant/issues/55 --- custom_components/o365/const.py | 1 + custom_components/o365/schema.py | 2 ++ custom_components/o365/sensor.py | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/custom_components/o365/const.py b/custom_components/o365/const.py index ac3b4b4..34fbf78 100644 --- a/custom_components/o365/const.py +++ b/custom_components/o365/const.py @@ -51,6 +51,7 @@ class EventResponse(Enum): CONF_ALIASES = "aliases" CONF_ALT_AUTH_FLOW = "alt_auth_flow" CONF_ALT_AUTH_METHOD = "alt_auth_method" +CONF_BODY_CONTAINS = "body_contains" CONF_CACHE_PATH = "cache_path" CONF_CALENDAR_NAME = "calendar_name" CONF_CAL_IDS = "cal_ids" diff --git a/custom_components/o365/schema.py b/custom_components/o365/schema.py index 06df93d..1a19833 100644 --- a/custom_components/o365/schema.py +++ b/custom_components/o365/schema.py @@ -40,6 +40,7 @@ CONF_ACCOUNTS, CONF_ALT_AUTH_FLOW, CONF_ALT_AUTH_METHOD, + CONF_BODY_CONTAINS, CONF_CAL_ID, CONF_CHAT_SENSORS, CONF_CLIENT_ID, @@ -96,6 +97,7 @@ vol.Optional(CONF_HAS_ATTACHMENT): bool, vol.Optional(CONF_IMPORTANCE): cv.string, vol.Optional(CONF_IS_UNREAD): bool, + vol.Exclusive(CONF_BODY_CONTAINS, "body_*"): cv.string, vol.Exclusive(CONF_SUBJECT_CONTAINS, "subject_*"): cv.string, vol.Exclusive(CONF_SUBJECT_IS, "subject_*"): cv.string, vol.Optional(CONF_DOWNLOAD_ATTACHMENTS): bool, diff --git a/custom_components/o365/sensor.py b/custom_components/o365/sensor.py index 4e032c2..53899fa 100644 --- a/custom_components/o365/sensor.py +++ b/custom_components/o365/sensor.py @@ -16,6 +16,7 @@ ATTR_SUMMARY, CONF_ACCOUNT, CONF_ACCOUNT_NAME, + CONF_BODY_CONTAINS, CONF_CHAT_SENSORS, CONF_DOWNLOAD_ATTACHMENTS, CONF_EMAIL_SENSORS, @@ -173,6 +174,7 @@ def __init__(self, conf, mail_folder): """Initialise the O365 Query.""" super().__init__(conf, mail_folder) + self._body_contains = conf.get(CONF_BODY_CONTAINS) self._subject_contains = conf.get(CONF_SUBJECT_CONTAINS) self._subject_is = conf.get(CONF_SUBJECT_IS) self._has_attachment = conf.get(CONF_HAS_ATTACHMENT) @@ -183,7 +185,8 @@ def __init__(self, conf, mail_folder): self._query.order_by("receivedDateTime", ascending=False) if ( - self._subject_contains is not None + self._body_contains is not None + or self._subject_contains is not None or self._subject_is is not None or self._has_attachment is not None or self._importance is not None @@ -191,6 +194,7 @@ def __init__(self, conf, mail_folder): or self._is_unread is not None ): self._add_to_query("ge", "receivedDateTime", dt.datetime(1900, 5, 1)) + self._add_to_query("contains", "body", self._body_contains) self._add_to_query("contains", "subject", self._subject_contains) self._add_to_query("equals", "subject", self._subject_is) self._add_to_query("equals", "hasAttachments", self._has_attachment)