1
mirror of https://github.com/home-assistant/core synced 2024-09-28 03:04:04 +02:00
ha-core/homeassistant/scripts/ensure_config.py

47 lines
1.3 KiB
Python
Raw Normal View History

2016-07-18 00:24:42 +02:00
"""Script to ensure a configuration file exists."""
import argparse
import asyncio
2016-07-18 00:24:42 +02:00
import os
import homeassistant.config as config_util
from homeassistant.core import HomeAssistant
2016-07-18 00:24:42 +02:00
# mypy: allow-untyped-calls, allow-untyped-defs
2019-07-31 21:25:30 +02:00
2016-07-18 00:24:42 +02:00
def run(args):
"""Handle ensure config commandline script."""
parser = argparse.ArgumentParser(
description=("Ensure a Home Assistant config exists, creates one if necessary.")
2019-07-31 21:25:30 +02:00
)
2016-07-18 00:24:42 +02:00
parser.add_argument(
2019-07-31 21:25:30 +02:00
"-c",
"--config",
metavar="path_to_config_dir",
2016-07-18 00:24:42 +02:00
default=config_util.get_default_config_dir(),
2019-07-31 21:25:30 +02:00
help="Directory that contains the Home Assistant configuration",
)
parser.add_argument("--script", choices=["ensure_config"])
2016-07-18 00:24:42 +02:00
args = parser.parse_args()
config_dir = os.path.join(os.getcwd(), args.config)
# Test if configuration directory exists
if not os.path.isdir(config_dir):
2019-07-31 21:25:30 +02:00
print("Creating directory", config_dir)
os.makedirs(config_dir, exist_ok=True)
2016-07-18 00:24:42 +02:00
config_path = asyncio.run(async_run(config_dir))
2019-07-31 21:25:30 +02:00
print("Configuration file:", config_path)
2016-07-18 00:24:42 +02:00
return 0
async def async_run(config_dir):
"""Make sure config exists."""
hass = HomeAssistant()
hass.config.config_dir = config_dir
path = await config_util.async_ensure_config_exists(hass)
await hass.async_stop(force=True)
return path