known devices yaml robustness (#2523)
This commit is contained in:
parent
e38b7d97d2
commit
ef64e11b50
2 changed files with 23 additions and 6 deletions
|
@ -377,12 +377,16 @@ def load_config(path, hass, consider_home, home_range):
|
||||||
"""Load devices from YAML configuration file."""
|
"""Load devices from YAML configuration file."""
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
return []
|
return []
|
||||||
return [
|
try:
|
||||||
Device(hass, consider_home, home_range, device.get('track', False),
|
return [
|
||||||
str(dev_id).lower(), str(device.get('mac')).upper(),
|
Device(hass, consider_home, home_range, device.get('track', False),
|
||||||
device.get('name'), device.get('picture'),
|
str(dev_id).lower(), str(device.get('mac')).upper(),
|
||||||
device.get(CONF_AWAY_HIDE, DEFAULT_AWAY_HIDE))
|
device.get('name'), device.get('picture'),
|
||||||
for dev_id, device in load_yaml_config_file(path).items()]
|
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):
|
def setup_scanner_platform(hass, config, scanner, see_device):
|
||||||
|
|
|
@ -4,6 +4,7 @@ import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.loader import get_component
|
||||||
import homeassistant.util.dt as dt_util
|
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))
|
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):
|
def test_reading_yaml_config(self):
|
||||||
"""Test the rendering of the YAML configuration."""
|
"""Test the rendering of the YAML configuration."""
|
||||||
dev_id = 'test'
|
dev_id = 'test'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue