Apply ConfigEntryNotReady improvements to PlatformNotReady (#48665)
* Apply ConfigEntryNotReady improvements to PlatformNotReady - Limit log spam #47201 - Log exception reason #48449 - Prevent startup blockage #48660 * coverage
This commit is contained in:
parent
ecec3c8ab9
commit
b5c679f3d0
2 changed files with 107 additions and 13 deletions
|
@ -6,8 +6,8 @@ from unittest.mock import Mock, patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import PERCENTAGE
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, PERCENTAGE
|
||||
from homeassistant.core import CoreState, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dr,
|
||||
|
@ -592,6 +592,52 @@ async def test_setup_entry_platform_not_ready(hass, caplog):
|
|||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_entry_platform_not_ready_with_message(hass, caplog):
|
||||
"""Test when an entry is not ready yet that includes a message."""
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady("lp0 on fire"))
|
||||
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 = f"{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 "lp0 on fire" in caplog.text
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_entry_platform_not_ready_from_exception(hass, caplog):
|
||||
"""Test when an entry is not ready yet that includes the causing exception string."""
|
||||
original_exception = HomeAssistantError("The device dropped the connection")
|
||||
platform_exception = PlatformNotReady()
|
||||
platform_exception.__cause__ = original_exception
|
||||
|
||||
async_setup_entry = Mock(side_effect=platform_exception)
|
||||
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 = f"{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 "The device dropped the connection" in caplog.text
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reset_cancels_retry_setup(hass):
|
||||
"""Test that resetting a platform will cancel scheduled a setup retry."""
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady)
|
||||
|
@ -614,6 +660,31 @@ async def test_reset_cancels_retry_setup(hass):
|
|||
assert ent_platform._async_cancel_retry_setup is None
|
||||
|
||||
|
||||
async def test_reset_cancels_retry_setup_when_not_started(hass):
|
||||
"""Test that resetting a platform will cancel scheduled a setup retry when not yet started."""
|
||||
hass.state = CoreState.starting
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady)
|
||||
initial_listeners = hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED]
|
||||
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry()
|
||||
ent_platform = MockEntityPlatform(
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
assert not await ent_platform.async_setup_entry(config_entry)
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED] == initial_listeners + 1
|
||||
)
|
||||
assert ent_platform._async_cancel_retry_setup is not None
|
||||
|
||||
await ent_platform.async_reset()
|
||||
await hass.async_block_till_done()
|
||||
assert hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED] == initial_listeners
|
||||
assert ent_platform._async_cancel_retry_setup is None
|
||||
|
||||
|
||||
async def test_not_fails_with_adding_empty_entities_(hass):
|
||||
"""Test for not fails on empty entities list."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue