From ce3c23cb3ac4a0c895cafea16984e96dff4a7811 Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:56:08 +0200 Subject: [PATCH] Add Nut commands to diagnostics data (#96285) * Add Nut commands to diagnostics data * Add test for diagnostics --- homeassistant/components/nut/diagnostics.py | 9 ++++- tests/components/nut/test_diagnostics.py | 43 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/components/nut/test_diagnostics.py diff --git a/homeassistant/components/nut/diagnostics.py b/homeassistant/components/nut/diagnostics.py index e8c0a0711dc..9ee430a655b 100644 --- a/homeassistant/components/nut/diagnostics.py +++ b/homeassistant/components/nut/diagnostics.py @@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from . import PyNUTData -from .const import DOMAIN, PYNUT_DATA, PYNUT_UNIQUE_ID +from .const import DOMAIN, PYNUT_DATA, PYNUT_UNIQUE_ID, USER_AVAILABLE_COMMANDS TO_REDACT = {CONF_PASSWORD, CONF_USERNAME} @@ -26,7 +26,12 @@ async def async_get_config_entry_diagnostics( # Get information from Nut library nut_data: PyNUTData = hass_data[PYNUT_DATA] - data["nut_data"] = {"ups_list": nut_data.ups_list, "status": nut_data.status} + nut_cmd: set[str] = hass_data[USER_AVAILABLE_COMMANDS] + data["nut_data"] = { + "ups_list": nut_data.ups_list, + "status": nut_data.status, + "commands": nut_cmd, + } # Gather information how this Nut device is represented in Home Assistant device_registry = dr.async_get(hass) diff --git a/tests/components/nut/test_diagnostics.py b/tests/components/nut/test_diagnostics.py new file mode 100644 index 00000000000..f91269f5196 --- /dev/null +++ b/tests/components/nut/test_diagnostics.py @@ -0,0 +1,43 @@ +"""Tests for the diagnostics data provided by the Nut integration.""" + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.components.nut.diagnostics import TO_REDACT +from homeassistant.core import HomeAssistant + +from .util import async_init_integration + +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, +) -> None: + """Test diagnostics.""" + list_commands: set[str] = ["beeper.enable"] + list_commands_return_value = { + supported_command: supported_command for supported_command in list_commands + } + + mock_config_entry = await async_init_integration( + hass, + username="someuser", + password="somepassword", + list_vars={"ups.status": "OL"}, + list_ups={"ups1": "UPS 1"}, + list_commands_return_value=list_commands_return_value, + ) + + entry_dict = async_redact_data(mock_config_entry.as_dict(), TO_REDACT) + nut_data_dict = { + "ups_list": {"ups1": "UPS 1"}, + "status": {"ups.status": "OL"}, + "commands": list_commands, + } + + result = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + assert result["entry"] == entry_dict + assert result["nut_data"] == nut_data_dict