Merge pull request #41 from theolind/feature_yaml_config
Support for YAML configuration
This commit is contained in:
commit
3ebfc0e917
6 files changed
+138
-116
No files matched your search
@@ -72,15 +72,19 @@ def ensure_config_path(config_dir):
|
||||
'directory {} ').format(config_dir))
|
||||
sys.exit()
|
||||
|
||||
config_path = os.path.join(config_dir, 'home-assistant.conf')
|
||||
# Try to use yaml configuration first
|
||||
config_path = os.path.join(config_dir, 'configuration.yaml')
|
||||
if not os.path.isfile(config_path):
|
||||
config_path = os.path.join(config_dir, 'home-assistant.conf')
|
||||
|
||||
# Ensure a config file exists to make first time usage easier
|
||||
if not os.path.isfile(config_path):
|
||||
config_path = os.path.join(config_dir, 'configuration.yaml')
|
||||
try:
|
||||
with open(config_path, 'w') as conf:
|
||||
conf.write("[frontend]\n\n")
|
||||
conf.write("[discovery]\n\n")
|
||||
conf.write("[history]\n\n")
|
||||
conf.write("frontend:\n\n")
|
||||
conf.write("discovery:\n\n")
|
||||
conf.write("history:\n\n")
|
||||
except IOError:
|
||||
print(('Fatal Error: No configuration file found and unable '
|
||||
'to write a default one to {}').format(config_path))
|
||||
|
||||
@@ -11,6 +11,8 @@ start by calling homeassistant.start_home_assistant(bus)
|
||||
|
||||
import os
|
||||
import configparser
|
||||
import yaml
|
||||
import io
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
|
||||
@@ -77,7 +79,9 @@ def from_config_dict(config, hass=None):
|
||||
|
||||
# Make a copy because we are mutating it.
|
||||
# Convert it to defaultdict so components can always have config dict
|
||||
config = defaultdict(dict, config)
|
||||
# Convert values to dictionaries if they are None
|
||||
config = defaultdict(
|
||||
dict, {key: value or {} for key, value in config.items()})
|
||||
|
||||
# Filter out the repeating and common config section [homeassistant]
|
||||
components = (key for key in config.keys()
|
||||
@@ -110,17 +114,21 @@ def from_config_file(config_path, hass=None):
|
||||
# Set config dir to directory holding config file
|
||||
hass.config_dir = os.path.abspath(os.path.dirname(config_path))
|
||||
|
||||
# Read config
|
||||
config = configparser.ConfigParser()
|
||||
config.read(config_path)
|
||||
|
||||
config_dict = {}
|
||||
# check config file type
|
||||
if os.path.splitext(config_path)[1] == '.yaml':
|
||||
# Read yaml
|
||||
config_dict = yaml.load(io.open(config_path, 'r'))
|
||||
else:
|
||||
# Read config
|
||||
config = configparser.ConfigParser()
|
||||
config.read(config_path)
|
||||
|
||||
for section in config.sections():
|
||||
config_dict[section] = {}
|
||||
for section in config.sections():
|
||||
config_dict[section] = {}
|
||||
|
||||
for key, val in config.items(section):
|
||||
config_dict[section][key] = val
|
||||
for key, val in config.items(section):
|
||||
config_dict[section][key] = val
|
||||
|
||||
return from_config_dict(config_dict, hass)
|
||||
|
||||
|
||||
@@ -36,6 +36,9 @@ TIME_DEVICE_NOT_FOUND = timedelta(minutes=3)
|
||||
# Filename to save known devices to
|
||||
KNOWN_DEVICES_FILE = "known_devices.csv"
|
||||
|
||||
CONF_SECONDS = "interval_seconds"
|
||||
|
||||
DEFAULT_CONF_SECONDS = 12
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -81,7 +84,10 @@ def setup(hass, config):
|
||||
|
||||
return False
|
||||
|
||||
tracker = DeviceTracker(hass, device_scanner)
|
||||
seconds = util.convert(config[DOMAIN].get(CONF_SECONDS), int,
|
||||
DEFAULT_CONF_SECONDS)
|
||||
|
||||
tracker = DeviceTracker(hass, device_scanner, seconds)
|
||||
|
||||
# We only succeeded if we got to parse the known devices file
|
||||
return not tracker.invalid_known_devices_file
|
||||
@@ -90,7 +96,7 @@ def setup(hass, config):
|
||||
class DeviceTracker(object):
|
||||
""" Class that tracks which devices are home and which are not. """
|
||||
|
||||
def __init__(self, hass, device_scanner):
|
||||
def __init__(self, hass, device_scanner, seconds):
|
||||
self.hass = hass
|
||||
|
||||
self.device_scanner = device_scanner
|
||||
@@ -126,8 +132,10 @@ class DeviceTracker(object):
|
||||
if self.invalid_known_devices_file:
|
||||
return
|
||||
|
||||
hass.track_time_change(
|
||||
update_device_state, second=range(0, 60, 12))
|
||||
seconds = range(0, 60, seconds)
|
||||
|
||||
_LOGGER.info("Device tracker interval second=%s", seconds)
|
||||
hass.track_time_change(update_device_state, second=seconds)
|
||||
|
||||
hass.services.register(DOMAIN,
|
||||
SERVICE_DEVICE_TRACKER_RELOAD,
|
||||
|
||||
Reference in new issue
Block a user