Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions homeassistant/components/zwave_js/device_automation_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
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"
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions homeassistant/components/zwave_js/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
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,
Expand Down Expand Up @@ -101,15 +101,15 @@ 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:
raise InvalidDeviceAutomationConfig(
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:
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/zwave_js/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
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,
Expand Down Expand Up @@ -215,15 +215,15 @@ 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:
raise InvalidDeviceAutomationConfig(
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]
Expand Down