From 087bffeaae8738692d1c0f4ed2d09f484193bc26 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 6 Nov 2018 05:00:48 -0500 Subject: [PATCH] Add workaround to use notification state for zwave lock state (#17386) * Add workaround to use notification state for zwave lock state There are several zwave lock models out there which do not seem to update the lock state on non-rf events (see #11934 #14632 #14534 for examples) including kwikset smartkey zwave plus locks (which I own). In these cases it seems that the notifications for non-rf events the access_control value is updated but not the primary value for the lock state, which is what is used to set the is_locked property. To properly have the lock state accurate for all types of notifications on these models we need to use the access_control field. This commit adds a workaround for the 4 models reported to exhibit this behavior so that home-assistant will reliably set the lock state for all device notifications. * Add YRD220 as per adrum to workaround list * Inline constants --- homeassistant/components/lock/zwave.py | 24 ++++++++++++++++++++++-- tests/components/lock/test_zwave.py | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/lock/zwave.py b/homeassistant/components/lock/zwave.py index 4a2b71f0b48..2ea8300fb9a 100644 --- a/homeassistant/components/lock/zwave.py +++ b/homeassistant/components/lock/zwave.py @@ -28,9 +28,22 @@ POLYCONTROL = 0x10E DANALOCK_V2_BTZE = 0x2 POLYCONTROL_DANALOCK_V2_BTZE_LOCK = (POLYCONTROL, DANALOCK_V2_BTZE) WORKAROUND_V2BTZE = 'v2btze' +WORKAROUND_DEVICE_STATE = 'state' DEVICE_MAPPINGS = { - POLYCONTROL_DANALOCK_V2_BTZE_LOCK: WORKAROUND_V2BTZE + POLYCONTROL_DANALOCK_V2_BTZE_LOCK: WORKAROUND_V2BTZE, + # Kwikset 914TRL ZW500 + (0x0090, 0x440): WORKAROUND_DEVICE_STATE, + # Yale YRD210 + (0x0129, 0x0209): WORKAROUND_DEVICE_STATE, + (0x0129, 0xAA00): WORKAROUND_DEVICE_STATE, + (0x0129, 0x0000): WORKAROUND_DEVICE_STATE, + # Yale YRD220 (as reported by adrum in PR #17386) + (0x0109, 0x0000): WORKAROUND_DEVICE_STATE, + # Schlage BE469 + (0x003B, 0x5044): WORKAROUND_DEVICE_STATE, + # Schlage FE599NX + (0x003B, 0x504C): WORKAROUND_DEVICE_STATE, } LOCK_NOTIFICATION = { @@ -204,6 +217,7 @@ class ZwaveLock(zwave.ZWaveDeviceEntity, LockDevice): self._notification = None self._lock_status = None self._v2btze = None + self._state_workaround = False # Enable appropriate workaround flags for our device # Make sure that we have values for the key before converting to int @@ -216,6 +230,11 @@ class ZwaveLock(zwave.ZWaveDeviceEntity, LockDevice): self._v2btze = 1 _LOGGER.debug("Polycontrol Danalock v2 BTZE " "workaround enabled") + if DEVICE_MAPPINGS[specific_sensor_key] == \ + WORKAROUND_DEVICE_STATE: + self._state_workaround = True + _LOGGER.debug( + "Notification device state workaround enabled") self.update_properties() def update_properties(self): @@ -225,7 +244,8 @@ class ZwaveLock(zwave.ZWaveDeviceEntity, LockDevice): if self.values.access_control: notification_data = self.values.access_control.data self._notification = LOCK_NOTIFICATION.get(str(notification_data)) - + if self._state_workaround: + self._state = LOCK_STATUS.get(str(notification_data)) if self._v2btze: if self.values.v2btze_advanced and \ self.values.v2btze_advanced.data == CONFIG_ADVANCED: diff --git a/tests/components/lock/test_zwave.py b/tests/components/lock/test_zwave.py index 83aec7f0ce9..89ce034d445 100644 --- a/tests/components/lock/test_zwave.py +++ b/tests/components/lock/test_zwave.py @@ -63,6 +63,22 @@ def test_lock_value_changed(mock_openzwave): assert device.is_locked +def test_lock_value_changed_workaround(mock_openzwave): + """Test value changed for Z-Wave lock using notification state.""" + node = MockNode(manufacturer_id='0090', product_id='0440') + values = MockEntityValues( + primary=MockValue(data=True, node=node), + access_control=MockValue(data=1, node=node), + alarm_type=None, + alarm_level=None, + ) + device = zwave.get_device(node=node, values=values) + assert device.is_locked + values.access_control.data = 2 + value_changed(values.access_control) + assert not device.is_locked + + def test_v2btze_value_changed(mock_openzwave): """Test value changed for v2btze Z-Wave lock.""" node = MockNode(manufacturer_id='010e', product_id='0002')