2015-04-26 17:05:01 +00:00
|
|
|
"""
|
|
|
|
homeassistant.config
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Module to help with parsing and generating configuration files.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
from homeassistant.core import HomeAssistantError
|
2015-04-26 17:05:01 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_LATITUDE, CONF_LONGITUDE, CONF_TEMPERATURE_UNIT, CONF_NAME,
|
|
|
|
CONF_TIME_ZONE)
|
2015-07-07 07:01:17 +00:00
|
|
|
import homeassistant.util.location as loc_util
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
YAML_CONFIG_FILE = 'configuration.yaml'
|
2015-06-02 05:50:57 +00:00
|
|
|
|
2015-08-04 20:21:25 +00:00
|
|
|
DEFAULT_CONFIG = (
|
2015-06-02 05:50:57 +00:00
|
|
|
# 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'),
|
2015-08-04 20:21:25 +00:00
|
|
|
)
|
2015-08-27 08:06:07 +00:00
|
|
|
DEFAULT_COMPONENTS = {
|
|
|
|
'introduction': 'Show links to resources in log and frontend',
|
|
|
|
'frontend': 'Enables the frontend',
|
|
|
|
'discovery': 'Discover some devices automatically',
|
|
|
|
'conversation': 'Allows you to issue voice commands from the frontend',
|
|
|
|
'history': 'Enables support for tracking state changes over time.',
|
|
|
|
'logbook': 'View all events in a logbook',
|
|
|
|
'sun': 'Track the sun',
|
|
|
|
}
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure_config_exists(config_dir, detect_location=True):
|
|
|
|
""" Ensures a config file exists in given config dir.
|
|
|
|
Creating a default one if needed.
|
|
|
|
Returns path to the config file. """
|
|
|
|
config_path = find_config_file(config_dir)
|
|
|
|
|
|
|
|
if config_path is None:
|
|
|
|
_LOGGER.info("Unable to find configuration. Creating default one")
|
|
|
|
config_path = create_default_config(config_dir, detect_location)
|
|
|
|
|
|
|
|
return config_path
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-06-02 05:50:57 +00:00
|
|
|
info = {attr: default for attr, default, *_ in DEFAULT_CONFIG}
|
|
|
|
|
2015-07-07 07:01:17 +00:00
|
|
|
location_info = detect_location and loc_util.detect_location_info()
|
2015-06-02 05:50:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-04-26 17:05:01 +00:00
|
|
|
# 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:
|
2015-06-02 05:50:57 +00:00
|
|
|
config_file.write("homeassistant:\n")
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-06-02 05:50:57 +00:00
|
|
|
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]))
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-06-02 05:50:57 +00:00
|
|
|
config_file.write("\n")
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-08-27 08:06:07 +00:00
|
|
|
for component, description in DEFAULT_COMPONENTS.items():
|
|
|
|
config_file.write("# {}\n".format(description))
|
2015-04-26 17:05:01 +00:00
|
|
|
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 find_config_file(config_dir):
|
|
|
|
""" Looks in given directory for supported config files. """
|
2015-08-04 20:21:25 +00:00
|
|
|
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-08-04 20:21:25 +00:00
|
|
|
return config_path if os.path.isfile(config_path) else None
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_config_file(config_path):
|
|
|
|
""" Loads given config file. """
|
2015-08-04 20:21:25 +00:00
|
|
|
return load_yaml_config_file(config_path)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_yaml_config_file(config_path):
|
|
|
|
""" Parse a YAML configuration file. """
|
|
|
|
import yaml
|
|
|
|
|
2015-05-16 16:27:34 +00:00
|
|
|
def parse(fname):
|
2015-08-03 15:05:33 +00:00
|
|
|
""" Parse a YAML file. """
|
2015-05-16 16:27:34 +00:00
|
|
|
try:
|
|
|
|
with open(fname) as conf_file:
|
|
|
|
# If configuration file is empty YAML returns None
|
|
|
|
# We convert that to an empty dict
|
2015-08-03 15:05:33 +00:00
|
|
|
return yaml.load(conf_file) or {}
|
2015-05-16 16:27:34 +00:00
|
|
|
except yaml.YAMLError:
|
2015-08-03 15:05:33 +00:00
|
|
|
error = 'Error reading YAML configuration file {}'.format(fname)
|
|
|
|
_LOGGER.exception(error)
|
|
|
|
raise HomeAssistantError(error)
|
2015-05-16 16:27:34 +00:00
|
|
|
|
2015-05-16 00:59:43 +00:00
|
|
|
def yaml_include(loader, node):
|
|
|
|
"""
|
|
|
|
Loads another YAML file and embeds it using the !include tag.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
device_tracker: !include device_tracker.yaml
|
|
|
|
"""
|
2015-05-16 16:27:34 +00:00
|
|
|
fname = os.path.join(os.path.dirname(loader.name), node.value)
|
|
|
|
return parse(fname)
|
2015-05-16 00:59:43 +00:00
|
|
|
|
|
|
|
yaml.add_constructor('!include', yaml_include)
|
|
|
|
|
2015-05-16 16:27:34 +00:00
|
|
|
conf_dict = parse(config_path)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
if not isinstance(conf_dict, dict):
|
|
|
|
_LOGGER.error(
|
|
|
|
'The configuration file %s does not contain a dictionary',
|
|
|
|
os.path.basename(config_path))
|
|
|
|
raise HomeAssistantError()
|
|
|
|
|
|
|
|
return conf_dict
|