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
bbbc9f646f
commit
44e7243e25
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(
|
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]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Get entities for a device."""
|
"""Get entities for a device."""
|
||||||
entity_entries = er.async_entries_for_device(
|
entity_entries = er.async_entries_for_device(
|
||||||
|
@ -73,6 +73,10 @@ def get_device_entities(
|
||||||
)
|
)
|
||||||
entities = []
|
entities = []
|
||||||
for entry in entity_entries:
|
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 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:
|
if (value_id := get_value_id_from_unique_id(entry.unique_id)) is None:
|
||||||
continue
|
continue
|
||||||
|
@ -142,7 +146,7 @@ async def async_get_device_diagnostics(
|
||||||
if node_id is None or node_id not in driver.controller.nodes:
|
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")
|
raise ValueError(f"Node for device {device.id} can't be found")
|
||||||
node = driver.controller.nodes[node_id]
|
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
|
assert client.version
|
||||||
node_state = redact_node_state(async_redact_data(node.data, KEYS_TO_REDACT))
|
node_state = redact_node_state(async_redact_data(node.data, KEYS_TO_REDACT))
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -18,11 +18,11 @@ from homeassistant.components.zwave_js.helpers import (
|
||||||
get_value_id_from_unique_id,
|
get_value_id_from_unique_id,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import async_get as async_get_dev_reg
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.entity_registry import async_get as async_get_ent_reg
|
|
||||||
|
|
||||||
from .common import PROPERTY_ULTRAVIOLET
|
from .common import PROPERTY_ULTRAVIOLET
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
from tests.components.diagnostics import (
|
from tests.components.diagnostics import (
|
||||||
get_diagnostics_for_config_entry,
|
get_diagnostics_for_config_entry,
|
||||||
get_diagnostics_for_device,
|
get_diagnostics_for_device,
|
||||||
|
@ -57,10 +57,26 @@ async def test_device_diagnostics(
|
||||||
version_state,
|
version_state,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the device level diagnostics data dump."""
|
"""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)})
|
device = dev_reg.async_get_device({get_device_id(client.driver, multisensor_6)})
|
||||||
assert device
|
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
|
# Update a value and ensure it is reflected in the node state
|
||||||
event = Event(
|
event = Event(
|
||||||
type="value updated",
|
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
|
# 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
|
# 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(
|
assert len(diagnostics_data["entities"]) == len(
|
||||||
list(async_discover_node_values(multisensor_6, device, {device.id: set()}))
|
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
|
assert diagnostics_data["state"] == multisensor_6.data
|
||||||
|
|
||||||
|
|
||||||
async def test_device_diagnostics_error(hass: HomeAssistant, integration) -> None:
|
async def test_device_diagnostics_error(hass: HomeAssistant, integration) -> None:
|
||||||
"""Test the device diagnostics raises exception when an invalid device is used."""
|
"""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(
|
device = dev_reg.async_get_or_create(
|
||||||
config_entry_id=integration.entry_id, identifiers={("test", "test")}
|
config_entry_id=integration.entry_id, identifiers={("test", "test")}
|
||||||
)
|
)
|
||||||
|
@ -123,12 +150,12 @@ async def test_device_diagnostics_missing_primary_value(
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that device diagnostics handles an entity with a missing primary value."""
|
"""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)})
|
device = dev_reg.async_get_device({get_device_id(client.driver, multisensor_6)})
|
||||||
assert device
|
assert device
|
||||||
|
|
||||||
entity_id = "sensor.multisensor_6_air_temperature"
|
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)
|
entry = ent_reg.async_get(entity_id)
|
||||||
|
|
||||||
# check that the primary value for the entity exists in the diagnostics
|
# 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.nodes[node.node_id] = node
|
||||||
client.driver.controller.emit("node added", {"node": node})
|
client.driver.controller.emit("node added", {"node": node})
|
||||||
await hass.async_block_till_done()
|
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)})
|
device = dev_reg.async_get_device({get_device_id(client.driver, node)})
|
||||||
assert device
|
assert device
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue