Handle disconnects in zwave_js repair flow (#99964)

* Handle disconnects in zwave_js repair flow

* Combine logic to reduce LoC

* only check once
This commit is contained in:
Raman Gupta 2023-09-11 22:21:44 -04:00 committed by Paulus Schoutsen
parent 0f9d00e4aa
commit e6c2833032
3 changed files with 80 additions and 21 deletions

View File

@ -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
@ -14,10 +13,10 @@ from .helpers import async_get_node_from_device_id
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
@ -30,7 +29,14 @@ class DeviceConfigFileChangedFlow(RepairsFlow):
) -> 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(
@ -41,15 +47,11 @@ class DeviceConfigFileChangedFlow(RepairsFlow):
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()

View File

@ -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."
}
}
}

View File

@ -22,16 +22,10 @@ import homeassistant.helpers.issue_registry as ir
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)
@ -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}"
@ -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}