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

Remove zwave_js device on device reset #104291

Merged
merged 5 commits into from
Dec 9, 2023
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
9 changes: 6 additions & 3 deletions homeassistant/components/zwave_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ def async_on_node_removed(self, event: dict) -> None:
"remove_entity"
),
)
elif reason == RemoveNodeReason.RESET:
# We don't want to remove the device so we can keep the user customizations
return

if reason == RemoveNodeReason.RESET:
device_name = device.name_by_user or device.name or f"Node {node.node_id}"
identifier = get_network_identifier_for_notification(
self.hass, self.config_entry, self.driver_events.driver.controller
Expand All @@ -471,8 +474,8 @@ def async_on_node_removed(self, event: dict) -> None:
"Device Was Factory Reset!",
f"{DOMAIN}.node_reset_and_removed.{dev_id[1]}",
)
else:
self.remove_device(device)

self.remove_device(device)

@callback
def async_on_identify(self, event: dict) -> None:
Expand Down
29 changes: 24 additions & 5 deletions tests/components/zwave_js/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,7 @@ async def test_factory_reset_node(
hass: HomeAssistant, client, multisensor_6, multisensor_6_state, integration
) -> None:
"""Test when a node is removed because it was reset."""
dev_reg = dr.async_get(hass)
# One config entry scenario
remove_event = Event(
type="node removed",
Expand All @@ -1670,32 +1671,50 @@ async def test_factory_reset_node(
assert notifications[msg_id]["message"].startswith("`Multisensor 6`")
assert "with the home ID" not in notifications[msg_id]["message"]
async_dismiss(hass, msg_id)
await hass.async_block_till_done()
assert not dev_reg.async_get_device(identifiers={dev_id})

# Add mock config entry to simulate having multiple entries
new_entry = MockConfigEntry(domain=DOMAIN)
new_entry.add_to_hass(hass)

# Re-add the node then remove it again
client.driver.controller.nodes[multisensor_6_state["nodeId"]] = Node(
client, deepcopy(multisensor_6_state)
add_event = Event(
type="node added",
data={
"source": "controller",
"event": "node added",
"node": deepcopy(multisensor_6_state),
"result": {},
},
)
client.driver.controller.receive_event(add_event)
await hass.async_block_till_done()
remove_event.data["node"] = deepcopy(multisensor_6_state)
client.driver.controller.receive_event(remove_event)
# Test case where config entry title and home ID don't match
notifications = async_get_persistent_notifications(hass)
assert len(notifications) == 1
assert list(notifications)[0] == msg_id
assert (
"network `Mock Title`, with the home ID `3245146787`."
"network `Mock Title`, with the home ID `3245146787`"
in notifications[msg_id]["message"]
)
async_dismiss(hass, msg_id)

# Test case where config entry title and home ID do match
hass.config_entries.async_update_entry(integration, title="3245146787")
client.driver.controller.nodes[multisensor_6_state["nodeId"]] = Node(
client, deepcopy(multisensor_6_state)
add_event = Event(
type="node added",
data={
"source": "controller",
"event": "node added",
"node": deepcopy(multisensor_6_state),
"result": {},
},
)
client.driver.controller.receive_event(add_event)
await hass.async_block_till_done()
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
remove_event.data["node"] = deepcopy(multisensor_6_state)
client.driver.controller.receive_event(remove_event)
notifications = async_get_persistent_notifications(hass)
Expand Down
Loading