mirror of
https://github.com/home-assistant/core.git
synced 2025-04-22 16:27:56 +00:00
Better approach writing default configuration
This commit is contained in:
parent
17af1a68e8
commit
a557cf388b
@ -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()
|
||||
config_file.write("homeassistant:\n")
|
||||
|
||||
if location_info:
|
||||
temp_unit = 'F' if location_info.use_fahrenheit else 'C'
|
||||
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]))
|
||||
|
||||
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")
|
||||
config_file.write("\n")
|
||||
|
||||
for component in DEFAULT_COMPONENTS:
|
||||
config_file.write("{}:\n\n".format(component))
|
||||
|
Loading…
x
Reference in New Issue
Block a user