Skip to content
Snippets Groups Projects
Commit a557cf38 authored by Paulus Schoutsen's avatar Paulus Schoutsen
Browse files

Better approach writing default configuration

parent 17af1a68
No related branches found
No related tags found
No related merge requests found
......@@ -19,8 +19,20 @@ _LOGGER = logging.getLogger(__name__)
YAML_CONFIG_FILE = 'configuration.yaml'
CONF_CONFIG_FILE = 'home-assistant.conf'
DEFAULT_CONFIG = [
# Tuples (attribute, default, auto detect property, description)
(CONF_NAME, 'Home', None, 'Name of the location where Home Assistant is '
'running'),
(CONF_LATITUDE, None, 'latitude', 'Location required to calculate the time'
' the sun rises and sets'),
(CONF_LONGITUDE, None, 'longitude', None),
(CONF_TEMPERATURE_UNIT, 'C', None, 'C for Celcius, F for Fahrenheit'),
(CONF_TIME_ZONE, 'UTC', 'time_zone', 'Pick yours from here: http://en.wiki'
'pedia.org/wiki/List_of_tz_database_time_zones'),
]
DEFAULT_COMPONENTS = [
'discovery', 'frontend', 'conversation', 'history', 'logbook']
'discovery', 'frontend', 'conversation', 'history', 'logbook', 'sun']
def ensure_config_exists(config_dir, detect_location=True):
......@@ -41,29 +53,33 @@ def create_default_config(config_dir, detect_location=True):
Returns path to new config file if success, None if failed. """
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
info = {attr: default for attr, default, *_ in DEFAULT_CONFIG}
location_info = detect_location and util.detect_location_info()
if location_info:
if location_info.use_fahrenheit:
info[CONF_TEMPERATURE_UNIT] = 'F'
for attr, default, prop, _ in DEFAULT_CONFIG:
if prop is None:
continue
info[attr] = getattr(location_info, prop) or default
# Writing files with YAML does not create the most human readable results
# So we're hard coding a YAML template.
try:
with open(config_path, 'w') as config_file:
location_info = detect_location and util.detect_location_info()
if location_info:
temp_unit = 'F' if location_info.use_fahrenheit else 'C'
auto_config = {
CONF_NAME: 'Home',
CONF_LATITUDE: location_info.latitude,
CONF_LONGITUDE: location_info.longitude,
CONF_TEMPERATURE_UNIT: temp_unit,
CONF_TIME_ZONE: location_info.time_zone,
}
config_file.write("homeassistant:\n")
config_file.write("homeassistant:\n")
for key, value in auto_config.items():
config_file.write(" {}: {}\n".format(key, value))
for attr, _, _, description in DEFAULT_CONFIG:
if info[attr] is None:
continue
elif description:
config_file.write(" # {}\n".format(description))
config_file.write(" {}: {}\n".format(attr, info[attr]))
config_file.write("\n")
config_file.write("\n")
for component in DEFAULT_COMPONENTS:
config_file.write("{}:\n\n".format(component))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment