4
0
Эх сурвалжийг харах

Setup: print useful errors, allow config to proceed on exceptions

When we get exceptions during entity creation, the exception was being
thrown to HA to handle. But this loses some of the info such as which
config file the error is in, and will also prevent other entities using
the same platform from being enabled.

Catch the exception to prevent other entities from being affected, and print
a useful error message. There is already a separate exception thrown if
no entities are available to set up for the platform, so that will still be
thrown if there are no other entities that would have been blocked by an
exception being thrown during initialisation.

Issue #1487
Jason Rumney 2 жил өмнө
parent
commit
b7f9a60415

+ 27 - 9
custom_components/tuya_local/helpers/config.py

@@ -23,21 +23,39 @@ async def async_tuya_setup_platform(
         raise ValueError(f"No device config found for {discovery_info}")
     ecfg = cfg.primary_entity
     if ecfg.entity == platform:
-        data[ecfg.config_id] = entity_class(device, ecfg)
-        entities.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding {platform} for {ecfg.config_id}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == platform:
+        try:
             data[ecfg.config_id] = entity_class(device, ecfg)
             entities.append(data[ecfg.config_id])
             if ecfg.deprecated:
                 _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding {platform} for {ecfg.config_id}")
+            _LOGGER.debug(f"Adding %s for %s", platform, ecfg.config_id)
+        except Exception as e:
+            _LOGGER.error(
+                f"Error adding %s for %s: %s",
+                ecfg.config_id,
+                cfg.config,
+                e,
+            )
+
+    for ecfg in cfg.secondary_entities():
+        if ecfg.entity == platform:
+            try:
+                data[ecfg.config_id] = entity_class(device, ecfg)
+                entities.append(data[ecfg.config_id])
+                if ecfg.deprecated:
+                    _LOGGER.warning(ecfg.deprecation_message)
+                _LOGGER.debug(f"Adding %s for %s", platform, ecfg.config_id)
+            except Exception as e:
+                _LOGGER.error(
+                    f"Error adding %s for %s: %s",
+                    ecfg.config_id,
+                    cfg.config,
+                    e,
+                )
+
     if not entities:
         raise ValueError(f"{device.name} does not support use as a {platform} device.")
+
     async_add_entities(entities)