From 08762399276ebb12fdef1414b3f4ac0d943e6891 Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Sun, 3 Apr 2022 13:17:08 +0200 Subject: [PATCH] Do not redact diagnostics empty key (#69160) --- homeassistant/components/diagnostics/util.py | 4 ++++ tests/components/co2signal/test_diagnostics.py | 2 +- tests/components/diagnostics/test_util.py | 9 +++++++++ tests/components/esphome/conftest.py | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/diagnostics/util.py b/homeassistant/components/diagnostics/util.py index 8db2399fd53..cbb8831e9b5 100644 --- a/homeassistant/components/diagnostics/util.py +++ b/homeassistant/components/diagnostics/util.py @@ -33,6 +33,10 @@ def async_redact_data(data: _T, to_redact: Iterable[Any]) -> _T: redacted = {**data} for key, value in redacted.items(): + if value is None: + continue + if isinstance(value, str) and not value: + continue if key in to_redact: redacted[key] = REDACTED elif isinstance(value, Mapping): diff --git a/tests/components/co2signal/test_diagnostics.py b/tests/components/co2signal/test_diagnostics.py index f84cb5e84df..d6713662cb9 100644 --- a/tests/components/co2signal/test_diagnostics.py +++ b/tests/components/co2signal/test_diagnostics.py @@ -15,7 +15,7 @@ from tests.components.diagnostics import get_diagnostics_for_config_entry async def test_entry_diagnostics(hass, hass_client): """Test config entry diagnostics.""" config_entry = MockConfigEntry( - domain=DOMAIN, data={CONF_API_KEY: "", "location": ""} + domain=DOMAIN, data={CONF_API_KEY: "api_key", "location": ""} ) config_entry.add_to_hass(hass) with patch("CO2Signal.get_latest", return_value=VALID_PAYLOAD): diff --git a/tests/components/diagnostics/test_util.py b/tests/components/diagnostics/test_util.py index 702b838334f..b981ae096d1 100644 --- a/tests/components/diagnostics/test_util.py +++ b/tests/components/diagnostics/test_util.py @@ -13,12 +13,18 @@ def test_redact(): "key4_2": ["value4_2a", "value4_2b"], "key4_3": [["value4_3a", "value4_3b"], ["value4_3c", "value4_3d"]], }, + "key5": None, + "key6": "", + "key7": False, } to_redact = { "key1", "key3", "key4_1", + "key5", + "key6", + "key7", } assert async_redact_data(data, to_redact) == { @@ -30,4 +36,7 @@ def test_redact(): "key4_2": ["value4_2a", "value4_2b"], "key4_3": [["value4_3a", "value4_3b"], ["value4_3c", "value4_3d"]], }, + "key5": None, + "key6": "", + "key7": REDACTED, } diff --git a/tests/components/esphome/conftest.py b/tests/components/esphome/conftest.py index 7cf25f13015..4252e9d605b 100644 --- a/tests/components/esphome/conftest.py +++ b/tests/components/esphome/conftest.py @@ -22,7 +22,7 @@ def mock_config_entry() -> MockConfigEntry: data={ CONF_HOST: "192.168.1.2", CONF_PORT: 6053, - CONF_PASSWORD: "", + CONF_PASSWORD: "pwd", CONF_NOISE_PSK: "12345678123456781234567812345678", }, unique_id="esphome-device",