2016-03-18 12:33:52 +00:00
|
|
|
"""The tests for the notify file platform."""
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import homeassistant.components.notify as notify
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.notify import ATTR_TITLE_DEFAULT
|
2019-12-09 10:45:11 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-03-18 12:33:52 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import call, mock_open, patch
|
2017-01-15 02:53:14 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant
|
2016-03-18 12:33:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestNotifyFile(unittest.TestCase):
|
|
|
|
"""Test the file notify."""
|
|
|
|
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-03-18 12:33:52 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2020-06-08 19:26:40 +00:00
|
|
|
self.addCleanup(self.tear_down_cleanup)
|
2016-03-18 12:33:52 +00:00
|
|
|
|
2020-06-08 19:26:40 +00:00
|
|
|
def tear_down_cleanup(self):
|
2018-05-13 09:06:15 +00:00
|
|
|
"""Stop down everything that was started."""
|
2016-03-18 12:33:52 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_bad_config(self):
|
|
|
|
"""Test set up the platform with bad/missing config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
|
2017-01-15 02:53:14 +00:00
|
|
|
with assert_setup_component(0) as handle_config:
|
|
|
|
assert setup_component(self.hass, notify.DOMAIN, config)
|
|
|
|
assert not handle_config[notify.DOMAIN]
|
2016-03-18 12:33:52 +00:00
|
|
|
|
2018-05-07 09:25:48 +00:00
|
|
|
def _test_notify_file(self, timestamp):
|
2016-03-18 12:33:52 +00:00
|
|
|
"""Test the notify file output."""
|
2019-07-31 19:25:30 +00:00
|
|
|
filename = "mock_file"
|
|
|
|
message = "one, two, testing, testing"
|
2018-05-07 09:25:48 +00:00
|
|
|
with assert_setup_component(1) as handle_config:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
notify.DOMAIN,
|
|
|
|
{
|
|
|
|
"notify": {
|
|
|
|
"name": "test",
|
|
|
|
"platform": "file",
|
|
|
|
"filename": filename,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-05-07 09:25:48 +00:00
|
|
|
assert handle_config[notify.DOMAIN]
|
2016-04-16 07:55:35 +00:00
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
m_open = mock_open()
|
|
|
|
with patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"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()
|
|
|
|
):
|
2018-05-07 09:25:48 +00:00
|
|
|
|
|
|
|
mock_st.return_value.st_size = 0
|
2020-02-25 01:54:20 +00:00
|
|
|
title = (
|
|
|
|
f"{ATTR_TITLE_DEFAULT} notifications "
|
|
|
|
f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-03-18 12:33:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(
|
|
|
|
"notify", "test", {"message": message}, blocking=True
|
|
|
|
)
|
2016-03-18 12:33:52 +00:00
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
full_filename = os.path.join(self.hass.config.path(), filename)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert m_open.call_count == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert m_open.call_args == call(full_filename, "a")
|
2016-10-18 03:16:36 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert m_open.return_value.write.call_count == 2
|
2017-01-15 02:53:14 +00:00
|
|
|
if not timestamp:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert m_open.return_value.write.call_args_list == [
|
|
|
|
call(title),
|
2020-02-25 01:54:20 +00:00
|
|
|
call(f"{message}\n"),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2017-01-15 02:53:14 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert m_open.return_value.write.call_args_list == [
|
|
|
|
call(title),
|
2020-02-25 01:54:20 +00:00
|
|
|
call(f"{dt_util.utcnow().isoformat()} {message}\n"),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2018-05-07 09:25:48 +00:00
|
|
|
def test_notify_file(self):
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Test the notify file output without timestamp."""
|
2018-05-07 09:25:48 +00:00
|
|
|
self._test_notify_file(False)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2018-05-07 09:25:48 +00:00
|
|
|
def test_notify_file_timestamp(self):
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Test the notify file output with timestamp."""
|
2018-05-07 09:25:48 +00:00
|
|
|
self._test_notify_file(True)
|