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
@@ -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)
|
||||
Reference in new issue
Block a user