Coerce device IDs from known_devices to be slugs (#3764)

* Slugify & consider_home test fix [due to load valid PR]

* undo schema change

* Fix slugify error
This commit is contained in:
Johann Kellerman 2016-10-08 23:40:50 +02:00 committed by Paulus Schoutsen
parent 6419d273ea
commit 4d9bac6f9c
3 changed files with 34 additions and 7 deletions

View file

@ -46,6 +46,7 @@ CONF_TRACK_NEW = 'track_new_devices'
DEFAULT_TRACK_NEW = True DEFAULT_TRACK_NEW = True
CONF_CONSIDER_HOME = 'consider_home' CONF_CONSIDER_HOME = 'consider_home'
DEFAULT_CONSIDER_HOME = 180
CONF_SCAN_INTERVAL = 'interval_seconds' CONF_SCAN_INTERVAL = 'interval_seconds'
DEFAULT_SCAN_INTERVAL = 12 DEFAULT_SCAN_INTERVAL = 12
@ -119,8 +120,9 @@ def setup(hass: HomeAssistantType, config: ConfigType):
return False return False
else: else:
conf = conf[0] if len(conf) > 0 else {} conf = conf[0] if len(conf) > 0 else {}
consider_home = conf[CONF_CONSIDER_HOME] consider_home = conf.get(CONF_CONSIDER_HOME,
track_new = conf[CONF_TRACK_NEW] timedelta(seconds=DEFAULT_CONSIDER_HOME))
track_new = conf.get(CONF_TRACK_NEW, DEFAULT_TRACK_NEW)
devices = load_config(yaml_path, hass, consider_home) devices = load_config(yaml_path, hass, consider_home)
@ -415,7 +417,7 @@ def load_config(path: str, hass: HomeAssistantType, consider_home: timedelta):
for dev_id, device in devices.items(): for dev_id, device in devices.items():
try: try:
device = dev_schema(device) device = dev_schema(device)
device['dev_id'] = cv.slug(dev_id) device['dev_id'] = cv.slugify(dev_id)
except vol.Invalid as exp: except vol.Invalid as exp:
log_exception(exp, dev_id, devices) log_exception(exp, dev_id, devices)
else: else:

View file

@ -17,7 +17,7 @@ from homeassistant.const import (
from homeassistant.core import valid_entity_id from homeassistant.core import valid_entity_id
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.util import slugify from homeassistant.util import slugify as util_slugify
from homeassistant.helpers import template as template_helper from homeassistant.helpers import template as template_helper
# pylint: disable=invalid-name # pylint: disable=invalid-name
@ -218,12 +218,22 @@ def slug(value):
if value is None: if value is None:
raise vol.Invalid('Slug should not be None') raise vol.Invalid('Slug should not be None')
value = str(value) value = str(value)
slg = slugify(value) slg = util_slugify(value)
if value == slg: if value == slg:
return value return value
raise vol.Invalid('invalid slug {} (try {})'.format(value, slg)) raise vol.Invalid('invalid slug {} (try {})'.format(value, slg))
def slugify(value):
"""Coerce a value to a slug."""
if value is None:
raise vol.Invalid('Slug should not be None')
slg = util_slugify(str(value))
if len(slg) > 0:
return slg
raise vol.Invalid('Unable to slugify {}'.format(value))
def string(value: Any) -> str: def string(value: Any) -> str:
"""Coerce value to string, except for None.""" """Coerce value to string, except for None."""
if value is not None: if value is not None:

View file

@ -60,12 +60,27 @@ class TestComponentsDeviceTracker(unittest.TestCase):
"""Test when known devices contains invalid data.""" """Test when known devices contains invalid data."""
files = {'empty.yaml': '', files = {'empty.yaml': '',
'nodict.yaml': '100', 'nodict.yaml': '100',
'allok.yaml': 'my_device:\n name: Device'} 'badkey.yaml': '@:\n name: Device',
'noname.yaml': 'my_device:\n',
'allok.yaml': 'My Device:\n name: Device',
'oneok.yaml': ('My Device!:\n name: Device\n'
'bad_device:\n nme: Device')}
args = {'hass': self.hass, 'consider_home': timedelta(seconds=60)} args = {'hass': self.hass, 'consider_home': timedelta(seconds=60)}
with patch_yaml_files(files): with patch_yaml_files(files):
assert device_tracker.load_config('empty.yaml', **args) == [] assert device_tracker.load_config('empty.yaml', **args) == []
assert device_tracker.load_config('nodict.yaml', **args) == [] assert device_tracker.load_config('nodict.yaml', **args) == []
assert len(device_tracker.load_config('allok.yaml', **args)) == 1 assert device_tracker.load_config('noname.yaml', **args) == []
assert device_tracker.load_config('badkey.yaml', **args) == []
res = device_tracker.load_config('allok.yaml', **args)
assert len(res) == 1
assert res[0].name == 'Device'
assert res[0].dev_id == 'my_device'
res = device_tracker.load_config('oneok.yaml', **args)
assert len(res) == 1
assert res[0].name == 'Device'
assert res[0].dev_id == 'my_device'
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."""