Fix zwave_js device automation bug (#71715)

This commit is contained in:
Raman Gupta 2022-05-11 22:55:12 -04:00 committed by Paulus Schoutsen
parent 1f53786e1b
commit a19a88db63
3 changed files with 30 additions and 6 deletions

View File

@ -8,6 +8,12 @@ from zwave_js_server.const import ConfigurationValueType
from zwave_js_server.model.node import Node
from zwave_js_server.model.value import ConfigurationValue
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from .const import DOMAIN
NODE_STATUSES = ["asleep", "awake", "dead", "alive"]
CONF_SUBTYPE = "subtype"
@ -41,3 +47,21 @@ def generate_config_parameter_subtype(config_value: ConfigurationValue) -> str:
parameter = f"{parameter}[{hex(config_value.property_key)}]"
return f"{parameter} ({config_value.property_name})"
@callback
def async_bypass_dynamic_config_validation(hass: HomeAssistant, device_id: str) -> bool:
"""Return whether device's config entries are not loaded."""
dev_reg = dr.async_get(hass)
if (device := dev_reg.async_get(device_id)) is None:
raise ValueError(f"Device {device_id} not found")
entry = next(
(
config_entry
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.entry_id in device.config_entries
and config_entry.state == ConfigEntryState.LOADED
),
None,
)
return not entry

View File

@ -29,12 +29,12 @@ from .device_automation_helpers import (
CONF_SUBTYPE,
CONF_VALUE_ID,
NODE_STATUSES,
async_bypass_dynamic_config_validation,
generate_config_parameter_subtype,
get_config_parameter_value_schema,
)
from .helpers import (
async_get_node_from_device_id,
async_is_device_config_entry_not_loaded,
check_type_schema_map,
get_zwave_value_from_config,
remove_keys_with_empty_values,
@ -101,7 +101,7 @@ 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
try:
device_config_entry_not_loaded = async_is_device_config_entry_not_loaded(
bypass_dynamic_config_validation = async_bypass_dynamic_config_validation(
hass, config[CONF_DEVICE_ID]
)
except ValueError as err:
@ -109,7 +109,7 @@ async def async_validate_condition_config(
f"Device {config[CONF_DEVICE_ID]} not found"
) from err
if device_config_entry_not_loaded:
if bypass_dynamic_config_validation:
return config
if config[CONF_TYPE] == VALUE_TYPE:

View File

@ -53,12 +53,12 @@ from .const import (
from .device_automation_helpers import (
CONF_SUBTYPE,
NODE_STATUSES,
async_bypass_dynamic_config_validation,
generate_config_parameter_subtype,
)
from .helpers import (
async_get_node_from_device_id,
async_get_node_status_sensor_entity_id,
async_is_device_config_entry_not_loaded,
check_type_schema_map,
copy_available_params,
get_value_state_schema,
@ -215,7 +215,7 @@ 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
try:
device_config_entry_not_loaded = async_is_device_config_entry_not_loaded(
bypass_dynamic_config_validation = async_bypass_dynamic_config_validation(
hass, config[CONF_DEVICE_ID]
)
except ValueError as err:
@ -223,7 +223,7 @@ async def async_validate_trigger_config(
f"Device {config[CONF_DEVICE_ID]} not found"
) from err
if device_config_entry_not_loaded:
if bypass_dynamic_config_validation:
return config
trigger_type = config[CONF_TYPE]