def create_default_config(config_dir, detect_location=True): """ Creates a default configuration file in given config dir. 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: config_file.write("homeassistant:\n") 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") for component in DEFAULT_COMPONENTS: config_file.write("{}:\n\n".format(component)) return config_path except IOError: _LOGGER.exception( 'Unable to write default configuration file %s', config_path) return None
def create_default_config(config_dir, detect_location=True): """ Creates a default configuration file in given config dir. Returns path to new config file if success, None if failed. """ config_path = os.path.join(config_dir, YAML_CONFIG_FILE) # 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") for key, value in auto_config.items(): config_file.write(" {}: {}\n".format(key, value)) config_file.write("\n") for component in DEFAULT_COMPONENTS: config_file.write("{}:\n\n".format(component)) return config_path except IOError: _LOGGER.exception( 'Unable to write default configuration file %s', config_path) return None
def create_default_config(config_dir, detect_location=True): """ Creates a default configuration file in given config dir. Returns path to new config file if success, None if failed. """ config_path = os.path.join(config_dir, YAML_CONFIG_FILE) # 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") for key, value in auto_config.items(): config_file.write(" {}: {}\n".format(key, value)) config_file.write("\n") for component in DEFAULT_COMPONENTS: config_file.write("{}:\n\n".format(component)) return config_path except IOError: _LOGGER.exception('Unable to write default configuration file %s', config_path) return None
def process_ha_core_config(hass, config): """ Processes the [homeassistant] section from the config. """ hac = hass.config def set_time_zone(time_zone_str): """ Helper method to set time zone in HA. """ if time_zone_str is None: return time_zone = date_util.get_time_zone(time_zone_str) if time_zone: hac.time_zone = time_zone date_util.set_default_time_zone(time_zone) else: _LOGGER.error("Received invalid time zone %s", time_zone_str) for key, attr in ((CONF_LATITUDE, "latitude"), (CONF_LONGITUDE, "longitude"), (CONF_NAME, "location_name")): if key in config: setattr(hac, attr, config[key]) set_time_zone(config.get(CONF_TIME_ZONE)) for entity_id, attrs in config.get(CONF_CUSTOMIZE, {}).items(): Entity.overwrite_attribute(entity_id, attrs.keys(), attrs.values()) if CONF_TEMPERATURE_UNIT in config: unit = config[CONF_TEMPERATURE_UNIT] if unit == "C": hac.temperature_unit = TEMP_CELCIUS elif unit == "F": hac.temperature_unit = TEMP_FAHRENHEIT # If we miss some of the needed values, auto detect them if None not in (hac.latitude, hac.longitude, hac.temperature_unit, hac.time_zone): return _LOGGER.info("Auto detecting location and temperature unit") info = util.detect_location_info() if info is None: _LOGGER.error("Could not detect location information") return if hac.latitude is None and hac.longitude is None: hac.latitude = info.latitude hac.longitude = info.longitude if hac.temperature_unit is None: if info.use_fahrenheit: hac.temperature_unit = TEMP_FAHRENHEIT else: hac.temperature_unit = TEMP_CELCIUS if hac.location_name is None: hac.location_name = info.city if hac.time_zone is None: set_time_zone(info.time_zone)
def process_ha_core_config(hass, config): """ Processes the [homeassistant] section from the config. """ hac = hass.config def set_time_zone(time_zone_str): """ Helper method to set time zone in HA. """ if time_zone_str is None: return time_zone = date_util.get_time_zone(time_zone_str) if time_zone: hac.time_zone = time_zone date_util.set_default_time_zone(time_zone) else: _LOGGER.error('Received invalid time zone %s', time_zone_str) for key, attr in ((CONF_LATITUDE, 'latitude'), (CONF_LONGITUDE, 'longitude'), (CONF_NAME, 'location_name')): if key in config: setattr(hac, attr, config[key]) set_time_zone(config.get(CONF_TIME_ZONE)) for entity_id, attrs in config.get(CONF_CUSTOMIZE, {}).items(): Entity.overwrite_attribute(entity_id, attrs.keys(), attrs.values()) if CONF_TEMPERATURE_UNIT in config: unit = config[CONF_TEMPERATURE_UNIT] if unit == 'C': hac.temperature_unit = TEMP_CELCIUS elif unit == 'F': hac.temperature_unit = TEMP_FAHRENHEIT # If we miss some of the needed values, auto detect them if None not in (hac.latitude, hac.longitude, hac.temperature_unit, hac.time_zone): return _LOGGER.info('Auto detecting location and temperature unit') info = util.detect_location_info() if info is None: _LOGGER.error('Could not detect location information') return if hac.latitude is None and hac.longitude is None: hac.latitude = info.latitude hac.longitude = info.longitude if hac.temperature_unit is None: if info.use_fahrenheit: hac.temperature_unit = TEMP_FAHRENHEIT else: hac.temperature_unit = TEMP_CELCIUS if hac.location_name is None: hac.location_name = info.city if hac.time_zone is None: set_time_zone(info.time_zone)