Skip to content

Commit 7ff90ca

Browse files
authored
Open repair issue when outbound WebSocket is enabled for Shelly non-sleeping RPC device (home-assistant#147901)
1 parent bab9ec9 commit 7ff90ca

File tree

5 files changed

+194
-5
lines changed

5 files changed

+194
-5
lines changed

homeassistant/components/shelly/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
ShellyRpcCoordinator,
5757
ShellyRpcPollingCoordinator,
5858
)
59-
from .repairs import async_manage_ble_scanner_firmware_unsupported_issue
59+
from .repairs import (
60+
async_manage_ble_scanner_firmware_unsupported_issue,
61+
async_manage_outbound_websocket_incorrectly_enabled_issue,
62+
)
6063
from .utils import (
6164
async_create_issue_unsupported_firmware,
6265
get_coap_context,
@@ -327,6 +330,10 @@ async def _async_setup_rpc_entry(hass: HomeAssistant, entry: ShellyConfigEntry)
327330
hass,
328331
entry,
329332
)
333+
async_manage_outbound_websocket_incorrectly_enabled_issue(
334+
hass,
335+
entry,
336+
)
330337
elif (
331338
sleep_period is None
332339
or device_entry is None

homeassistant/components/shelly/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class BLEScannerMode(StrEnum):
237237
FIRMWARE_UNSUPPORTED_ISSUE_ID = "firmware_unsupported_{unique}"
238238

239239
BLE_SCANNER_FIRMWARE_UNSUPPORTED_ISSUE_ID = "ble_scanner_firmware_unsupported_{unique}"
240+
OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID = (
241+
"outbound_websocket_incorrectly_enabled_{unique}"
242+
)
240243

241244
GAS_VALVE_OPEN_STATES = ("opening", "opened")
242245

homeassistant/components/shelly/repairs.py

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import voluptuous as vol
1212

1313
from homeassistant import data_entry_flow
14-
from homeassistant.components.repairs import RepairsFlow
14+
from homeassistant.components.repairs import ConfirmRepairFlow, RepairsFlow
1515
from homeassistant.core import HomeAssistant, callback
1616
from homeassistant.helpers import issue_registry as ir
1717

@@ -20,9 +20,11 @@
2020
BLE_SCANNER_MIN_FIRMWARE,
2121
CONF_BLE_SCANNER_MODE,
2222
DOMAIN,
23+
OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID,
2324
BLEScannerMode,
2425
)
2526
from .coordinator import ShellyConfigEntry
27+
from .utils import get_rpc_ws_url
2628

2729

