Add discovery notify support and mysensors notify (#5219)

* Add mysensors notify platform

* Make add_devices optional in platform callback function.
* Use new argument structure for all existing mysensors platforms.
* Add notify platform.
* Update mysensors gateway.

* Refactor notify setup

* Enable discovery of notify platforms.
* Update and add tests for notify component and some platforms.
* Continue setup of notify platforms if a platform fails setup.
* Remove notify tests that check platform config. These tests are not
  needed when config validation is used.
* Add config validation to APNS notify platform.
* Use discovery to set up mysensors notify platform.

* Add discovery_info to get_service and update tests

* Add discovery_info as keyword argument to the get_service function
  signature and update all notify platforms.
* Update existing notify tests to check config validation using test
  helper.
* Add removed tests back in that checked config in apns, command_line
  and file platforms, but use config validation test helper to verify
  config.
* Add a test for notify file to increase coverage.
* Fix some PEP issues.

* Fix comments and use more constants

* Move apns notify service under notify domain
This commit is contained in:
Martin Hjelmare authored and GitHub committed 2017-01-15 03:53:14 +01:00
1 parent 3b9fb6ccf5
commit 9db1aa7629
51 files changed
+395 -255

No files matched your search

+65 -106
View File
@@ -1,14 +1,26 @@
"""The tests for the APNS component."""
import unittest
import os
import unittest
from unittest.mock import patch
from unittest.mock import Mock
from apns2.errors import Unregistered
import homeassistant.components.notify as notify
from homeassistant.core import State
from homeassistant.bootstrap import setup_component
from homeassistant.components.notify.apns import ApnsNotificationService
from tests.common import get_test_home_assistant
from homeassistant.config import load_yaml_config_file
from unittest.mock import patch
from apns2.errors import Unregistered
from homeassistant.core import State
from tests.common import assert_setup_component, get_test_home_assistant
CONFIG = {
notify.DOMAIN: {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
class TestApns(unittest.TestCase):
@@ -22,6 +34,13 @@ class TestApns(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@patch('os.path.isfile', Mock(return_value=True))
@patch('os.access', Mock(return_value=True))
def _setup_notify(self):
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, CONFIG)
assert handle_config[notify.DOMAIN]
def test_apns_setup_full(self):
"""Test setup with all data."""
config = {
@@ -41,53 +60,49 @@ class TestApns(unittest.TestCase):
config = {
'notify': {
'platform': 'apns',
'sandbox': 'True',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
'cert_file': 'test_app.pem',
}
}
self.assertFalse(notify.setup(self.hass, config))
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_apns_setup_missing_certificate(self):
"""Test setup with missing name."""
"""Test setup with missing certificate."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'name': 'test_app'
}
}
self.assertFalse(notify.setup(self.hass, config))
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_apns_setup_missing_topic(self):
"""Test setup with missing topic."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'cert_file': 'test_app.pem',
'name': 'test_app'
}
}
self.assertFalse(notify.setup(self.hass, config))
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_register_new_device(self):
"""Test registering a new device with a name."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('5678: {name: test device 2}\n')
notify.setup(self.hass, config)
self.assertTrue(self.hass.services.call('apns',
'test_app',
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'test device'},
blocking=True))
@@ -107,21 +122,12 @@ class TestApns(unittest.TestCase):
def test_register_device_without_name(self):
"""Test registering a without a name."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('5678: {name: test device 2}\n')
notify.setup(self.hass, config)
self.assertTrue(self.hass.services.call('apns', 'test_app',
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN, 'apns_test_app',
{'push_id': '1234'},
blocking=True))
@@ -137,23 +143,14 @@ class TestApns(unittest.TestCase):
def test_update_existing_device(self):
"""Test updating an existing device."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('1234: {name: test device 1}\n')
out.write('5678: {name: test device 2}\n')
notify.setup(self.hass, config)
self.assertTrue(self.hass.services.call('apns',
'test_app',
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'updated device 1'},
blocking=True))
@@ -173,15 +170,6 @@ class TestApns(unittest.TestCase):
def test_update_existing_device_with_tracking_id(self):
"""Test updating an existing device that has a tracking id."""
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('1234: {name: test device 1, '
@@ -189,9 +177,9 @@ class TestApns(unittest.TestCase):
out.write('5678: {name: test device 2, '
'tracking_device_id: tracking456}\n')
notify.setup(self.hass, config)
self.assertTrue(self.hass.services.call('apns',
'test_app',
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'updated device 1'},
blocking=True))
@@ -216,30 +204,20 @@ class TestApns(unittest.TestCase):
def test_send(self, mock_client):
"""Test updating an existing device."""
send = mock_client.return_value.send_notification
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('1234: {name: test device 1}\n')
notify.setup(self.hass, config)
self._setup_notify()
self.assertTrue(self.hass.services.call('notify', 'test_app',
{'message': 'Hello',
'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'
}
},
blocking=True))
self.assertTrue(self.hass.services.call(
'notify', 'test_app',
{'message': 'Hello', 'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'}},
blocking=True))
self.assertTrue(send.called)
self.assertEqual(1, len(send.mock_calls))
@@ -257,30 +235,20 @@ class TestApns(unittest.TestCase):
def test_send_when_disabled(self, mock_client):
"""Test updating an existing device."""
send = mock_client.return_value.send_notification
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('1234: {name: test device 1, disabled: True}\n')
notify.setup(self.hass, config)
self._setup_notify()
self.assertTrue(self.hass.services.call('notify', 'test_app',
{'message': 'Hello',
'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'
}
},
blocking=True))
self.assertTrue(self.hass.services.call(
'notify', 'test_app',
{'message': 'Hello', 'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'}},
blocking=True))
self.assertFalse(send.called)
@@ -328,20 +296,11 @@ class TestApns(unittest.TestCase):
send = mock_client.return_value.send_notification
send.side_effect = Unregistered()
config = {
'notify': {
'platform': 'apns',
'name': 'test_app',
'topic': 'testapp.appname',
'cert_file': 'test_app.pem'
}
}
devices_path = self.hass.config.path('test_app_apns.yaml')
with open(devices_path, 'w+') as out:
out.write('1234: {name: test device 1}\n')
notify.setup(self.hass, config)
self._setup_notify()
self.assertTrue(self.hass.services.call('notify', 'test_app',
{'message': 'Hello'},
+34 -25
View File
@@ -6,7 +6,7 @@ from unittest.mock import patch
from homeassistant.bootstrap import setup_component
import homeassistant.components.notify as notify
from tests.common import get_test_home_assistant
from tests.common import assert_setup_component, get_test_home_assistant
class TestCommandLine(unittest.TestCase):
@@ -22,34 +22,41 @@ class TestCommandLine(unittest.TestCase):
def test_setup(self):
"""Test setup."""
assert setup_component(self.hass, 'notify', {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1',
}})
with assert_setup_component(1) as handle_config:
assert setup_component(self.hass, 'notify', {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1', }
})
assert handle_config[notify.DOMAIN]
def test_bad_config(self):
"""Test set up the platform with bad/missing configuration."""
self.assertFalse(setup_component(self.hass, notify.DOMAIN, {
'notify': {
config = {
notify.DOMAIN: {
'name': 'test',
'platform': 'bad_platform',
'platform': 'command_line',
}
}))
}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
def test_command_line_output(self):
"""Test the command line output."""
with tempfile.TemporaryDirectory() as tempdirname:
filename = os.path.join(tempdirname, 'message.txt')
message = 'one, two, testing, testing'
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat) > {}'.format(filename)
}
}))
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat) > {}'.format(filename)
}
}))
assert handle_config[notify.DOMAIN]
self.assertTrue(
self.hass.services.call('notify', 'test', {'message': message},
@@ -63,13 +70,15 @@ class TestCommandLine(unittest.TestCase):
@patch('homeassistant.components.notify.command_line._LOGGER.error')
def test_error_for_none_zero_exit_code(self, mock_error):
"""Test if an error is logged for non zero exit codes."""
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1'
}
}))
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1'
}
}))
assert handle_config[notify.DOMAIN]
self.assertTrue(
self.hass.services.call('notify', 'test', {'message': 'error'},
+70 -9
View File
@@ -1,13 +1,19 @@
"""The tests for the notify demo platform."""
import unittest
from unittest.mock import patch
from homeassistant.core import callback
from homeassistant.bootstrap import setup_component
import homeassistant.components.notify as notify
from homeassistant.bootstrap import setup_component
from homeassistant.components.notify import demo
from homeassistant.helpers import script
from homeassistant.core import callback
from homeassistant.helpers import discovery, script
from tests.common import assert_setup_component, get_test_home_assistant
from tests.common import get_test_home_assistant
CONFIG = {
notify.DOMAIN: {
'platform': 'demo'
}
}
class TestNotifyDemo(unittest.TestCase):
@@ -16,11 +22,6 @@ class TestNotifyDemo(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'platform': 'demo'
}
}))
self.events = []
self.calls = []
@@ -35,6 +36,59 @@ class TestNotifyDemo(unittest.TestCase):
""""Stop down everything that was started."""
self.hass.stop()
def _setup_notify(self):
with assert_setup_component(1) as config:
assert setup_component(self.hass, notify.DOMAIN, CONFIG)
assert config[notify.DOMAIN]
def test_setup(self):
"""Test setup."""
self._setup_notify()
@patch('homeassistant.bootstrap.prepare_setup_platform')
def test_no_prepare_setup_platform(self, mock_prep_setup_platform):
"""Test missing notify platform."""
mock_prep_setup_platform.return_value = None
with self.assertLogs('homeassistant.components.notify',
level='ERROR') as log_handle:
self._setup_notify()
self.hass.block_till_done()
assert mock_prep_setup_platform.called
self.assertEqual(
log_handle.output,
['ERROR:homeassistant.components.notify:'
'Unknown notification service specified',
'ERROR:homeassistant.components.notify:'
'Failed to set up platform demo'])
@patch('homeassistant.components.notify.demo.get_service')
def test_no_notify_service(self, mock_demo_get_service):
"""Test missing platform notify service instance."""
mock_demo_get_service.return_value = None
with self.assertLogs('homeassistant.components.notify',
level='ERROR') as log_handle:
self._setup_notify()
self.hass.block_till_done()
assert mock_demo_get_service.called
self.assertEqual(
log_handle.output,
['ERROR:homeassistant.components.notify:'
'Failed to initialize notification service demo',
'ERROR:homeassistant.components.notify:'
'Failed to set up platform demo'])
@patch('homeassistant.components.notify.demo.get_service')
def test_discover_notify(self, mock_demo_get_service):
"""Test discovery of notify demo platform."""
assert notify.DOMAIN not in self.hass.config.components
discovery.load_platform(
self.hass, 'notify', 'demo', {'test_key': 'test_val'}, {})
self.hass.block_till_done()
assert notify.DOMAIN in self.hass.config.components
assert mock_demo_get_service.called
assert mock_demo_get_service.call_args[0] == (
self.hass, {}, {'test_key': 'test_val'})
@callback
def record_calls(self, *args):
"""Helper for recording calls."""
@@ -42,12 +96,14 @@ class TestNotifyDemo(unittest.TestCase):
def test_sending_none_message(self):
"""Test send with None as message."""
self._setup_notify()
notify.send_message(self.hass, None)
self.hass.block_till_done()
self.assertTrue(len(self.events) == 0)
def test_sending_templated_message(self):
"""Send a templated message."""
self._setup_notify()
self.hass.states.set('sensor.temperature', 10)
notify.send_message(self.hass, '{{ states.sensor.temperature.state }}',
'{{ states.sensor.temperature.name }}')
@@ -58,6 +114,7 @@ class TestNotifyDemo(unittest.TestCase):
def test_method_forwards_correct_data(self):
"""Test that all data from the service gets forwarded to service."""
self._setup_notify()
notify.send_message(self.hass, 'my message', 'my title',
{'hello': 'world'})
self.hass.block_till_done()
@@ -71,6 +128,7 @@ class TestNotifyDemo(unittest.TestCase):
def test_calling_notify_from_script_loaded_from_yaml_without_title(self):
"""Test if we can call a notify from a script."""
self._setup_notify()
conf = {
'service': 'notify.notify',
'data': {
@@ -97,6 +155,7 @@ class TestNotifyDemo(unittest.TestCase):
def test_calling_notify_from_script_loaded_from_yaml_with_title(self):
"""Test if we can call a notify from a script."""
self._setup_notify()
conf = {
'service': 'notify.notify',
'data': {
@@ -127,12 +186,14 @@ class TestNotifyDemo(unittest.TestCase):
def test_targets_are_services(self):
"""Test that all targets are exposed as individual services."""
self._setup_notify()
self.assertIsNotNone(self.hass.services.has_service("notify", "demo"))
service = "demo_test_target_name"
self.assertIsNotNone(self.hass.services.has_service("notify", service))
def test_messages_to_targets_route(self):
"""Test message routing to specific target services."""
self._setup_notify()
self.hass.bus.listen_once("notify", self.record_calls)
self.hass.services.call("notify", "demo_test_target_name",
+46 -25
View File
@@ -9,7 +9,7 @@ from homeassistant.components.notify import (
ATTR_TITLE_DEFAULT)
import homeassistant.util.dt as dt_util
from tests.common import get_test_home_assistant, assert_setup_component
from tests.common import assert_setup_component, get_test_home_assistant
class TestNotifyFile(unittest.TestCase):
@@ -25,36 +25,38 @@ class TestNotifyFile(unittest.TestCase):
def test_bad_config(self):
"""Test set up the platform with bad/missing config."""
with assert_setup_component(0):
assert not setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
},
})
config = {
notify.DOMAIN: {
'name': 'test',
'platform': 'file',
},
}
with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN]
@patch('homeassistant.components.notify.file.os.stat')
@patch('homeassistant.util.dt.utcnow')
def test_notify_file(self, mock_utcnow, mock_stat):
def _test_notify_file(self, timestamp, mock_utcnow, mock_stat):
"""Test the notify file output."""
mock_utcnow.return_value = dt_util.as_utc(dt_util.now())
mock_stat.return_value.st_size = 0
m_open = mock_open()
with patch(
'homeassistant.components.notify.file.open',
m_open, create=True
'homeassistant.components.notify.file.open',
m_open, create=True
):
filename = 'mock_file'
message = 'one, two, testing, testing'
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
'filename': filename,
'timestamp': False,
}
}))
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
'filename': filename,
'timestamp': timestamp,
}
}))
assert handle_config[notify.DOMAIN]
title = '{} notifications (Log started: {})\n{}\n'.format(
ATTR_TITLE_DEFAULT,
dt_util.utcnow().isoformat(),
@@ -68,7 +70,26 @@ class TestNotifyFile(unittest.TestCase):
self.assertEqual(m_open.call_args, call(full_filename, 'a'))
self.assertEqual(m_open.return_value.write.call_count, 2)
self.assertEqual(
m_open.return_value.write.call_args_list,
[call(title), call(message + '\n')]
)
if not timestamp:
self.assertEqual(
m_open.return_value.write.call_args_list,
[call(title), call('{}\n'.format(message))]
)
else:
self.assertEqual(
m_open.return_value.write.call_args_list,
[call(title), call('{} {}\n'.format(
dt_util.utcnow().isoformat(), message))]
)
@patch('homeassistant.components.notify.file.os.stat')
@patch('homeassistant.util.dt.utcnow')
def test_notify_file(self, mock_utcnow, mock_stat):
"""Test the notify file output without timestamp."""
self._test_notify_file(False, mock_utcnow, mock_stat)
@patch('homeassistant.components.notify.file.os.stat')
@patch('homeassistant.util.dt.utcnow')
def test_notify_file_timestamp(self, mock_utcnow, mock_stat):
"""Test the notify file output with timestamp."""
self._test_notify_file(True, mock_utcnow, mock_stat)
+1 -1
View File
@@ -19,7 +19,7 @@ class TestNotifyGroup(unittest.TestCase):
self.service1 = MagicMock()
self.service2 = MagicMock()
def mock_get_service(hass, config):
def mock_get_service(hass, config, discovery_info=None):
if config['name'] == 'demo1':
return self.service1
else: