Skip to content

Commit

Permalink
Handle disconnects in zwave_js repair flow (home-assistant#99964)
Browse files Browse the repository at this point in the history
* Handle disconnects in zwave_js repair flow

* Combine logic to reduce LoC

* only check once
  • Loading branch information
raman325 committed Sep 12, 2023
1 parent e0e05f9 commit e231da4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
24 changes: 13 additions & 11 deletions homeassistant/components/zwave_js/repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import voluptuous as vol
from zwave_js_server.model.node import Node

from homeassistant import data_entry_flow
from homeassistant.components.repairs import ConfirmRepairFlow, RepairsFlow
Expand All @@ -14,10 +13,10 @@
class DeviceConfigFileChangedFlow(RepairsFlow):
"""Handler for an issue fixing flow."""

def __init__(self, node: Node, device_name: str) -> None:
def __init__(self, data: dict[str, str]) -> None:
"""Initialize."""
self.node = node
self.device_name = device_name
self.device_name: str = data["device_name"]
self.device_id: str = data["device_id"]

async def async_step_init(
self, user_input: dict[str, str] | None = None
Expand All @@ -30,7 +29,14 @@ async def async_step_confirm(
) -> data_entry_flow.FlowResult:
"""Handle the confirm step of a fix flow."""
if user_input is not None:
self.hass.async_create_task(self.node.async_refresh_info())
try:
node = async_get_node_from_device_id(self.hass, self.device_id)
except ValueError:
return self.async_abort(
reason="cannot_connect",
description_placeholders={"device_name": self.device_name},
)
self.hass.async_create_task(node.async_refresh_info())
return self.async_create_entry(title="", data={})

return self.async_show_form(
Expand All @@ -41,15 +47,11 @@ async def async_step_confirm(


async def async_create_fix_flow(
hass: HomeAssistant,
issue_id: str,
data: dict[str, str] | None,
hass: HomeAssistant, issue_id: str, data: dict[str, str] | None
) -> RepairsFlow:
"""Create flow."""

if issue_id.split(".")[0] == "device_config_file_changed":
assert data
return DeviceConfigFileChangedFlow(
async_get_node_from_device_id(hass, data["device_id"]), data["device_name"]
)
return DeviceConfigFileChangedFlow(data)
return ConfirmRepairFlow()
3 changes: 3 additions & 0 deletions homeassistant/components/zwave_js/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
"title": "Z-Wave device configuration file changed: {device_name}",
"description": "Z-Wave JS discovers a lot of device metadata by interviewing the device. However, some of the information has to be loaded from a configuration file. Some of this information is only evaluated once, during the device interview.\n\nWhen a device config file is updated, this information may be stale and and the device must be re-interviewed to pick up the changes.\n\n This is not a required operation and device functionality will be impacted during the re-interview process, but you may see improvements for your device once it is complete.\n\nIf you'd like to proceed, click on SUBMIT below. The re-interview will take place in the background."
}
},
"abort": {
"cannot_connect": "Cannot connect to {device_name}. Please try again later after confirming that your Z-Wave network is up and connected to Home Assistant."
}
}
}
Expand Down
74 changes: 64 additions & 10 deletions tests/components/zwave_js/test_repairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@
from tests.typing import ClientSessionGenerator, WebSocketGenerator


async def test_device_config_file_changed(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
client,
multisensor_6_state,
integration,
) -> None:
"""Test the device_config_file_changed issue."""
dev_reg = dr.async_get(hass)
async def _trigger_repair_issue(
hass: HomeAssistant, client, multisensor_6_state
) -> Node:
"""Trigger repair issue."""
# Create a node
node_state = deepcopy(multisensor_6_state)
node = Node(client, node_state)
Expand All @@ -53,6 +47,23 @@ async def test_device_config_file_changed(

client.async_send_command_no_wait.reset_mock()

return node


async def test_device_config_file_changed(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
client,
multisensor_6_state,
integration,
) -> None:
"""Test the device_config_file_changed issue."""
dev_reg = dr.async_get(hass)
node = await _trigger_repair_issue(hass, client, multisensor_6_state)

client.async_send_command_no_wait.reset_mock()

device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
assert device
issue_id = f"device_config_file_changed.{device.id}"
Expand Down Expand Up @@ -157,3 +168,46 @@ async def test_invalid_issue(
msg = await ws_client.receive_json()
assert msg["success"]
assert len(msg["result"]["issues"]) == 0


async def test_abort_confirm(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
client,
multisensor_6_state,
integration,
) -> None:
"""Test aborting device_config_file_changed issue in confirm step."""
dev_reg = dr.async_get(hass)
node = await _trigger_repair_issue(hass, client, multisensor_6_state)

device = dev_reg.async_get_device(identifiers={get_device_id(client.driver, node)})
assert device
issue_id = f"device_config_file_changed.{device.id}"

await async_process_repairs_platforms(hass)
await hass_ws_client(hass)
http_client = await hass_client()

url = RepairsFlowIndexView.url
resp = await http_client.post(url, json={"handler": DOMAIN, "issue_id": issue_id})
assert resp.status == HTTPStatus.OK
data = await resp.json()

flow_id = data["flow_id"]
assert data["step_id"] == "confirm"

# Unload config entry so we can't connect to the node
await hass.config_entries.async_unload(integration.entry_id)

# Apply fix
url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await http_client.post(url)

assert resp.status == HTTPStatus.OK
data = await resp.json()

assert data["type"] == "abort"
assert data["reason"] == "cannot_connect"
assert data["description_placeholders"] == {"device_name": device.name}

0 comments on commit e231da4

Please sign in to comment.