Asyncio notify component migration (#5377)

* Async migrate notify/platform

* convert group to async

* fix unittest
This commit is contained in:
Pascal Vizeli authored and Paulus Schoutsen committed 2017-01-17 22:08:03 -08:00
1 parent f7ac644c11
commit 2a362fd1ff
6 files changed
+136 -88

No files matched your search

+6 -2
View File
@@ -41,7 +41,9 @@ class TestApns(unittest.TestCase):
assert setup_component(self.hass, notify.DOMAIN, CONFIG)
assert handle_config[notify.DOMAIN]
def test_apns_setup_full(self):
@patch('os.path.isfile', return_value=True)
@patch('os.access', return_value=True)
def test_apns_setup_full(self, mock_access, mock_isfile):
"""Test setup with all data."""
config = {
'notify': {
@@ -53,7 +55,9 @@ class TestApns(unittest.TestCase):
}
}
self.assertTrue(notify.setup(self.hass, config))
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_missing_name(self):
"""Test setup with missing name."""
+16 -18
View File
@@ -1,4 +1,5 @@
"""The tests for the notify demo platform."""
import asyncio
import unittest
from unittest.mock import patch
@@ -16,6 +17,12 @@ CONFIG = {
}
@asyncio.coroutine
def mock_setup_platform():
"""Mock prepare_setup_platform."""
return None
class TestNotifyDemo(unittest.TestCase):
"""Test the demo notify."""
@@ -45,23 +52,16 @@ class TestNotifyDemo(unittest.TestCase):
"""Test setup."""
self._setup_notify()
@patch('homeassistant.bootstrap.prepare_setup_platform')
@patch('homeassistant.bootstrap.async_prepare_setup_platform',
return_value=mock_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'])
with assert_setup_component(0):
setup_component(self.hass, notify.DOMAIN, CONFIG)
@patch('homeassistant.components.notify.demo.get_service')
assert mock_prep_setup_platform.called
@patch('homeassistant.components.notify.demo.get_service', autospec=True)
def test_no_notify_service(self, mock_demo_get_service):
"""Test missing platform notify service instance."""
mock_demo_get_service.return_value = None
@@ -73,11 +73,9 @@ class TestNotifyDemo(unittest.TestCase):
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'])
'Failed to initialize notification service demo'])
@patch('homeassistant.components.notify.demo.get_service')
@patch('homeassistant.components.notify.demo.get_service', autospec=True)
def test_discover_notify(self, mock_demo_get_service):
"""Test discovery of notify demo platform."""
assert notify.DOMAIN not in self.hass.config.components
+20 -11
View File
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
from homeassistant.bootstrap import setup_component
import homeassistant.components.notify as notify
from homeassistant.components.notify import group, demo
from homeassistant.util.async import run_coroutine_threadsafe
from tests.common import assert_setup_component, get_test_home_assistant
@@ -16,8 +17,11 @@ class TestNotifyGroup(unittest.TestCase):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.events = []
self.service1 = MagicMock()
self.service2 = MagicMock()
self.service1 = demo.DemoNotificationService(self.hass)
self.service2 = demo.DemoNotificationService(self.hass)
self.service1.send_message = MagicMock(autospec=True)
self.service2.send_message = MagicMock(autospec=True)
def mock_get_service(hass, config, discovery_info=None):
if config['name'] == 'demo1':
@@ -37,11 +41,14 @@ class TestNotifyGroup(unittest.TestCase):
}]
})
self.service = group.get_service(self.hass, {'services': [
{'service': 'demo1'},
{'service': 'demo2',
'data': {'target': 'unnamed device',
'data': {'test': 'message'}}}]})
self.service = run_coroutine_threadsafe(
group.async_get_service(self.hass, {'services': [
{'service': 'demo1'},
{'service': 'demo2',
'data': {'target': 'unnamed device',
'data': {'test': 'message'}}}]}),
self.hass.loop
).result()
assert self.service is not None
@@ -51,17 +58,19 @@ class TestNotifyGroup(unittest.TestCase):
def test_send_message_with_data(self):
"""Test sending a message with to a notify group."""
self.service.send_message('Hello', title='Test notification',
data={'hello': 'world'})
run_coroutine_threadsafe(
self.service.async_send_message(
'Hello', title='Test notification', data={'hello': 'world'}),
self.hass.loop).result()
self.hass.block_till_done()
assert self.service1.send_message.mock_calls[0][1][0] == 'Hello'
assert self.service1.send_message.mock_calls[0][2] == {
'message': 'Hello',
'title': 'Test notification',
'data': {'hello': 'world'}
}
assert self.service2.send_message.mock_calls[0][1][0] == 'Hello'
assert self.service2.send_message.mock_calls[0][2] == {
'message': 'Hello',
'target': ['unnamed device'],
'title': 'Test notification',
'data': {'hello': 'world', 'test': 'message'}