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:
1 parent
3b9fb6ccf5
commit
9db1aa7629
51 files changed
+395
-255
No files matched your search
@@ -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'},
|
||||
|
||||
Reference in new issue
Block a user