mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Split mock_service (#8198)
This commit is contained in:
parent
1cfed4f015
commit
7bf6ceafec
@ -177,7 +177,8 @@ def get_test_instance_port():
|
|||||||
return _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."""
|
"""Set up a fake service & return a calls log list to this service."""
|
||||||
calls = []
|
calls = []
|
||||||
|
|
||||||
@ -186,14 +187,14 @@ def mock_service(hass, domain, service):
|
|||||||
"""Mock service call."""
|
"""Mock service call."""
|
||||||
calls.append(call)
|
calls.append(call)
|
||||||
|
|
||||||
if hass.loop.__dict__.get("_thread_ident", 0) == threading.get_ident():
|
hass.services.async_register(domain, service, mock_service_log)
|
||||||
hass.services.async_register(domain, service, mock_service_log)
|
|
||||||
else:
|
|
||||||
hass.services.register(domain, service, mock_service_log)
|
|
||||||
|
|
||||||
return calls
|
return calls
|
||||||
|
|
||||||
|
|
||||||
|
mock_service = threadsafe_callback_factory(async_mock_service)
|
||||||
|
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def async_fire_mqtt_message(hass, topic, payload, qos=0):
|
def async_fire_mqtt_message(hass, topic, payload, qos=0):
|
||||||
"""Fire the MQTT message."""
|
"""Fire the MQTT message."""
|
||||||
|
@ -6,13 +6,13 @@ from homeassistant.core import CoreState
|
|||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.components.automation as automation
|
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
|
@asyncio.coroutine
|
||||||
def test_if_fires_on_hass_start(hass):
|
def test_if_fires_on_hass_start(hass):
|
||||||
"""Test the firing when HASS starts."""
|
"""Test the firing when HASS starts."""
|
||||||
calls = mock_service(hass, 'test', 'automation')
|
calls = async_mock_service(hass, 'test', 'automation')
|
||||||
hass.state = CoreState.not_running
|
hass.state = CoreState.not_running
|
||||||
config = {
|
config = {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
@ -48,7 +48,7 @@ def test_if_fires_on_hass_start(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_if_fires_on_hass_shutdown(hass):
|
def test_if_fires_on_hass_shutdown(hass):
|
||||||
"""Test the firing when HASS starts."""
|
"""Test the firing when HASS starts."""
|
||||||
calls = mock_service(hass, 'test', 'automation')
|
calls = async_mock_service(hass, 'test', 'automation')
|
||||||
hass.state = CoreState.not_running
|
hass.state = CoreState.not_running
|
||||||
|
|
||||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||||
|
@ -14,7 +14,7 @@ import homeassistant.util.dt as dt_util
|
|||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
assert_setup_component, get_test_home_assistant, fire_time_changed,
|
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
|
# pylint: disable=invalid-name
|
||||||
@ -565,7 +565,7 @@ def test_automation_restore_state(hass):
|
|||||||
assert state.state == STATE_OFF
|
assert state.state == STATE_OFF
|
||||||
assert state.attributes.get('last_triggered') == time
|
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
|
assert automation.is_on(hass, 'automation.bye') is False
|
||||||
|
|
||||||
@ -584,7 +584,7 @@ def test_automation_restore_state(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_initial_value_off(hass):
|
def test_initial_value_off(hass):
|
||||||
"""Test initial value off."""
|
"""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, {
|
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
@ -611,7 +611,7 @@ def test_initial_value_off(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_initial_value_on(hass):
|
def test_initial_value_on(hass):
|
||||||
"""Test initial value on."""
|
"""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, {
|
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
@ -638,7 +638,7 @@ def test_initial_value_on(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_initial_value_off_but_restore_on(hass):
|
def test_initial_value_off_but_restore_on(hass):
|
||||||
"""Test initial value off and restored state is turned on."""
|
"""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, (
|
mock_restore_cache(hass, (
|
||||||
State('automation.hello', STATE_ON),
|
State('automation.hello', STATE_ON),
|
||||||
))
|
))
|
||||||
@ -668,7 +668,7 @@ def test_initial_value_off_but_restore_on(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_initial_value_on_but_restore_off(hass):
|
def test_initial_value_on_but_restore_off(hass):
|
||||||
"""Test initial value on and restored state is turned off."""
|
"""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, (
|
mock_restore_cache(hass, (
|
||||||
State('automation.hello', STATE_OFF),
|
State('automation.hello', STATE_OFF),
|
||||||
))
|
))
|
||||||
@ -698,7 +698,7 @@ def test_initial_value_on_but_restore_off(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_no_initial_value_and_restore_off(hass):
|
def test_no_initial_value_and_restore_off(hass):
|
||||||
"""Test initial value off and restored state is turned on."""
|
"""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, (
|
mock_restore_cache(hass, (
|
||||||
State('automation.hello', STATE_OFF),
|
State('automation.hello', STATE_OFF),
|
||||||
))
|
))
|
||||||
@ -727,7 +727,7 @@ def test_no_initial_value_and_restore_off(hass):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_automation_is_on_if_no_initial_state_or_restore(hass):
|
def test_automation_is_on_if_no_initial_state_or_restore(hass):
|
||||||
"""Test initial value is on when no initial state or restored state."""
|
"""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, {
|
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
@ -754,7 +754,7 @@ def test_automation_is_on_if_no_initial_state_or_restore(hass):
|
|||||||
def test_automation_not_trigger_on_bootstrap(hass):
|
def test_automation_not_trigger_on_bootstrap(hass):
|
||||||
"""Test if automation is not trigger on bootstrap."""
|
"""Test if automation is not trigger on bootstrap."""
|
||||||
hass.state = CoreState.not_running
|
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, {
|
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||||
automation.DOMAIN: {
|
automation.DOMAIN: {
|
||||||
|
@ -16,7 +16,8 @@ from homeassistant.helpers import entity
|
|||||||
from homeassistant.util.async import run_coroutine_threadsafe
|
from homeassistant.util.async import run_coroutine_threadsafe
|
||||||
|
|
||||||
from tests.common import (
|
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):
|
class TestComponentsCore(unittest.TestCase):
|
||||||
@ -77,7 +78,7 @@ class TestComponentsCore(unittest.TestCase):
|
|||||||
@patch('homeassistant.core.ServiceRegistry.call')
|
@patch('homeassistant.core.ServiceRegistry.call')
|
||||||
def test_turn_on_to_not_block_for_domains_without_service(self, mock_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."""
|
"""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
|
# 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
|
# because by mocking out the call service method, we mock out all
|
||||||
|
Loading…
x
Reference in New Issue
Block a user