known devices yaml robustness (#2523)

This commit is contained in:
Paulus Schoutsen 2016-07-13 23:56:02 -07:00 committed by GitHub
parent e38b7d97d2
commit ef64e11b50
2 changed files with 23 additions and 6 deletions

View File

@ -377,12 +377,16 @@ def load_config(path, hass, consider_home, home_range):
"""Load devices from YAML configuration file."""
if not os.path.isfile(path):
return []
return [
Device(hass, consider_home, home_range, device.get('track', False),
str(dev_id).lower(), str(device.get('mac')).upper(),
device.get('name'), device.get('picture'),
device.get(CONF_AWAY_HIDE, DEFAULT_AWAY_HIDE))
for dev_id, device in load_yaml_config_file(path).items()]
try:
return [
Device(hass, consider_home, home_range, device.get('track', False),
str(dev_id).lower(), str(device.get('mac')).upper(),
device.get('name'), device.get('picture'),
device.get(CONF_AWAY_HIDE, DEFAULT_AWAY_HIDE))
for dev_id, device in load_yaml_config_file(path).items()]
except HomeAssistantError:
# When YAML file could not be loaded/did not contain a dict
return []
def setup_scanner_platform(hass, config, scanner, see_device):

View File

@ -4,6 +4,7 @@ import unittest
from unittest.mock import patch
from datetime import datetime, timedelta
import os
import tempfile
from homeassistant.loader import get_component
import homeassistant.util.dt as dt_util
@ -45,6 +46,18 @@ class TestComponentsDeviceTracker(unittest.TestCase):
self.assertFalse(device_tracker.is_on(self.hass, entity_id))
def test_reading_broken_yaml_config(self):
"""Test when known devices contains invalid data."""
with tempfile.NamedTemporaryFile() as fp:
# file is empty
assert device_tracker.load_config(fp.name, None, False, 0) == []
fp.write('100'.encode('utf-8'))
fp.flush()
# file contains a non-dict format
assert device_tracker.load_config(fp.name, None, False, 0) == []
def test_reading_yaml_config(self):
"""Test the rendering of the YAML configuration."""
dev_id = 'test'