Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def get_test_instance_port():
return _TEST_INSTANCE_PORT


def mock_service(hass, domain, service):
@ha.callback
def async_mock_service(hass, domain, service):
"""Set up a fake service & return a calls log list to this service."""
calls = []

Expand All @@ -186,14 +187,14 @@ def mock_service_log(call): # pylint: disable=unnecessary-lambda
"""Mock service call."""
calls.append(call)

if hass.loop.__dict__.get("_thread_ident", 0) == threading.get_ident():
hass.services.async_register(domain, service, mock_service_log)
else:
hass.services.register(domain, service, mock_service_log)
hass.services.async_register(domain, service, mock_service_log)

return calls


mock_service = threadsafe_callback_factory(async_mock_service)


@ha.callback
def async_fire_mqtt_message(hass, topic, payload, qos=0):
"""Fire the MQTT message."""
Expand Down
6 changes: 3 additions & 3 deletions tests/components/automation/test_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation

from tests.common import mock_service, mock_coro
from tests.common import async_mock_service, mock_coro


@asyncio.coroutine
def test_if_fires_on_hass_start(hass):
"""Test the firing when HASS starts."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running
config = {
automation.DOMAIN: {
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_if_fires_on_hass_start(hass):
@asyncio.coroutine
def test_if_fires_on_hass_shutdown(hass):
"""Test the firing when HASS starts."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running

res = yield from async_setup_component(hass, automation.DOMAIN, {
Expand Down
18 changes: 9 additions & 9 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from tests.common import (
assert_setup_component, get_test_home_assistant, fire_time_changed,
mock_service, mock_restore_cache)
mock_service, async_mock_service, mock_restore_cache)


# pylint: disable=invalid-name
Expand Down Expand Up @@ -565,7 +565,7 @@ def test_automation_restore_state(hass):
assert state.state == STATE_OFF
assert state.attributes.get('last_triggered') == time

calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')

assert automation.is_on(hass, 'automation.bye') is False

Expand All @@ -584,7 +584,7 @@ def test_automation_restore_state(hass):
@asyncio.coroutine
def test_initial_value_off(hass):
"""Test initial value off."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')

res = yield from async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
Expand All @@ -611,7 +611,7 @@ def test_initial_value_off(hass):
@asyncio.coroutine
def test_initial_value_on(hass):
"""Test initial value on."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')

res = yield from async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
Expand All @@ -638,7 +638,7 @@ def test_initial_value_on(hass):
@asyncio.coroutine
def test_initial_value_off_but_restore_on(hass):
"""Test initial value off and restored state is turned on."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')
mock_restore_cache(hass, (
State('automation.hello', STATE_ON),
))
Expand Down Expand Up @@ -668,7 +668,7 @@ def test_initial_value_off_but_restore_on(hass):
@asyncio.coroutine
def test_initial_value_on_but_restore_off(hass):
"""Test initial value on and restored state is turned off."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')
mock_restore_cache(hass, (
State('automation.hello', STATE_OFF),
))
Expand Down Expand Up @@ -698,7 +698,7 @@ def test_initial_value_on_but_restore_off(hass):
@asyncio.coroutine
def test_no_initial_value_and_restore_off(hass):
"""Test initial value off and restored state is turned on."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')
mock_restore_cache(hass, (
State('automation.hello', STATE_OFF),
))
Expand Down Expand Up @@ -727,7 +727,7 @@ def test_no_initial_value_and_restore_off(hass):
@asyncio.coroutine
def test_automation_is_on_if_no_initial_state_or_restore(hass):
"""Test initial value is on when no initial state or restored state."""
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')

res = yield from async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
Expand All @@ -754,7 +754,7 @@ def test_automation_is_on_if_no_initial_state_or_restore(hass):
def test_automation_not_trigger_on_bootstrap(hass):
"""Test if automation is not trigger on bootstrap."""
hass.state = CoreState.not_running
calls = mock_service(hass, 'test', 'automation')
calls = async_mock_service(hass, 'test', 'automation')

res = yield from async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
Expand Down
5 changes: 3 additions & 2 deletions tests/components/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from homeassistant.util.async import run_coroutine_threadsafe

from tests.common import (
get_test_home_assistant, mock_service, patch_yaml_files, mock_coro)
get_test_home_assistant, mock_service, patch_yaml_files, mock_coro,
async_mock_service)


class TestComponentsCore(unittest.TestCase):
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_toggle(self):
@patch('homeassistant.core.ServiceRegistry.call')
def test_turn_on_to_not_block_for_domains_without_service(self, mock_call):
"""Test if turn_on is blocking domain with no service."""
mock_service(self.hass, 'light', SERVICE_TURN_ON)
async_mock_service(self.hass, 'light', SERVICE_TURN_ON)

# We can't test if our service call results in services being called
# because by mocking out the call service method, we mock out all
Expand Down