2016-03-18 13:33:52 +01:00
|
|
|
"""The tests for the notify file platform."""
|
|
|
|
import os
|
2021-01-01 22:31:56 +01:00
|
|
|
from unittest.mock import call, mock_open, patch
|
2020-10-16 13:30:03 +02:00
|
|
|
|
|
|
|
import pytest
|
2016-03-18 13:33:52 +01:00
|
|
|
|
2022-02-12 18:49:37 +01:00
|
|
|
from homeassistant.components import notify
|
2019-07-31 12:25:30 -07:00
|
|
|
from homeassistant.components.notify import ATTR_TITLE_DEFAULT
|
2022-02-12 18:49:37 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-10-16 13:30:03 +02:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-03-18 13:33:52 +01:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
from tests.common import assert_setup_component
|
2016-03-18 13:33:52 +01:00
|
|
|
|
|
|
|
|
2023-02-13 09:45:11 +01:00
|
|
|
async def test_bad_config(hass: HomeAssistant) -> None:
|
2020-10-16 13:30:03 +02:00
|
|
|
"""Test set up the platform with bad/missing config."""
|
|
|
|
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
|
|
|
|
with assert_setup_component(0) as handle_config:
|
|
|
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
|
|
|
assert not handle_config[notify.DOMAIN]
|
2016-04-16 00:55:35 -07:00
|
|
|
|
2018-05-07 05:25:48 -04:00
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"timestamp",
|
|
|
|
[
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
)
|
2023-02-13 09:45:11 +01:00
|
|
|
async def test_notify_file(hass: HomeAssistant, timestamp: bool) -> None:
|
2020-10-16 13:30:03 +02:00
|
|
|
"""Test the notify file output."""
|
|
|
|
filename = "mock_file"
|
|
|
|
message = "one, two, testing, testing"
|
|
|
|
with assert_setup_component(1) as handle_config:
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
notify.DOMAIN,
|
|
|
|
{
|
|
|
|
"notify": {
|
|
|
|
"name": "test",
|
|
|
|
"platform": "file",
|
|
|
|
"filename": filename,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert handle_config[notify.DOMAIN]
|
2016-03-18 13:33:52 +01:00
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
m_open = mock_open()
|
|
|
|
with patch("homeassistant.components.file.notify.open", m_open, create=True), patch(
|
|
|
|
"homeassistant.components.file.notify.os.stat"
|
|
|
|
) as mock_st, patch("homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()):
|
|
|
|
mock_st.return_value.st_size = 0
|
|
|
|
title = (
|
|
|
|
f"{ATTR_TITLE_DEFAULT} notifications "
|
|
|
|
f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
|
|
|
|
)
|
2016-10-17 23:16:36 -04:00
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
await hass.services.async_call(
|
|
|
|
"notify", "test", {"message": message}, blocking=True
|
|
|
|
)
|
2017-01-15 03:53:14 +01:00
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
full_filename = os.path.join(hass.config.path(), filename)
|
|
|
|
assert m_open.call_count == 1
|
2021-07-28 09:41:45 +02:00
|
|
|
assert m_open.call_args == call(full_filename, "a", encoding="utf8")
|
2017-01-15 03:53:14 +01:00
|
|
|
|
2020-10-16 13:30:03 +02:00
|
|
|
assert m_open.return_value.write.call_count == 2
|
|
|
|
if not timestamp:
|
|
|
|
assert m_open.return_value.write.call_args_list == [
|
|
|
|
call(title),
|
|
|
|
call(f"{message}\n"),
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
assert m_open.return_value.write.call_args_list == [
|
|
|
|
call(title),
|
|
|
|
call(f"{dt_util.utcnow().isoformat()} {message}\n"),
|
|
|
|
]
|