2830
@callback
@@ -65,7 +67,46 @@ def async_manage_ble_scanner_firmware_unsupported_issue(
6567
ir.async_delete_issue(hass, DOMAIN, issue_id)
6668

6769

68-
class BleScannerFirmwareUpdateFlow(RepairsFlow):
70+
@callback
71+
def async_manage_outbound_websocket_incorrectly_enabled_issue(
72+
hass: HomeAssistant,
73+
entry: ShellyConfigEntry,
74+
) -> None:
75+
"""Manage the Outbound WebSocket incorrectly enabled issue."""
76+
issue_id = OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID.format(
77+
unique=entry.unique_id
78+
)
79+
80+
if TYPE_CHECKING:
81+
assert entry.runtime_data.rpc is not None
82+
83+
device = entry.runtime_data.rpc.device
84+
85+
if (
86+
(ws_config := device.config.get("ws"))
87+
and ws_config["enable"]
88+
and ws_config["server"] == get_rpc_ws_url(hass)
89+
):
90+
ir.async_create_issue(
91+
hass,
92+
DOMAIN,
93+
issue_id,
94+
is_fixable=True,
95+
is_persistent=True,
96+
severity=ir.IssueSeverity.WARNING,
97+
translation_key="outbound_websocket_incorrectly_enabled",
98+
translation_placeholders={
99+
"device_name": device.name,
100+
"ip_address": device.ip_address,
101+
},
102+
data={"entry_id": entry.entry_id},
103+
)
104+
return
105+
106+
ir.async_delete_issue(hass, DOMAIN, issue_id)
107+
108+
109+
class ShellyRpcRepairsFlow(RepairsFlow):
69110
"""Handler for an issue fixing flow."""
70111

71112
def __init__(self, device: RpcDevice) -> None:
@@ -83,7 +124,7 @@ async def async_step_confirm(
83124
) -> data_entry_flow.FlowResult:
84125
"""Handle the confirm step of a fix flow."""
85126
if user_input is not None:
86-
return await self.async_step_update_firmware()
127+
return await self._async_step_confirm()
87128

88129
issue_registry = ir.async_get(self.hass)
89130
description_placeholders = None
@@ -96,6 +137,18 @@ async def async_step_confirm(
96137
description_placeholders=description_placeholders,
97138
)
98139

140+
async def _async_step_confirm(self) -> data_entry_flow.FlowResult:
141+
"""Handle the confirm step of a fix flow."""
142+
raise NotImplementedError
143+
144+
145+
class BleScannerFirmwareUpdateFlow(ShellyRpcRepairsFlow):
146+
"""Handler for BLE Scanner Firmware Update flow."""
147+
148+
async def _async_step_confirm(self) -> data_entry_flow.FlowResult:
149+
"""Handle the confirm step of a fix flow."""
150+
return await self.async_step_update_firmware()
151+
99152
async def async_step_update_firmware(
100153
self, user_input: dict[str, str] | None = None
101154
) -> data_entry_flow.FlowResult:
@@ -110,6 +163,29 @@ async def async_step_update_firmware(
110163
return self.async_create_entry(title="", data={})
111164

112165

166+
class DisableOutboundWebSocketFlow(ShellyRpcRepairsFlow):
167+
"""Handler for Disable Outbound WebSocket flow."""
168+
169+
async def _async_step_confirm(self) -> data_entry_flow.FlowResult:
170+
"""Handle the confirm step of a fix flow."""
171+
return await self.async_step_disable_outbound_websocket()
172+
173+
async def async_step_disable_outbound_websocket(
174+
self, user_input: dict[str, str] | None = None
175+
) -> data_entry_flow.FlowResult:
176+
"""Handle the confirm step of a fix flow."""
177+
try:
178+
result = await self._device.ws_setconfig(
179+
False, self._device.config["ws"]["server"]
180+
)
181+
if result["restart_required"]:
182+
await self._device.trigger_reboot()
183+
except (DeviceConnectionError, RpcCallError):
184+
return self.async_abort(reason="cannot_connect")
185+
186+
return self.async_create_entry(title="", data={})
187+
188+
113189
async def async_create_fix_flow(
114190
hass: HomeAssistant, issue_id: str, data: dict[str, str] | None
115191
) -> RepairsFlow:
@@ -124,4 +200,11 @@ async def async_create_fix_flow(
124200
assert entry is not None
125201

126202
device = entry.runtime_data.rpc.device
127-
return BleScannerFirmwareUpdateFlow(device)
203+
204+
if "ble_scanner_firmware_unsupported" in issue_id:
205+
return BleScannerFirmwareUpdateFlow(device)
206+
207+
if "outbound_websocket_incorrectly_enabled" in issue_id:
208+
return DisableOutboundWebSocketFlow(device)
209+
210+
return ConfirmRepairFlow()

homeassistant/components/shelly/strings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@
288288
"unsupported_firmware": {
289289
"title": "Unsupported firmware for device {device_name}",
290290
"description": "Your Shelly device {device_name} with IP address {ip_address} is running an unsupported firmware. Please update the firmware.\n\nIf the device does not offer an update, check internet connectivity (gateway, DNS, time) and restart the device."
291+
},
292+
"outbound_websocket_incorrectly_enabled": {
293+
"title": "Outbound WebSocket is enabled for {device_name}",
294+
"fix_flow": {
295+
"step": {
296+
"confirm": {
297+
"title": "Outbound WebSocket is enabled for {device_name}",
298+
"description": "Your Shelly device {device_name} with IP address {ip_address} is a non-sleeping device and Outbound WebSocket should be disabled in its configuration.\n\nSelect **Submit** button to disable Outbound WebSocket."
299+
}
300+
},
301+
"abort": {
302+
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
303+
}
304+
}
291305
}
292306
}
293307
}

tests/components/shelly/test_repairs.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BLE_SCANNER_FIRMWARE_UNSUPPORTED_ISSUE_ID,
1010
CONF_BLE_SCANNER_MODE,
1111
DOMAIN,
12+
OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID,
1213
BLEScannerMode,
1314
)
1415
from homeassistant.core import HomeAssistant
@@ -129,3 +130,84 @@ async def test_unsupported_firmware_issue_exc(
129130

130131
assert issue_registry.async_get_issue(DOMAIN, issue_id)
131132
assert len(issue_registry.issues) == 1
133+
134+
135+
async def test_outbound_websocket_incorrectly_enabled_issue(
136+
hass: HomeAssistant,
137+
hass_client: ClientSessionGenerator,
138+
mock_rpc_device: Mock,
139+
issue_registry: ir.IssueRegistry,
140+
monkeypatch: pytest.MonkeyPatch,
141+
) -> None:
142+
"""Test repair issues handling for the outbound WebSocket incorrectly enabled."""
143+
ws_url = "ws://10.10.10.10:8123/api/shelly/ws"
144+
monkeypatch.setitem(
145+
mock_rpc_device.config, "ws", {"enable": True, "server": ws_url}
146+
)
147+
148+
issue_id = OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID.format(unique=MOCK_MAC)
149+
assert await async_setup_component(hass, "repairs", {})
150+
await hass.async_block_till_done()
151+
await init_integration(hass, 2)
152+
153+
assert issue_registry.async_get_issue(DOMAIN, issue_id)
154+
assert len(issue_registry.issues) == 1
155+
156+
await async_process_repairs_platforms(hass)
157+
client = await hass_client()
158+
result = await start_repair_fix_flow(client, DOMAIN, issue_id)
159+
160+
flow_id = result["flow_id"]
161+
assert result["step_id"] == "confirm"
162+
163+
result = await process_repair_fix_flow(client, flow_id)
164+
assert result["type"] == "create_entry"
165+
assert mock_rpc_device.ws_setconfig.call_count == 1
166+
assert mock_rpc_device.ws_setconfig.call_args[0] == (False, ws_url)
167+
assert mock_rpc_device.trigger_reboot.call_count == 1
168+
169+
# Assert the issue is no longer present
170+
assert not issue_registry.async_get_issue(DOMAIN, issue_id)
171+
assert len(issue_registry.issues) == 0
172+
173+
174+
@pytest.mark.parametrize(
175+
"exception", [DeviceConnectionError, RpcCallError(999, "Unknown error")]
176+
)
177+
async def test_outbound_websocket_incorrectly_enabled_issue_exc(
178+
hass: HomeAssistant,
179+
hass_client: ClientSessionGenerator,
180+
mock_rpc_device: Mock,
181+
issue_registry: ir.IssueRegistry,
182+
monkeypatch: pytest.MonkeyPatch,
183+
exception: Exception,
184+
) -> None:
185+
"""Test repair issues handling when ws_setconfig ends with an exception."""
186+
ws_url = "ws://10.10.10.10:8123/api/shelly/ws"
187+
monkeypatch.setitem(
188+
mock_rpc_device.config, "ws", {"enable": True, "server": ws_url}
189+
)
190+
191+
issue_id = OUTBOUND_WEBSOCKET_INCORRECTLY_ENABLED_ISSUE_ID.format(unique=MOCK_MAC)
192+
assert await async_setup_component(hass, "repairs", {})
193+
await hass.async_block_till_done()
194+
await init_integration(hass, 2)
195+
196+
assert issue_registry.async_get_issue(DOMAIN, issue_id)
197+
assert len(issue_registry.issues) == 1
198+
199+
await async_process_repairs_platforms(hass)
200+
client = await hass_client()
201+
result = await start_repair_fix_flow(client, DOMAIN, issue_id)
202+
203+
flow_id = result["flow_id"]
204+
assert result["step_id"] == "confirm"
205+
206+
mock_rpc_device.ws_setconfig.side_effect = exception
207+
result = await process_repair_fix_flow(client, flow_id)
208+
assert result["type"] == "abort"
209+
assert result["reason"] == "cannot_connect"
210+
assert mock_rpc_device.ws_setconfig.call_count == 1
211+
212+
assert issue_registry.async_get_issue(DOMAIN, issue_id)
213+
assert len(issue_registry.issues) == 1

0 commit comments

Comments
 (0)