88import logging
99import sys
1010
11- from .const import ALLOWED_IMPORTS , LOGGER_PATH
11+ from .const import ALLOWED_IMPORTS , DOMAIN , LOGGER_PATH
1212
1313_LOGGER = logging .getLogger (LOGGER_PATH + ".eval" )
1414
@@ -361,6 +361,11 @@ def __init__(
361361 self .logger_handlers = set ()
362362 self .logger = None
363363 self .set_logger_name (logger_name if logger_name is not None else self .name )
364+ self .allow_all_imports = (
365+ global_ctx .hass .data [DOMAIN ]["allow_all_imports" ]
366+ if global_ctx .hass is not None
367+ else False
368+ )
364369
365370 async def ast_not_implemented (self , arg , * args ):
366371 """Raise NotImplementedError exception for unimplemented AST types."""
@@ -399,7 +404,7 @@ async def ast_module(self, arg):
399404 async def ast_import (self , arg ):
400405 """Execute import."""
401406 for imp in arg .names :
402- if imp .name not in ALLOWED_IMPORTS :
407+ if not self . allow_all_imports and imp .name not in ALLOWED_IMPORTS :
403408 raise ModuleNotFoundError (f"import of { imp .name } not allowed" )
404409 if imp .name not in sys .modules :
405410 mod = importlib .import_module (imp .name )
@@ -409,7 +414,7 @@ async def ast_import(self, arg):
409414
410415 async def ast_importfrom (self , arg ):
411416 """Execute from X import Y."""
412- if arg .module not in ALLOWED_IMPORTS :
417+ if not self . allow_all_imports and arg .module not in ALLOWED_IMPORTS :
413418 raise ModuleNotFoundError (f"import from { arg .module } not allowed" )
414419 if arg .module not in sys .modules :
415420 mod = importlib .import_module (arg .module )
0 commit comments