예제 #1
0
def _identify_config_schema(module: ModuleType) -> Optional[str]:
    """Extract the schema and identify list or dict based."""
    try:
        key = next(k for k in module.CONFIG_SCHEMA.schema if k == module.DOMAIN)  # type: ignore
    except (AttributeError, StopIteration):
        return None

    schema = module.CONFIG_SCHEMA.schema[key]  # type: ignore

    if hasattr(key, "default") and not isinstance(
        key.default, vol.schema_builder.Undefined
    ):
        default_value = module.CONFIG_SCHEMA({module.DOMAIN: key.default()})[  # type: ignore
            module.DOMAIN  # type: ignore
        ]

        if isinstance(default_value, dict):
            return "dict"

        if isinstance(default_value, list):
            return "list"

        return None

    t_schema = str(schema)
    if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema:
        return "dict"
    if t_schema.startswith(("[", "All(<function ensure_list")):
        return "list"
    return None
예제 #2
0
def _identify_config_schema(module: ModuleType) -> str | None:
    """Extract the schema and identify list or dict based."""
    if not isinstance(module.CONFIG_SCHEMA, vol.Schema):  # type: ignore
        return None

    schema = module.CONFIG_SCHEMA.schema  # type: ignore

    if isinstance(schema, vol.All):
        for subschema in schema.validators:
            if isinstance(subschema, dict):
                schema = subschema
                break
        else:
            return None

    try:
        key = next(k for k in schema if k == module.DOMAIN)  # type: ignore
    except (TypeError, AttributeError, StopIteration):
        return None
    except Exception:  # pylint: disable=broad-except
        _LOGGER.exception("Unexpected error identifying config schema")
        return None

    if hasattr(key, "default") and not isinstance(
            key.default, vol.schema_builder.Undefined):
        default_value = module.CONFIG_SCHEMA({module.DOMAIN:
                                              key.default()})[  # type: ignore
                                                  module.DOMAIN  # type: ignore
                                              ]

        if isinstance(default_value, dict):
            return "dict"

        if isinstance(default_value, list):
            return "list"

        return None

    domain_schema = schema[key]

    t_schema = str(domain_schema)
    if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema:
        return "dict"
    if t_schema.startswith(("[", "All(<function ensure_list")):
        return "list"
    return None