Convert discovery tests to async (#64411)

This commit is contained in:
Paulus Schoutsen 2022-01-19 03:21:15 -08:00 committed by GitHub
parent e6c7c01e6c
commit 8d7cca9774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,38 +1,31 @@
"""Test discovery helpers.""" """Test discovery helpers."""
from unittest.mock import patch from unittest.mock import patch
import pytest
from homeassistant import setup from homeassistant import setup
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.util.async_ import run_callback_threadsafe
from tests.common import ( from tests.common import (
MockModule, MockModule,
MockPlatform, MockPlatform,
get_test_home_assistant,
mock_coro,
mock_entity_platform, mock_entity_platform,
mock_integration, mock_integration,
) )
class TestHelpersDiscovery: @pytest.fixture
"""Tests for discovery helper methods.""" def mock_setup_component():
"""Mock setup component."""
with patch("homeassistant.setup.async_setup_component", return_value=True) as mock:
yield mock
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method): async def test_listen(hass, mock_setup_component):
"""Stop everything that was started."""
self.hass.stop()
@patch("homeassistant.setup.async_setup_component", return_value=mock_coro())
def test_listen(self, mock_setup_component):
"""Test discovery listen/discover combo.""" """Test discovery listen/discover combo."""
helpers = self.hass.helpers
calls_single = [] calls_single = []
@callback @callback
@ -40,26 +33,24 @@ class TestHelpersDiscovery:
"""Service discovered callback.""" """Service discovered callback."""
calls_single.append((service, info)) calls_single.append((service, info))
self.hass.add_job( discovery.async_listen(hass, "test service", callback_single)
helpers.discovery.async_listen, "test service", callback_single
)
self.hass.add_job( await discovery.async_discover(
helpers.discovery.async_discover, hass,
"test service", "test service",
"discovery info", "discovery info",
"test_component", "test_component",
{}, {},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert mock_setup_component.called assert mock_setup_component.called
assert mock_setup_component.call_args[0] == (self.hass, "test_component", {}) assert mock_setup_component.call_args[0] == (hass, "test_component", {})
assert len(calls_single) == 1 assert len(calls_single) == 1
assert calls_single[0] == ("test service", "discovery info") assert calls_single[0] == ("test service", "discovery info")
@patch("homeassistant.setup.async_setup_component", return_value=mock_coro(True))
def test_platform(self, mock_setup_component): async def test_platform(hass, mock_setup_component):
"""Test discover platform method.""" """Test discover platform method."""
calls = [] calls = []
@ -68,52 +59,52 @@ class TestHelpersDiscovery:
"""Platform callback method.""" """Platform callback method."""
calls.append((platform, info)) calls.append((platform, info))
run_callback_threadsafe( discovery.async_listen_platform(
self.hass.loop, hass,
discovery.async_listen_platform,
self.hass,
"test_component", "test_component",
platform_callback, platform_callback,
).result() )
discovery.load_platform( await discovery.async_load_platform(
self.hass, hass,
"test_component", "test_component",
"test_platform", "test_platform",
"discovery info", "discovery info",
{"test_component": {}}, {"test_component": {}},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert mock_setup_component.called assert mock_setup_component.called
assert mock_setup_component.call_args[0] == ( assert mock_setup_component.call_args[0] == (
self.hass, hass,
"test_component", "test_component",
{"test_component": {}}, {"test_component": {}},
) )
self.hass.block_till_done() await hass.async_block_till_done()
discovery.load_platform( await hass.async_add_executor_job(
self.hass, discovery.load_platform,
hass,
"test_component_2", "test_component_2",
"test_platform", "test_platform",
"discovery info", "discovery info",
{"test_component": {}}, {"test_component": {}},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert calls[0] == ("test_platform", "discovery info") assert calls[0] == ("test_platform", "discovery info")
dispatcher_send( async_dispatcher_send(
self.hass, hass,
discovery.SIGNAL_PLATFORM_DISCOVERED, discovery.SIGNAL_PLATFORM_DISCOVERED,
{"service": discovery.EVENT_LOAD_PLATFORM.format("test_component")}, {"service": discovery.EVENT_LOAD_PLATFORM.format("test_component")},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
def test_circular_import(self):
async def test_circular_import(hass):
"""Test we don't break doing circular import. """Test we don't break doing circular import.
This test will have test_component discover the switch.test_circular This test will have test_component discover the switch.test_circular
@ -140,35 +131,31 @@ class TestHelpersDiscovery:
"""Set up mock platform.""" """Set up mock platform."""
platform_calls.append("disc" if discovery_info else "component") platform_calls.append("disc" if discovery_info else "component")
mock_integration(self.hass, MockModule("test_component", setup=component_setup)) mock_integration(hass, MockModule("test_component", setup=component_setup))
# dependencies are only set in component level # dependencies are only set in component level
# since we are using manifest to hold them # since we are using manifest to hold them
mock_integration( mock_integration(hass, MockModule("test_circular", dependencies=["test_component"]))
self.hass, MockModule("test_circular", dependencies=["test_component"]) mock_entity_platform(hass, "switch.test_circular", MockPlatform(setup_platform))
)
mock_entity_platform(
self.hass, "switch.test_circular", MockPlatform(setup_platform)
)
setup.setup_component( await setup.async_setup_component(
self.hass, hass,
"test_component", "test_component",
{"test_component": None, "switch": [{"platform": "test_circular"}]}, {"test_component": None, "switch": [{"platform": "test_circular"}]},
) )
self.hass.block_till_done() await hass.async_block_till_done()
# test_component will only be setup once # test_component will only be setup once
assert len(component_calls) == 1 assert len(component_calls) == 1
# The platform will be setup once via the config in `setup_component` # The platform will be setup once via the config in `setup_component`
# and once via the discovery inside test_component. # and once via the discovery inside test_component.
assert len(platform_calls) == 2 assert len(platform_calls) == 2
assert "test_component" in self.hass.config.components assert "test_component" in hass.config.components
assert "switch" in self.hass.config.components assert "switch" in hass.config.components
@patch("homeassistant.helpers.signal.async_register_signal_handling")
def test_1st_discovers_2nd_component(self, mock_signal): async def test_1st_discovers_2nd_component(hass):
"""Test that we don't break if one component discovers the other. """Test that we don't break if one component discovers the other.
If the first component fires a discovery event to set up the If the first component fires a discovery event to set up the
@ -190,26 +177,13 @@ class TestHelpersDiscovery:
component_calls.append(1) component_calls.append(1)
return True return True
mock_integration( mock_integration(hass, MockModule("test_component1", async_setup=component1_setup))
self.hass, MockModule("test_component1", async_setup=component1_setup)
)
mock_integration( mock_integration(hass, MockModule("test_component2", setup=component2_setup))
self.hass, MockModule("test_component2", setup=component2_setup)
)
@callback hass.async_create_task(setup.async_setup_component(hass, "test_component1", {}))
def do_setup(): hass.async_create_task(setup.async_setup_component(hass, "test_component2", {}))
"""Set up 2 components.""" await hass.async_block_till_done()
self.hass.async_add_job(
setup.async_setup_component(self.hass, "test_component1", {})
)
self.hass.async_add_job(
setup.async_setup_component(self.hass, "test_component2", {})
)
self.hass.add_job(do_setup)
self.hass.block_till_done()
# test_component will only be setup once # test_component will only be setup once
assert len(component_calls) == 1 assert len(component_calls) == 1