mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
known devices yaml robustness (#2523)
This commit is contained in:
parent
e38b7d97d2
commit
ef64e11b50
@ -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):
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user