2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the command line notification platform."""
|
2016-02-19 21:30:38 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2016-09-02 14:09:09 +00:00
|
|
|
from unittest.mock import patch
|
2016-02-19 21:30:38 +00:00
|
|
|
|
|
|
|
import homeassistant.components.notify as notify
|
2019-12-08 16:55:57 +00:00
|
|
|
from homeassistant.setup import setup_component
|
|
|
|
|
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
|
|
|
|
2016-02-19 21:30:38 +00:00
|
|
|
|
|
|
|
class TestCommandLine(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the command line notifications."""
|
2016-02-19 21:30:38 +00:00
|
|
|
|
|
|
|
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()
|
2016-02-19 21:30:38 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop down everything that was started."""
|
2016-02-19 21:30:38 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-09-02 14:09:09 +00:00
|
|
|
def test_setup(self):
|
|
|
|
"""Test setup."""
|
2017-01-15 02:53:14 +00:00
|
|
|
with assert_setup_component(1) as handle_config:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"notify",
|
|
|
|
{
|
|
|
|
"notify": {
|
|
|
|
"name": "test",
|
|
|
|
"platform": "command_line",
|
|
|
|
"command": "echo $(cat); exit 1",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
assert handle_config[notify.DOMAIN]
|
2016-09-02 14:09:09 +00:00
|
|
|
|
|
|
|
def test_bad_config(self):
|
|
|
|
"""Test set up the platform with bad/missing configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {notify.DOMAIN: {"name": "test", "platform": "command_line"}}
|
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
|
|
|
|
2016-02-19 21:30:38 +00:00
|
|
|
def test_command_line_output(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the command line output."""
|
2016-02-19 21:30:38 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdirname:
|
2019-07-31 19:25:30 +00:00
|
|
|
filename = os.path.join(tempdirname, "message.txt")
|
|
|
|
message = "one, two, testing, testing"
|
2017-01-15 02:53:14 +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": "command_line",
|
|
|
|
"command": "echo $(cat) > {}".format(filename),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
assert handle_config[notify.DOMAIN]
|
2016-02-19 21:30:38 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.hass.services.call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"notify", "test", {"message": message}, blocking=True
|
|
|
|
)
|
2016-02-19 21:30:38 +00:00
|
|
|
|
2017-05-26 20:12:17 +00:00
|
|
|
with open(filename) as fil:
|
|
|
|
# the echo command adds a line break
|
2018-10-24 10:10:05 +00:00
|
|
|
assert fil.read() == "{}\n".format(message)
|
2016-02-19 21:30:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.command_line.notify._LOGGER.error")
|
2016-02-19 21:30:38 +00:00
|
|
|
def test_error_for_none_zero_exit_code(self, mock_error):
|
2016-03-18 12:33:52 +00:00
|
|
|
"""Test if an error is logged for non zero exit codes."""
|
2017-01-15 02:53:14 +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": "command_line",
|
|
|
|
"command": "echo $(cat); exit 1",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2017-01-15 02:53:14 +00:00
|
|
|
assert handle_config[notify.DOMAIN]
|
2016-02-19 21:30:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert self.hass.services.call(
|
|
|
|
"notify", "test", {"message": "error"}, blocking=True
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == mock_error.call_count
|