diff --git a/homeassistant/components/zha/helpers.py b/homeassistant/components/zha/helpers.py index 5e84eca36ce..4f60e8b32b2 100644 --- a/homeassistant/components/zha/helpers.py +++ b/homeassistant/components/zha/helpers.py @@ -1116,7 +1116,17 @@ async def async_add_entities( if not entities: 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) entities.clear() diff --git a/tests/components/zha/test_light.py b/tests/components/zha/test_light.py index 6a7ede341f5..ef2714b3b58 100644 --- a/tests/components/zha/test_light.py +++ b/tests/components/zha/test_light.py @@ -556,3 +556,41 @@ async def async_test_flash_from_hass( manufacturer=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