From b960ebeb8bbf791a8072e8503b417474fec134ac Mon Sep 17 00:00:00 2001
From: Maciej Bieniek <bieniu@users.noreply.github.com>
Date: Mon, 26 Aug 2024 21:17:38 +0200
Subject: [PATCH] Add WS outbound config to Shelly diagnostics (#124654)

Add WS Outbound config to diagnostics
---
 .../components/shelly/diagnostics.py          |  7 +++++
 tests/components/shelly/test_diagnostics.py   | 30 ++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py
index e70b76a7c00..a5fe1f5b6c0 100644
--- a/homeassistant/components/shelly/diagnostics.py
+++ b/homeassistant/components/shelly/diagnostics.py
@@ -11,6 +11,7 @@ from homeassistant.core import HomeAssistant
 from homeassistant.helpers.device_registry import format_mac
 
 from .coordinator import ShellyConfigEntry
+from .utils import get_rpc_ws_url
 
 TO_REDACT = {CONF_USERNAME, CONF_PASSWORD}
 
@@ -73,6 +74,12 @@ async def async_get_config_entry_diagnostics(
             device_settings = {
                 k: v for k, v in rpc_coordinator.device.config.items() if k in ["cloud"]
             }
+            ws_config = rpc_coordinator.device.config["ws"]
+            device_settings["ws_outbound_enabled"] = ws_config["enable"]
+            if ws_config["enable"]:
+                device_settings["ws_outbound_server_valid"] = bool(
+                    ws_config["server"] == get_rpc_ws_url(hass)
+                )
             device_status = {
                 k: v
                 for k, v in rpc_coordinator.device.status.items()
diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py
index 4fc8ea6ca8f..395c7ccfeaf 100644
--- a/tests/components/shelly/test_diagnostics.py
+++ b/tests/components/shelly/test_diagnostics.py
@@ -1,5 +1,6 @@
 """Tests for Shelly diagnostics platform."""
 
+from copy import deepcopy
 from unittest.mock import ANY, Mock, PropertyMock
 
 from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT
@@ -151,7 +152,7 @@ async def test_rpc_config_entry_diagnostics(
             "model": MODEL_25,
             "sw_version": "some fw string",
         },
-        "device_settings": {},
+        "device_settings": {"ws_outbound_enabled": False},
         "device_status": {
             "sys": {
                 "available_updates": {
@@ -164,3 +165,30 @@ async def test_rpc_config_entry_diagnostics(
         },
         "last_error": "DeviceConnectionError()",
     }
+
+
+@pytest.mark.parametrize(
+    ("ws_outbound_server", "ws_outbound_server_valid"),
+    [("ws://10.10.10.10:8123/api/shelly/ws", True), ("wrong_url", False)],
+)
+async def test_rpc_config_entry_diagnostics_ws_outbound(
+    hass: HomeAssistant,
+    hass_client: ClientSessionGenerator,
+    mock_rpc_device: Mock,
+    monkeypatch: pytest.MonkeyPatch,
+    ws_outbound_server: str,
+    ws_outbound_server_valid: bool,
+) -> None:
+    """Test config entry diagnostics for rpc device with websocket outbound."""
+    config = deepcopy(mock_rpc_device.config)
+    config["ws"] = {"enable": True, "server": ws_outbound_server}
+    monkeypatch.setattr(mock_rpc_device, "config", config)
+
+    entry = await init_integration(hass, 2, sleep_period=60)
+
+    result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
+
+    assert (
+        result["device_settings"]["ws_outbound_server_valid"]
+        == ws_outbound_server_valid
+    )
-- 
GitLab