mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Prevent entity creation errors from causing all entities for a platform from loading in ZHA (#121631)
This commit is contained in:
parent
4245357403
commit
0aa6a17da8
@ -1116,7 +1116,17 @@ async def async_add_entities(
|
|||||||
if not entities:
|
if not entities:
|
||||||
return
|
return
|
||||||
|
|
||||||
entities_to_add = [entity_class(entity_data) for entity_data in entities]
|
entities_to_add = []
|
||||||
|
for entity_data in entities:
|
||||||
|
try:
|
||||||
|
entities_to_add.append(entity_class(entity_data))
|
||||||
|
# broad exception to prevent a single entity from preventing an entire platform from loading
|
||||||
|
# this can potentially be caused by a misbehaving device or a bad quirk. Not ideal but the
|
||||||
|
# alternative is adding try/catch to each entity class __init__ method with a specific exception
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Error while adding entity from entity data: %s", entity_data
|
||||||
|
)
|
||||||
_async_add_entities(entities_to_add, update_before_add=False)
|
_async_add_entities(entities_to_add, update_before_add=False)
|
||||||
entities.clear()
|
entities.clear()
|
||||||
|
|
||||||
|
@ -556,3 +556,41 @@ async def async_test_flash_from_hass(
|
|||||||
manufacturer=None,
|
manufacturer=None,
|
||||||
tsn=None,
|
tsn=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
"zigpy.zcl.clusters.lighting.Color.request",
|
||||||
|
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
|
||||||
|
)
|
||||||
|
@patch(
|
||||||
|
"zigpy.zcl.clusters.general.Identify.request",
|
||||||
|
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
|
||||||
|
)
|
||||||
|
@patch(
|
||||||
|
"zigpy.zcl.clusters.general.LevelControl.request",
|
||||||
|
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
|
||||||
|
)
|
||||||
|
@patch(
|
||||||
|
"zigpy.zcl.clusters.general.OnOff.request",
|
||||||
|
new=AsyncMock(return_value=[sentinel.data, zcl_f.Status.SUCCESS]),
|
||||||
|
)
|
||||||
|
async def test_light_exception_on_creation(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
setup_zha,
|
||||||
|
zigpy_device_mock,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test ZHA light entity creation exception."""
|
||||||
|
|
||||||
|
await setup_zha()
|
||||||
|
gateway = get_zha_gateway(hass)
|
||||||
|
zigpy_device = zigpy_device_mock(LIGHT_COLOR)
|
||||||
|
|
||||||
|
gateway.get_or_create_device(zigpy_device)
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.zha.light.Light.__init__", side_effect=Exception
|
||||||
|
):
|
||||||
|
await gateway.async_device_initialized(zigpy_device)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
assert "Error while adding entity from entity data" in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user