@@ -418,6 +418,32 @@ def _is_concatenated_template_match(template_str: str, match: re.Match[str]) ->
418418 return before_literal .endswith ("~" ) or after_literal .startswith ("~" )
419419
420420
421+ def _is_jinja_import_match (template_str : str , match : re .Match [str ]) -> bool :
422+ """Return if a quoted entity-like literal is a Jinja import filename."""
423+ groups = match .groups ()
424+ if len (groups ) == _STATES_DOMAIN_ENTITY_GROUPS :
425+ return False
426+
427+ entity_start , entity_end = match .span (1 )
428+ block_start = template_str .rfind ("{%" , 0 , entity_start )
429+ expression_start = template_str .rfind ("{{" , 0 , entity_start )
430+ if block_start == - 1 or expression_start > block_start :
431+ return False
432+
433+ block_end = template_str .find ("%}" , entity_end )
434+ expression_end = template_str .find ("}}" , entity_end )
435+ if block_end == - 1 or (expression_end != - 1 and expression_end < block_end ):
436+ return False
437+
438+ block = template_str [block_start : block_end + 2 ]
439+ return bool (
440+ re .match (
441+ r"\{%-?\s*(?:from\s+['\"][^'\"]+['\"]\s+import|import\s+['\"][^'\"]+['\"]\s+as)" ,
442+ block ,
443+ )
444+ )
445+
446+
421447def _is_string_method_argument_match (template_str : str , match : re .Match [str ]) -> bool :
422448 """Return if an entity-like literal is used as a string method argument."""
423449 groups = match .groups ()
@@ -473,9 +499,11 @@ def extract_entities_from_template_regex(
473499
474500 for pattern in ENTITY_ID_TEMPLATE_PATTERNS :
475501 for match in re .finditer (pattern , template_str , re .IGNORECASE ):
476- if _is_concatenated_template_match (
477- template_str , match
478- ) or _is_string_method_argument_match (template_str , match ):
502+ if (
503+ _is_concatenated_template_match (template_str , match )
504+ or _is_jinja_import_match (template_str , match )
505+ or _is_string_method_argument_match (template_str , match )
506+ ):
479507 continue
480508
481509 entity_id = _entity_id_from_template_match (match )
0 commit comments