From 4821858afb751b27596b3f9e4d205385876c2eb3 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Tue, 27 Feb 2018 02:09:49 +0200 Subject: [PATCH] Homekit schema gracefully fail with integer (#12725) * Homekit schema gracefully fail with integer * Fix return value * Added test * Fix 2 --- homeassistant/components/homekit/__init__.py | 8 ++++---- tests/components/homekit/test_homekit.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 52b90af7b9b..77c69c14596 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -19,7 +19,7 @@ from homeassistant.util.decorator import Registry TYPES = Registry() _LOGGER = logging.getLogger(__name__) -_RE_VALID_PINCODE = re.compile(r"^(\d{3}-\d{2}-\d{3})$") +_RE_VALID_PINCODE = r"^(\d{3}-\d{2}-\d{3})$" DOMAIN = 'homekit' REQUIREMENTS = ['HAP-python==1.1.7'] @@ -32,10 +32,10 @@ HOMEKIT_FILE = '.homekit.state' def valid_pin(value): """Validate pin code value.""" - match = _RE_VALID_PINCODE.findall(value.strip()) - if match == []: + match = re.match(_RE_VALID_PINCODE, str(value).strip()) + if not match: raise vol.Invalid("Pin must be in the format: '123-45-678'") - return match[0] + return match.group(0) CONFIG_SCHEMA = vol.Schema({ diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index ca6bf8a8510..2ab284e829a 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -73,7 +73,7 @@ class TestHomeKit(unittest.TestCase): """Test async_setup with invalid config option.""" schema = vol.Schema(valid_pin) - for value in ('', '123-456-78', 'a23-45-678', '12345678'): + for value in ('', '123-456-78', 'a23-45-678', '12345678', 1234): with self.assertRaises(vol.MultipleInvalid): schema(value)