Improve known_device.yaml writing (#3955)

* Better known_device.yaml writing

* yaml dump
This commit is contained in:
Johann Kellerman 2016-10-22 06:41:27 +02:00 committed by Paulus Schoutsen
parent 1d2d338cd0
commit 8d375e2d47

View File

@ -14,6 +14,7 @@ import threading
from typing import Any, Sequence, Callable from typing import Any, Sequence, Callable
import voluptuous as vol import voluptuous as vol
import yaml
from homeassistant.bootstrap import ( from homeassistant.bootstrap import (
prepare_setup_platform, log_exception) prepare_setup_platform, log_exception)
@ -420,7 +421,12 @@ def load_config(path: str, hass: HomeAssistantType, consider_home: timedelta):
}) })
try: try:
result = [] result = []
devices = load_yaml_config_file(path) try:
devices = load_yaml_config_file(path)
except HomeAssistantError as err:
_LOGGER.error('Unable to load %s: %s', path, str(err))
return []
for dev_id, device in devices.items(): for dev_id, device in devices.items():
try: try:
device = dev_schema(device) device = dev_schema(device)
@ -463,14 +469,15 @@ def update_config(path: str, dev_id: str, device: Device):
"""Add device to YAML configuration file.""" """Add device to YAML configuration file."""
with open(path, 'a') as out: with open(path, 'a') as out:
out.write('\n') out.write('\n')
out.write('{}:\n'.format(device.dev_id))
for key, value in (('name', device.name), ('mac', device.mac), device = {device.dev_id: {
('picture', device.config_picture), 'name': device.name,
('track', 'yes' if device.track else 'no'), 'mac': device.mac,
(CONF_AWAY_HIDE, 'picture': device.config_picture,
'yes' if device.away_hide else 'no')): 'track': device.track,
out.write(' {}: {}\n'.format(key, '' if value is None else value)) CONF_AWAY_HIDE: device.away_hide
}}
yaml.dump(device, out, default_flow_style=False)
def get_gravatar_for_email(email: str): def get_gravatar_for_email(email: str):