Fix zwave_js device diagnostics dump (#94999)
* Fix zwave_js device diagnostics dump * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/zwave_js/test_diagnostics.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * improve test --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
6ceb973606
commit
5265584159
2 changed files with 41 additions and 10 deletions
|
@ -65,7 +65,7 @@ def redact_node_state(node_state: NodeDataType) -> NodeDataType:
|
|||
|
||||
|
||||
def get_device_entities(
|
||||
hass: HomeAssistant, node: Node, device: dr.DeviceEntry
|
||||
hass: HomeAssistant, node: Node, config_entry: ConfigEntry, device: dr.DeviceEntry
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get entities for a device."""
|
||||
entity_entries = er.async_entries_for_device(
|
||||
|
@ -73,6 +73,10 @@ def get_device_entities(
|
|||
)
|
||||
entities = []
|
||||
for entry in entity_entries:
|
||||
# Skip entities that are not part of this integration
|
||||
if entry.config_entry_id != config_entry.entry_id:
|
||||
continue
|
||||
|
||||
# If the value ID returns as None, we don't need to include this entity
|
||||
if (value_id := get_value_id_from_unique_id(entry.unique_id)) is None:
|
||||
continue
|
||||
|
@ -142,7 +146,7 @@ async def async_get_device_diagnostics(
|
|||
if node_id is None or node_id not in driver.controller.nodes:
|
||||
raise ValueError(f"Node for device {device.id} can't be found")
|
||||
node = driver.controller.nodes[node_id]
|
||||
entities = get_device_entities(hass, node, device)
|
||||
entities = get_device_entities(hass, node, config_entry, device)
|
||||
assert client.version
|
||||
node_state = redact_node_state(async_redact_data(node.data, KEYS_TO_REDACT))
|
||||
return {
|
||||
|
|
|
@ -18,11 +18,11 @@ from homeassistant.components.zwave_js.helpers import (
|
|||
get_value_id_from_unique_id,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
|
||||
from homeassistant.helpers.entity_registry import async_get as async_get_ent_reg
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from .common import PROPERTY_ULTRAVIOLET
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import (
|
||||
get_diagnostics_for_config_entry,
|
||||
get_diagnostics_for_device,
|
||||
|
@ -57,10 +57,26 @@ async def test_device_diagnostics(
|
|||
version_state,
|
||||
) -> None:
|
||||
"""Test the device level diagnostics data dump."""
|
||||
dev_reg = async_get_dev_reg(hass)
|
||||
dev_reg = dr.async_get(hass)
|
||||
device = dev_reg.async_get_device({get_device_id(client.driver, multisensor_6)})
|
||||
assert device
|
||||
|
||||
# Create mock config entry for fake entity
|
||||
mock_config_entry = MockConfigEntry(domain="test_integration")
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
# Add an entity entry to the device that is not part of this config entry
|
||||
ent_reg = er.async_get(hass)
|
||||
ent_reg.async_get_or_create(
|
||||
"test",
|
||||
"test_integration",
|
||||
"test_unique_id",
|
||||
suggested_object_id="unrelated_entity",
|
||||
config_entry=mock_config_entry,
|
||||
device_id=device.id,
|
||||
)
|
||||
assert ent_reg.async_get("test.unrelated_entity")
|
||||
|
||||
# Update a value and ensure it is reflected in the node state
|
||||
event = Event(
|
||||
type="value updated",
|
||||
|
@ -92,16 +108,27 @@ async def test_device_diagnostics(
|
|||
}
|
||||
# Assert that we only have the entities that were discovered for this device
|
||||
# Entities that are created outside of discovery (e.g. node status sensor and
|
||||
# ping button) should not be in dump.
|
||||
# ping button) as well as helper entities created from other integrations should
|
||||
# not be in dump.
|
||||
assert len(diagnostics_data["entities"]) == len(
|
||||
list(async_discover_node_values(multisensor_6, device, {device.id: set()}))
|
||||
)
|
||||
assert any(
|
||||
entity.entity_id == "test.unrelated_entity"
|
||||
for entity in er.async_entries_for_device(ent_reg, device.id)
|
||||
)
|
||||
# Explicitly check that the entity that is not part of this config entry is not
|
||||
# in the dump.
|
||||
assert not any(
|
||||
entity["entity_id"] == "test.unrelated_entity"
|
||||
for entity in diagnostics_data["entities"]
|
||||
)
|
||||
assert diagnostics_data["state"] == multisensor_6.data
|
||||
|
||||
|
||||
async def test_device_diagnostics_error(hass: HomeAssistant, integration) -> None:
|
||||
"""Test the device diagnostics raises exception when an invalid device is used."""
|
||||
dev_reg = async_get_dev_reg(hass)
|
||||
dev_reg = dr.async_get(hass)
|
||||
device = dev_reg.async_get_or_create(
|
||||
config_entry_id=integration.entry_id, identifiers={("test", "test")}
|
||||
)
|
||||
|
@ -123,12 +150,12 @@ async def test_device_diagnostics_missing_primary_value(
|
|||
hass_client: ClientSessionGenerator,
|
||||
) -> None:
|
||||
"""Test that device diagnostics handles an entity with a missing primary value."""
|
||||
dev_reg = async_get_dev_reg(hass)
|
||||
dev_reg = dr.async_get(hass)
|
||||
device = dev_reg.async_get_device({get_device_id(client.driver, multisensor_6)})
|
||||
assert device
|
||||
|
||||
entity_id = "sensor.multisensor_6_air_temperature"
|
||||
ent_reg = async_get_ent_reg(hass)
|
||||
ent_reg = er.async_get(hass)
|
||||
entry = ent_reg.async_get(entity_id)
|
||||
|
||||
# check that the primary value for the entity exists in the diagnostics
|
||||
|
@ -212,7 +239,7 @@ async def test_device_diagnostics_secret_value(
|
|||
client.driver.controller.nodes[node.node_id] = node
|
||||
client.driver.controller.emit("node added", {"node": node})
|
||||
await hass.async_block_till_done()
|
||||
dev_reg = async_get_dev_reg(hass)
|
||||
dev_reg = dr.async_get(hass)
|
||||
device = dev_reg.async_get_device({get_device_id(client.driver, node)})
|
||||
assert device
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue