From 81bf0fef2e2fb13574611f524aaaa15933853b1d Mon Sep 17 00:00:00 2001 From: Sven Delmas Date: Thu, 10 Sep 2026 03:37:48 -0600 Subject: [PATCH] Report UniFi WAN latency as unknown when a monitor is unresponsive (#181809) Co-authored-by: Claude Opus 5 (1M context) Co-authored-by: Ariel Ebersberger --- homeassistant/components/unifi/sensor.py | 2 +- .../unifi/snapshots/test_sensor.ambr | 6 +-- tests/components/unifi/test_sensor.py | 37 ++++++++++++------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/unifi/sensor.py b/homeassistant/components/unifi/sensor.py index cf10b61d0578..0d6671ba78dc 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -263,7 +263,7 @@ def async_device_wan_latency_value_fn( # Checked by async_device_wan_latency_supported_fn assert target - return target.get("latency_average", 0) + return target.get("latency_average") @callback diff --git a/tests/components/unifi/snapshots/test_sensor.ambr b/tests/components/unifi/snapshots/test_sensor.ambr index efc258d9be8a..e493f9a7dd2b 100644 --- a/tests/components/unifi/snapshots/test_sensor.ambr +++ b/tests/components/unifi/snapshots/test_sensor.ambr @@ -812,7 +812,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0', + 'state': 'unknown', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.mock_name_cloudflare_wan_latency-entry] @@ -928,7 +928,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0', + 'state': 'unknown', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.mock_name_google_wan_latency-entry] @@ -1044,7 +1044,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0', + 'state': 'unknown', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.mock_name_microsoft_wan_latency-entry] diff --git a/tests/components/unifi/test_sensor.py b/tests/components/unifi/test_sensor.py index bf346b4b12d3..80e65b8651f9 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -1710,14 +1710,22 @@ async def test_device_uptime( ], ) @pytest.mark.parametrize( - ("monitor_id", "state", "updated_state", "index_to_update"), + ("monitor_id", "state", "index_to_update", "monitor_update", "updated_state"), [ - # Microsoft - ("microsoft_wan", "56", "20", 0), - # Google - ("google_wan", "53", "90", 1), - # Cloudflare - ("cloudflare_wan", "30", "80", 2), + pytest.param( + "microsoft_wan", "56", 0, {"latency_average": 20}, "20", id="microsoft" + ), + pytest.param("google_wan", "53", 1, {"latency_average": 90}, "90", id="google"), + pytest.param( + "cloudflare_wan", "30", 2, {"latency_average": 80}, "80", id="cloudflare" + ), + pytest.param( + "microsoft_wan", "56", 0, {}, STATE_UNKNOWN, id="microsoft_no_response" + ), + pytest.param("google_wan", "53", 1, {}, STATE_UNKNOWN, id="google_no_response"), + pytest.param( + "cloudflare_wan", "30", 2, {}, STATE_UNKNOWN, id="cloudflare_no_response" + ), ], ) @pytest.mark.usefixtures("config_entry_setup") @@ -1728,8 +1736,9 @@ async def test_wan_monitor_latency( device_payload: list[dict[str, Any]], monitor_id: str, state: str, - updated_state: str, index_to_update: int, + monitor_update: dict[str, Any], + updated_state: str, ) -> None: """Verify that wan latency sensors are working as expected.""" entity_id = f"sensor.mock_name_{monitor_id}_latency" @@ -1757,14 +1766,14 @@ async def test_wan_monitor_latency( # Verify sensor state assert hass.states.get(entity_id).state == state - # Verify state update - device = device_payload[0] - device["uptime_stats"]["WAN"]["monitors"][index_to_update]["latency_average"] = ( - updated_state - ) - + # Update state + device = deepcopy(device_payload[0]) + monitor = device["uptime_stats"]["WAN"]["monitors"][index_to_update] + monitor.pop("latency_average") + monitor.update(monitor_update) mock_websocket_message(message=MessageKey.DEVICE, data=device) + # Verify state update assert hass.states.get(entity_id).state == updated_state