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 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
15 changes: 9 additions & 6 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 @@ -462,17 +465,17 @@ def async_on_node_removed(self, event: dict) -> None:
# Remove trailing comma if it's there
if identifier[-1] == ",":
identifier = identifier[:-1]
notification_msg = f"{notification_msg} {identifier}."
notification_msg = f"{notification_msg} {identifier}"
else:
notification_msg = f"{notification_msg}."
notification_msg = f"{notification_msg}"
async_create(
self.hass,
notification_msg,
f"{notification_msg}, reloading integration.",
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
"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
27 changes: 22 additions & 5 deletions tests/components/zwave_js/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1670,32 +1670,49 @@ 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()

# 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