Add imap custom event data template (#93423)

* Add imap custom event template

* Add template validation
This commit is contained in:
Jan Bouwhuis 2023-05-25 11:05:25 +02:00 committed by GitHub
parent 6cd766ef1f
commit 1b5d207984
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 155 additions and 23 deletions

View file

@ -505,3 +505,57 @@ async def test_message_is_truncated(
event_data = event_called[0].data
assert len(event_data["text"]) == 3
@pytest.mark.parametrize(
("imap_search", "imap_fetch"),
[(TEST_SEARCH_RESPONSE, TEST_FETCH_RESPONSE_TEXT_PLAIN)],
ids=["plain"],
)
@pytest.mark.parametrize("imap_has_capability", [True, False], ids=["push", "poll"])
@pytest.mark.parametrize(
("custom_template", "result", "error"),
[
("{{ subject }}", "Test subject", None),
('{{ "@example.com" in sender }}', True, None),
("{% bad template }}", None, "Error rendering imap custom template"),
],
ids=["subject_test", "sender_filter", "template_error"],
)
async def test_custom_template(
hass: HomeAssistant,
mock_imap_protocol: MagicMock,
caplog: pytest.LogCaptureFixture,
custom_template: str,
result: str | bool | None,
error: str | None,
) -> None:
"""Test the custom template event data."""
event_called = async_capture_events(hass, "imap_content")
config = MOCK_CONFIG.copy()
config["custom_event_data_template"] = custom_template
config_entry = MockConfigEntry(domain=DOMAIN, data=config)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
# we should have received one message
assert state is not None
assert state.state == "1"
# we should have received one event
assert len(event_called) == 1
data: dict[str, Any] = event_called[0].data
assert data["server"] == "imap.server.com"
assert data["username"] == "email@email.com"
assert data["search"] == "UnSeen UnDeleted"
assert data["folder"] == "INBOX"
assert data["sender"] == "john.doe@example.com"
assert data["subject"] == "Test subject"
assert data["text"]
assert data["custom"] == result
assert error in caplog.text if error is not None else True