Home Assistant Unofficial Reference 2024.12.1
device_automation_helpers.py
Go to the documentation of this file.
1 """Provides helpers for Z-Wave JS device automations."""
2 
3 from __future__ import annotations
4 
5 from zwave_js_server.client import Client as ZwaveClient
6 from zwave_js_server.model.value import ConfigurationValue
7 
8 from homeassistant.config_entries import ConfigEntryState
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr
11 
12 from .const import DATA_CLIENT, DOMAIN
13 
14 NODE_STATUSES = ["asleep", "awake", "dead", "alive"]
15 
16 CONF_SUBTYPE = "subtype"
17 CONF_VALUE_ID = "value_id"
18 
19 VALUE_ID_REGEX = r"([0-9]+-[0-9]+-[0-9]+-).+"
20 
21 
22 def generate_config_parameter_subtype(config_value: ConfigurationValue) -> str:
23  """Generate the config parameter name used in a device automation subtype."""
24  parameter = str(config_value.property_)
25  if config_value.property_key:
26  # Property keys for config values are always an int
27  assert isinstance(config_value.property_key, int)
28  parameter = (
29  f"{parameter}[{hex(config_value.property_key)}] on endpoint "
30  f"{config_value.endpoint}"
31  )
32 
33  return (
34  f"{parameter} ({config_value.property_name}) on endpoint "
35  f"{config_value.endpoint}"
36  )
37 
38 
39 @callback
40 def async_bypass_dynamic_config_validation(hass: HomeAssistant, device_id: str) -> bool:
41  """Return whether device's config entries are not loaded."""
42  dev_reg = dr.async_get(hass)
43  if (device := dev_reg.async_get(device_id)) is None:
44  raise ValueError(f"Device {device_id} not found")
45  entry = next(
46  (
47  config_entry
48  for config_entry in hass.config_entries.async_entries(DOMAIN)
49  if config_entry.entry_id in device.config_entries
50  and config_entry.state == ConfigEntryState.LOADED
51  ),
52  None,
53  )
54  if not entry:
55  return True
56 
57  # The driver may not be ready when the config entry is loaded.
58  client: ZwaveClient = entry.runtime_data[DATA_CLIENT]
59  return client.driver is None
bool async_bypass_dynamic_config_validation(HomeAssistant hass, str device_id)