diff --git a/homeassistant/config.py b/homeassistant/config.py index b24c224f543503964cff10b8860d09f70b219ba0..57c38d5bc8cffee50b810e60163c19a6bf6ec33f 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -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))