From c477378835d13a0b39d813868e9c6b38cc973b87 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Fri, 4 Feb 2022 02:50:47 +0100 Subject: [PATCH] Raise when zwave_js device automation fails validation (#65610) --- .../components/zwave_js/device_condition.py | 11 ++++++++++- .../components/zwave_js/device_trigger.py | 11 ++++++++++- homeassistant/components/zwave_js/helpers.py | 3 ++- .../zwave_js/test_device_condition.py | 17 +++++++++++++++++ .../components/zwave_js/test_device_trigger.py | 16 ++++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/zwave_js/device_condition.py b/homeassistant/components/zwave_js/device_condition.py index 9840d89dc9d..fcd769dc8a4 100644 --- a/homeassistant/components/zwave_js/device_condition.py +++ b/homeassistant/components/zwave_js/device_condition.py @@ -99,7 +99,16 @@ async def async_validate_condition_config( # We return early if the config entry for this device is not ready because we can't # validate the value without knowing the state of the device - if async_is_device_config_entry_not_loaded(hass, config[CONF_DEVICE_ID]): + try: + device_config_entry_not_loaded = async_is_device_config_entry_not_loaded( + hass, config[CONF_DEVICE_ID] + ) + except ValueError as err: + raise InvalidDeviceAutomationConfig( + f"Device {config[CONF_DEVICE_ID]} not found" + ) from err + + if device_config_entry_not_loaded: return config if config[CONF_TYPE] == VALUE_TYPE: diff --git a/homeassistant/components/zwave_js/device_trigger.py b/homeassistant/components/zwave_js/device_trigger.py index 481fc429cb0..888efbf2bfd 100644 --- a/homeassistant/components/zwave_js/device_trigger.py +++ b/homeassistant/components/zwave_js/device_trigger.py @@ -217,7 +217,16 @@ async def async_validate_trigger_config( # We return early if the config entry for this device is not ready because we can't # validate the value without knowing the state of the device - if async_is_device_config_entry_not_loaded(hass, config[CONF_DEVICE_ID]): + try: + device_config_entry_not_loaded = async_is_device_config_entry_not_loaded( + hass, config[CONF_DEVICE_ID] + ) + except ValueError as err: + raise InvalidDeviceAutomationConfig( + f"Device {config[CONF_DEVICE_ID]} not found" + ) from err + + if device_config_entry_not_loaded: return config trigger_type = config[CONF_TYPE] diff --git a/homeassistant/components/zwave_js/helpers.py b/homeassistant/components/zwave_js/helpers.py index 3f57f4bbe6f..de7ed5da502 100644 --- a/homeassistant/components/zwave_js/helpers.py +++ b/homeassistant/components/zwave_js/helpers.py @@ -298,7 +298,8 @@ def async_is_device_config_entry_not_loaded( """Return whether device's config entries are not loaded.""" dev_reg = dr.async_get(hass) device = dev_reg.async_get(device_id) - assert device + if device is None: + raise ValueError(f"Device {device_id} not found") return any( (entry := hass.config_entries.async_get_entry(entry_id)) and entry.state != ConfigEntryState.LOADED diff --git a/tests/components/zwave_js/test_device_condition.py b/tests/components/zwave_js/test_device_condition.py index 3919edbd340..71a6865287c 100644 --- a/tests/components/zwave_js/test_device_condition.py +++ b/tests/components/zwave_js/test_device_condition.py @@ -596,6 +596,23 @@ async def test_failure_scenarios(hass, client, hank_binary_switch, integration): == INVALID_CONFIG ) + # Test invalid device ID fails validation + with pytest.raises(InvalidDeviceAutomationConfig): + await device_condition.async_validate_condition_config( + hass, + { + "condition": "device", + "domain": DOMAIN, + "type": "value", + "device_id": "invalid_device_id", + "command_class": CommandClass.DOOR_LOCK.value, + "property": 9999, + "property_key": 9999, + "endpoint": 9999, + "value": 9999, + }, + ) + async def test_get_value_from_config_failure( hass, client, hank_binary_switch, integration diff --git a/tests/components/zwave_js/test_device_trigger.py b/tests/components/zwave_js/test_device_trigger.py index 19c86af22ed..bf3738a7fb3 100644 --- a/tests/components/zwave_js/test_device_trigger.py +++ b/tests/components/zwave_js/test_device_trigger.py @@ -1370,3 +1370,19 @@ async def test_failure_scenarios(hass, client, hank_binary_switch, integration): await device_trigger.async_validate_trigger_config(hass, INVALID_CONFIG) == INVALID_CONFIG ) + + # Test invalid device ID fails validation + with pytest.raises(InvalidDeviceAutomationConfig): + await device_trigger.async_validate_trigger_config( + hass, + { + "platform": "device", + "domain": DOMAIN, + "device_id": "invalid_device_id", + "type": "zwave_js.value_updated.value", + "command_class": CommandClass.DOOR_LOCK.value, + "property": 9999, + "property_key": 9999, + "endpoint": 9999, + }, + )