Remove zwave_js device on device reset (#104291)

* Reload zwave_js config entry on device reset

* remove device

* Just remove the device and don't reload

* revert change to notification message

* Assert device is no longer there
This commit is contained in:
Raman Gupta 2023-12-09 17:33:31 -05:00 committed by GitHub
parent 885410bcfc
commit 4e1677e3f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View File

@ -449,7 +449,10 @@ class ControllerEvents:
"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
@ -471,7 +474,7 @@ class ControllerEvents:
"Device Was Factory Reset!",
f"{DOMAIN}.node_reset_and_removed.{dev_id[1]}",
)
else:
self.remove_device(device)
@callback

View File

@ -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",
@ -1670,15 +1671,25 @@ 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
@ -1686,16 +1697,24 @@ async def test_factory_reset_node(
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()
remove_event.data["node"] = deepcopy(multisensor_6_state)
client.driver.controller.receive_event(remove_event)
notifications = async_get_persistent_notifications(hass)