From 51f55bddb7c1633c540b5e89da41cb705a6a3723 Mon Sep 17 00:00:00 2001 From: Matt Schmitt Date: Sat, 21 Apr 2018 10:16:46 -0400 Subject: [PATCH] HomeKit Alarm Control Panel Code Exception Fix (#14025) * Catch exception for KeyError * Use get and added test --- .../components/homekit/type_security_systems.py | 2 +- .../components/homekit/test_type_security_systems.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homekit/type_security_systems.py b/homeassistant/components/homekit/type_security_systems.py index 6b8457a3aa5..0762e0f25f9 100644 --- a/homeassistant/components/homekit/type_security_systems.py +++ b/homeassistant/components/homekit/type_security_systems.py @@ -30,7 +30,7 @@ class SecuritySystem(HomeAccessory): def __init__(self, *args, config): """Initialize a SecuritySystem accessory object.""" super().__init__(*args, category=CATEGORY_ALARM_SYSTEM) - self._alarm_code = config[ATTR_CODE] + self._alarm_code = config.get(ATTR_CODE) self.flag_target_state = False serv_alarm = add_preload_service(self, SERV_SECURITY_SYSTEM) diff --git a/tests/components/homekit/test_type_security_systems.py b/tests/components/homekit/test_type_security_systems.py index ec538ce4b50..9c1ff0faf1a 100644 --- a/tests/components/homekit/test_type_security_systems.py +++ b/tests/components/homekit/test_type_security_systems.py @@ -109,8 +109,16 @@ class TestHomekitSecuritySystems(unittest.TestCase): acc = SecuritySystem(self.hass, 'SecuritySystem', acp, 2, config={ATTR_CODE: None}) - acc.run() - + # Set from HomeKit + acc.char_target_state.client_update_value(0) + self.hass.block_till_done() + self.assertEqual( + self.events[0].data[ATTR_SERVICE], 'alarm_arm_home') + self.assertNotIn(ATTR_CODE, self.events[0].data[ATTR_SERVICE_DATA]) + self.assertEqual(acc.char_target_state.value, 0) + + acc = SecuritySystem(self.hass, 'SecuritySystem', acp, + 2, config={}) # Set from HomeKit acc.char_target_state.client_update_value(0) self.hass.block_till_done()