Use config entry to setup platforms (#13752)
* Use config entry to setup platforms * Rename to async_forward_entry * Add tests * Catch if platform not exists for entry
This commit is contained in:
1 parent
cb51553c2d
commit
73de749411
12 files changed
+271
-45
No files matched your search
+13
-1
@@ -344,7 +344,8 @@ class MockPlatform(object):
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def __init__(self, setup_platform=None, dependencies=None,
|
||||
platform_schema=None, async_setup_platform=None):
|
||||
platform_schema=None, async_setup_platform=None,
|
||||
async_setup_entry=None):
|
||||
"""Initialize the platform."""
|
||||
self.DEPENDENCIES = dependencies or []
|
||||
|
||||
@@ -358,6 +359,9 @@ class MockPlatform(object):
|
||||
if async_setup_platform is not None:
|
||||
self.async_setup_platform = async_setup_platform
|
||||
|
||||
if async_setup_entry is not None:
|
||||
self.async_setup_entry = async_setup_entry
|
||||
|
||||
if setup_platform is None and async_setup_platform is None:
|
||||
self.async_setup_platform = mock_coro_func()
|
||||
|
||||
@@ -376,6 +380,14 @@ class MockEntityPlatform(entity_platform.EntityPlatform):
|
||||
async_entities_added_callback=lambda: None
|
||||
):
|
||||
"""Initialize a mock entity platform."""
|
||||
if logger is None:
|
||||
logger = logging.getLogger('homeassistant.helpers.entity_platform')
|
||||
|
||||
# Otherwise the constructor will blow up.
|
||||
if (isinstance(platform, Mock) and
|
||||
isinstance(platform.PARALLEL_UPDATES, Mock)):
|
||||
platform.PARALLEL_UPDATES = 0
|
||||
|
||||
super().__init__(
|
||||
hass=hass,
|
||||
logger=logger,
|
||||
|
||||
@@ -18,10 +18,9 @@ async def test_bridge_setup():
|
||||
assert await hue_bridge.async_setup() is True
|
||||
|
||||
assert hue_bridge.api is api
|
||||
assert len(hass.helpers.discovery.async_load_platform.mock_calls) == 1
|
||||
assert hass.helpers.discovery.async_load_platform.mock_calls[0][1][2] == {
|
||||
'host': '1.2.3.4'
|
||||
}
|
||||
assert len(hass.config_entries.async_forward_entry.mock_calls) == 1
|
||||
assert hass.config_entries.async_forward_entry.mock_calls[0][1] == \
|
||||
(entry, 'light')
|
||||
|
||||
|
||||
async def test_bridge_setup_invalid_username():
|
||||
|
||||
@@ -9,6 +9,7 @@ from aiohue.lights import Lights
|
||||
from aiohue.groups import Groups
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import hue
|
||||
import homeassistant.components.light.hue as hue_light
|
||||
from homeassistant.util import color
|
||||
@@ -196,9 +197,11 @@ async def setup_bridge(hass, mock_bridge):
|
||||
"""Load the Hue light platform with the provided bridge."""
|
||||
hass.config.components.add(hue.DOMAIN)
|
||||
hass.data[hue.DOMAIN] = {'mock-host': mock_bridge}
|
||||
await hass.helpers.discovery.async_load_platform('light', 'hue', {
|
||||
config_entry = config_entries.ConfigEntry(1, hue.DOMAIN, 'Mock Title', {
|
||||
'host': 'mock-host'
|
||||
})
|
||||
}, 'test')
|
||||
await hass.config_entries.async_forward_entry(config_entry, 'light')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import unittest
|
||||
from unittest.mock import patch, Mock
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.core as ha
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
@@ -19,7 +21,7 @@ import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, MockPlatform, MockModule, mock_coro,
|
||||
async_fire_time_changed, MockEntity)
|
||||
async_fire_time_changed, MockEntity, MockConfigEntry)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DOMAIN = "test_domain"
|
||||
@@ -333,3 +335,44 @@ def test_setup_dependencies_platform(hass):
|
||||
assert 'test_component' in hass.config.components
|
||||
assert 'test_component2' in hass.config.components
|
||||
assert 'test_domain.test_component' in hass.config.components
|
||||
|
||||
|
||||
async def test_setup_entry(hass):
|
||||
"""Test setup entry calls async_setup_entry on platform."""
|
||||
mock_setup_entry = Mock(return_value=mock_coro(True))
|
||||
loader.set_component(
|
||||
'test_domain.entry_domain',
|
||||
MockPlatform(async_setup_entry=mock_setup_entry))
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
entry = MockConfigEntry(domain='entry_domain')
|
||||
|
||||
assert await component.async_setup_entry(entry)
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
p_hass, p_entry, p_add_entities = mock_setup_entry.mock_calls[0][1]
|
||||
assert p_hass is hass
|
||||
assert p_entry is entry
|
||||
|
||||
|
||||
async def test_setup_entry_platform_not_exist(hass):
|
||||
"""Test setup entry fails if platform doesnt exist."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
entry = MockConfigEntry(domain='non_existing')
|
||||
|
||||
assert (await component.async_setup_entry(entry)) is False
|
||||
|
||||
|
||||
async def test_setup_entry_fails_duplicate(hass):
|
||||
"""Test we don't allow setting up a config entry twice."""
|
||||
mock_setup_entry = Mock(return_value=mock_coro(True))
|
||||
loader.set_component(
|
||||
'test_domain.entry_domain',
|
||||
MockPlatform(async_setup_entry=mock_setup_entry))
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
entry = MockConfigEntry(domain='entry_domain')
|
||||
|
||||
assert await component.async_setup_entry(entry)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await component.async_setup_entry(entry)
|
||||
@@ -5,6 +5,7 @@ import unittest
|
||||
from unittest.mock import patch, Mock, MagicMock
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.helpers.entity_component import (
|
||||
@@ -15,7 +16,7 @@ import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, MockPlatform, fire_time_changed, mock_registry,
|
||||
MockEntity, MockEntityPlatform)
|
||||
MockEntity, MockEntityPlatform, MockConfigEntry, mock_coro)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DOMAIN = "test_domain"
|
||||
@@ -511,3 +512,46 @@ async def test_entity_registry_updates(hass):
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
assert state.name == 'after update'
|
||||
|
||||
|
||||
async def test_setup_entry(hass):
|
||||
"""Test we can setup an entry."""
|
||||
async_setup_entry = Mock(return_value=mock_coro(True))
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
config_entry = MockConfigEntry()
|
||||
entity_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
)
|
||||
|
||||
assert await entity_platform.async_setup_entry(config_entry)
|
||||
|
||||
full_name = '{}.{}'.format(entity_platform.domain, config_entry.domain)
|
||||
assert full_name in hass.config.components
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_entry_platform_not_ready(hass, caplog):
|
||||
"""Test when an entry is not ready yet."""
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady)
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
config_entry = MockConfigEntry()
|
||||
ent_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
)
|
||||
|
||||
with patch.object(entity_platform, 'async_call_later') as mock_call_later:
|
||||
assert not await ent_platform.async_setup_entry(config_entry)
|
||||
|
||||
full_name = '{}.{}'.format(ent_platform.domain, config_entry.domain)
|
||||
assert full_name not in hass.config.components
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
assert 'Platform test not ready yet' in caplog.text
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
@@ -389,3 +389,39 @@ def test_discovery_init_flow(manager):
|
||||
assert entry.title == 'hello'
|
||||
assert entry.data == data
|
||||
assert entry.source == config_entries.SOURCE_DISCOVERY
|
||||
|
||||
|
||||
async def test_forward_entry_sets_up_component(hass):
|
||||
"""Test we setup the component entry is forwarded to."""
|
||||
entry = MockConfigEntry(domain='original')
|
||||
|
||||
mock_original_setup_entry = MagicMock(return_value=mock_coro(True))
|
||||
loader.set_component(
|
||||
'original',
|
||||
MockModule('original', async_setup_entry=mock_original_setup_entry))
|
||||
|
||||
mock_forwarded_setup_entry = MagicMock(return_value=mock_coro(True))
|
||||
loader.set_component(
|
||||
'forwarded',
|
||||
MockModule('forwarded', async_setup_entry=mock_forwarded_setup_entry))
|
||||
|
||||
await hass.config_entries.async_forward_entry(entry, 'forwarded')
|
||||
assert len(mock_original_setup_entry.mock_calls) == 0
|
||||
assert len(mock_forwarded_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_forward_entry_does_not_setup_entry_if_setup_fails(hass):
|
||||
"""Test we do not setup entry if component setup fails."""
|
||||
entry = MockConfigEntry(domain='original')
|
||||
|
||||
mock_setup = MagicMock(return_value=mock_coro(False))
|
||||
mock_setup_entry = MagicMock()
|
||||
loader.set_component('forwarded', MockModule(
|
||||
'forwarded',
|
||||
async_setup=mock_setup,
|
||||
async_setup_entry=mock_setup_entry,
|
||||
))
|
||||
|
||||
await hass.config_entries.async_forward_entry(entry, 'forwarded')
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
Reference in new issue
Block a user