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:
Paulus Schoutsen authored and GitHub committed 2018-04-09 10:09:08 -04:00
1 parent cb51553c2d
commit 73de749411
12 files changed
+271 -45

No files matched your search

+45 -1
View File
@@ -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