Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor targets for zwave_js services #115734

Merged
merged 9 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
64 changes: 54 additions & 10 deletions homeassistant/components/zwave_js/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ATTR_AREA_ID,
ATTR_DEVICE_ID,
ATTR_ENTITY_ID,
ATTR_LABEL_ID,
CONF_TYPE,
__version__ as HA_VERSION,
)
Expand Down Expand Up @@ -344,20 +345,59 @@ def async_get_nodes_from_area_id(
}
)
# Add devices in an area that are Z-Wave JS devices
for device in dr.async_entries_for_area(dev_reg, area_id):
if next(
(
config_entry_id
for config_entry_id in device.config_entries
if cast(
nodes.update(
async_get_node_from_device_id(hass, device.id, dev_reg)
for device in dr.async_entries_for_area(dev_reg, area_id)
if any(
cast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is unsafe as it is not handling the case where the entry_id is not present. In order to write safely this each entry should be checked whether is present in the entries before getting the value, making then the cast unnecessary.

ConfigEntry,
hass.config_entries.async_get_entry(config_entry_id),
).domain
== DOMAIN
for config_entry_id in device.config_entries
)
)

return nodes


@callback
def async_get_nodes_from_label_id(
hass: HomeAssistant,
label_id: str,
ent_reg: er.EntityRegistry | None = None,
dev_reg: dr.DeviceRegistry | None = None,
raman325 marked this conversation as resolved.
Show resolved Hide resolved
) -> set[ZwaveNode]:
"""Get nodes for all Z-Wave JS devices and entities that are in an area."""
nodes: set[ZwaveNode] = set()
if ent_reg is None:
raman325 marked this conversation as resolved.
Show resolved Hide resolved
ent_reg = er.async_get(hass)
if dev_reg is None:
raman325 marked this conversation as resolved.
Show resolved Hide resolved
dev_reg = dr.async_get(hass)
# Add devices for all entities with a label that are Z-Wave JS entities
nodes.update(
{
async_get_node_from_device_id(hass, entity.device_id, dev_reg)
for entity in er.async_entries_for_label(ent_reg, label_id)
if entity.platform == DOMAIN and entity.device_id is not None
}
)

# Add devices with a label that are Z-Wave JS devices
nodes.update(
{
async_get_node_from_device_id(hass, device.id, dev_reg)
for device in dr.async_entries_for_label(dev_reg, label_id)
if any(
cast(
raman325 marked this conversation as resolved.
Show resolved Hide resolved
ConfigEntry,
hass.config_entries.async_get_entry(config_entry_id),
).domain
== DOMAIN
),
None,
):
nodes.add(async_get_node_from_device_id(hass, device.id, dev_reg))
for config_entry_id in device.config_entries
)
}
)

return nodes

Expand Down Expand Up @@ -393,6 +433,10 @@ def async_get_nodes_from_targets(
except ValueError as err:
logger.warning(err.args[0])

# Convert all label IDs to nodes
for label_id in val.get(ATTR_LABEL_ID, []):
nodes.update(async_get_nodes_from_label_id(hass, label_id, ent_reg, dev_reg))

return nodes


Expand Down
91 changes: 33 additions & 58 deletions homeassistant/components/zwave_js/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
async_set_config_parameter,
)

from homeassistant.const import ATTR_AREA_ID, ATTR_DEVICE_ID, ATTR_ENTITY_ID
from homeassistant.const import (
ATTR_AREA_ID,
ATTR_DEVICE_ID,
ATTR_ENTITY_ID,
ATTR_LABEL_ID,
)
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
Expand All @@ -40,6 +45,7 @@
async_get_node_from_device_id,
async_get_node_from_entity_id,
async_get_nodes_from_area_id,
async_get_nodes_from_label_id,
async_get_nodes_from_targets,
get_value_id_from_unique_id,
)
Expand All @@ -48,6 +54,13 @@

T = TypeVar("T", ZwaveNode, Endpoint)

TARGET_VALIDATORS = {
vol.Optional(ATTR_AREA_ID): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_LABEL_ID): vol.All(cv.ensure_list, [cv.string]),
raman325 marked this conversation as resolved.
Show resolved Hide resolved
}


def parameter_name_does_not_need_bitmask(
val: dict[str, int | str | list[str]],
Expand Down Expand Up @@ -261,13 +274,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Optional(const.ATTR_ENDPOINT, default=0): vol.Coerce(int),
vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Any(
vol.Coerce(int), cv.string
Expand All @@ -286,7 +293,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
),
},
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
cv.has_at_most_one_key(
const.ATTR_CONFIG_PARAMETER_BITMASK, const.ATTR_VALUE_SIZE
Expand All @@ -305,13 +312,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Optional(const.ATTR_ENDPOINT, default=0): vol.Coerce(int),
vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Coerce(int),
vol.Required(const.ATTR_CONFIG_VALUE): vol.Any(
Expand All @@ -324,7 +325,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
),
},
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
get_nodes_from_service_data,
has_at_least_one_node,
Expand Down Expand Up @@ -356,13 +357,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Required(const.ATTR_COMMAND_CLASS): vol.Coerce(int),
vol.Required(const.ATTR_PROPERTY): vol.Any(
vol.Coerce(int), str
Expand All @@ -376,7 +371,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
vol.Optional(const.ATTR_OPTIONS): {cv.string: VALUE_SCHEMA},
},
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
get_nodes_from_service_data,
has_at_least_one_node,
Expand All @@ -391,13 +386,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Optional(const.ATTR_BROADCAST, default=False): cv.boolean,
vol.Required(const.ATTR_COMMAND_CLASS): vol.Coerce(int),
vol.Required(const.ATTR_PROPERTY): vol.Any(
Expand All @@ -412,7 +401,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
},
vol.Any(
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
broadcast_command,
),
Expand All @@ -428,17 +417,9 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
self.async_ping,
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
},
TARGET_VALIDATORS,
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
get_nodes_from_service_data,
has_at_least_one_node,
Expand All @@ -453,13 +434,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Required(const.ATTR_COMMAND_CLASS): vol.All(
vol.Coerce(int), vol.Coerce(CommandClass)
),
Expand All @@ -468,7 +443,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
vol.Required(const.ATTR_PARAMETERS): list,
},
cv.has_at_least_one_key(
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID
ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID, ATTR_LABEL_ID
),
get_nodes_from_service_data,
has_at_least_one_node,
Expand All @@ -483,13 +458,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]:
schema=vol.Schema(
vol.All(
{
vol.Optional(ATTR_AREA_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_DEVICE_ID): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
**TARGET_VALIDATORS,
vol.Required(const.ATTR_NOTIFICATION_TYPE): vol.All(
vol.Coerce(int), vol.Coerce(NotificationType)
),
Expand Down Expand Up @@ -817,6 +786,12 @@ async def async_invoke_cc_api(self, service: ServiceCall) -> None:
continue
endpoints.add(node.endpoints[0])

for label_id in service.data.get(ATTR_LABEL_ID, []):
for node in async_get_nodes_from_label_id(
self._hass, label_id, self._ent_reg, self._dev_reg
):
endpoints.add(node.endpoints[0])

for entity_id in service.data.get(ATTR_ENTITY_ID, []):
if (
not (entity_entry := self._ent_reg.async_get(entity_id))
Expand Down
Loading
Loading