mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Added support for default value when environment variable is missing (#8484)
* Added support for a default value when an environment variable is missing * Shouldn't have used docstring
This commit is contained in:
parent
98568b5eb7
commit
ecc1429453
@ -205,8 +205,13 @@ def _construct_seq(loader: SafeLineLoader, node: yaml.nodes.Node):
|
||||
def _env_var_yaml(loader: SafeLineLoader,
|
||||
node: yaml.nodes.Node):
|
||||
"""Load environment variables and embed it into the configuration YAML."""
|
||||
if node.value in os.environ:
|
||||
return os.environ[node.value]
|
||||
args = node.value.split()
|
||||
|
||||
# Check for a default value
|
||||
if len(args) > 1:
|
||||
return os.getenv(args[0], ' '.join(args[1:]))
|
||||
elif args[0] in os.environ:
|
||||
return os.environ[args[0]]
|
||||
else:
|
||||
_LOGGER.error("Environment variable %s not defined.", node.value)
|
||||
raise HomeAssistantError(node.value)
|
||||
|
@ -60,6 +60,13 @@ class TestYaml(unittest.TestCase):
|
||||
assert doc['password'] == "secret_password"
|
||||
del os.environ["PASSWORD"]
|
||||
|
||||
def test_environment_variable_default(self):
|
||||
"""Test config file with default value for environment variable."""
|
||||
conf = "password: !env_var PASSWORD secret_password"
|
||||
with io.StringIO(conf) as file:
|
||||
doc = yaml.yaml.safe_load(file)
|
||||
assert doc['password'] == "secret_password"
|
||||
|
||||
def test_invalid_enviroment_variable(self):
|
||||
"""Test config file with no enviroment variable sat."""
|
||||
conf = "password: !env_var PASSWORD"
|
||||
|
Loading…
x
Reference in New Issue
Block a user