From 8f4b876868461c443d5f9e01fb2b097dc9313922 Mon Sep 17 00:00:00 2001 From: RaHehl Date: Sun, 3 May 2026 08:14:02 +0000 Subject: [PATCH 1/2] Fix UniFi wired client speed sensor respecting track_clients option --- homeassistant/components/unifi/hub/config.py | 4 +-- homeassistant/components/unifi/sensor.py | 5 ++++ tests/components/unifi/test_sensor.py | 26 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifi/hub/config.py b/homeassistant/components/unifi/hub/config.py index ef3c48e3c1b7a..bb644f5914bba 100644 --- a/homeassistant/components/unifi/hub/config.py +++ b/homeassistant/components/unifi/hub/config.py @@ -56,9 +56,9 @@ class UnifiConfig: # Device tracker options - option_track_clients: list[str] + option_track_clients: bool """Config entry option to not track clients.""" - option_track_wired_clients: list[str] + option_track_wired_clients: bool """Config entry option to not track wired clients.""" option_track_devices: bool """Config entry option to not track devices.""" diff --git a/homeassistant/components/unifi/sensor.py b/homeassistant/components/unifi/sensor.py index 57888cd2c0833..2d7f3f4b249e7 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -107,6 +107,11 @@ def async_client_uptime_value_fn(hub: UnifiHub, client: Client) -> datetime: @callback def async_wired_client_allowed_fn(hub: UnifiHub, obj_id: str) -> bool: """Check if client is wired and allowed.""" + if ( + obj_id not in hub.config.option_supported_clients + and not hub.config.option_track_clients + ): + return False client = hub.api.clients[obj_id] if not client.is_wired or client.wired_rate_mbps <= 0: return False diff --git a/tests/components/unifi/test_sensor.py b/tests/components/unifi/test_sensor.py index 9104b8c0de7b9..714c006d44c2b 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -20,6 +20,7 @@ from homeassistant.components.unifi.const import ( CONF_ALLOW_BANDWIDTH_SENSORS, CONF_ALLOW_UPTIME_SENSORS, + CONF_CLIENT_SOURCE, CONF_DETECTION_TIME, CONF_TRACK_CLIENTS, CONF_TRACK_DEVICES, @@ -579,6 +580,31 @@ async def test_wired_client_speed_sensor( assert hass.states.get("sensor.wired_client_link_speed").state == STATE_UNAVAILABLE +@pytest.mark.parametrize("config_entry_options", [{CONF_TRACK_CLIENTS: False}]) +@pytest.mark.parametrize("client_payload", [[WIRED_CLIENT]]) +@pytest.mark.usefixtures("config_entry_setup") +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_wired_client_speed_sensor_tracking_disabled( + hass: HomeAssistant, +) -> None: + """Verify wired client speed sensor is not created when track_clients is disabled.""" + assert hass.states.get("sensor.wired_client_link_speed") is None + + +@pytest.mark.parametrize( + "config_entry_options", + [{CONF_TRACK_CLIENTS: False, CONF_CLIENT_SOURCE: [WIRED_CLIENT["mac"]]}], +) +@pytest.mark.parametrize("client_payload", [[WIRED_CLIENT]]) +@pytest.mark.usefixtures("config_entry_setup") +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_wired_client_speed_sensor_supported_client_bypass( + hass: HomeAssistant, +) -> None: + """Verify wired client speed sensor is created for supported clients even when tracking is disabled.""" + assert hass.states.get("sensor.wired_client_link_speed").state == "1000" + + @pytest.mark.parametrize( "config_entry_options", [{CONF_ALLOW_BANDWIDTH_SENSORS: True, CONF_ALLOW_UPTIME_SENSORS: True}], From 5b0cb1507cb81713d6d0b6eb4196ff690cb5e035 Mon Sep 17 00:00:00 2001 From: RaHehl Date: Sun, 3 May 2026 08:20:49 +0000 Subject: [PATCH 2/2] Address review: also respect option_track_wired_clients in wired client speed sensor --- homeassistant/components/unifi/sensor.py | 5 ++--- tests/components/unifi/test_sensor.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/unifi/sensor.py b/homeassistant/components/unifi/sensor.py index 2d7f3f4b249e7..d1a7943ede3d3 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -107,9 +107,8 @@ def async_client_uptime_value_fn(hub: UnifiHub, client: Client) -> datetime: @callback def async_wired_client_allowed_fn(hub: UnifiHub, obj_id: str) -> bool: """Check if client is wired and allowed.""" - if ( - obj_id not in hub.config.option_supported_clients - and not hub.config.option_track_clients + if obj_id not in hub.config.option_supported_clients and ( + not hub.config.option_track_clients or not hub.config.option_track_wired_clients ): return False client = hub.api.clients[obj_id] diff --git a/tests/components/unifi/test_sensor.py b/tests/components/unifi/test_sensor.py index 714c006d44c2b..2def1ea7407b6 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -24,6 +24,7 @@ CONF_DETECTION_TIME, CONF_TRACK_CLIENTS, CONF_TRACK_DEVICES, + CONF_TRACK_WIRED_CLIENTS, DEFAULT_DETECTION_TIME, DEVICE_STATES, ) @@ -591,6 +592,17 @@ async def test_wired_client_speed_sensor_tracking_disabled( assert hass.states.get("sensor.wired_client_link_speed") is None +@pytest.mark.parametrize("config_entry_options", [{CONF_TRACK_WIRED_CLIENTS: False}]) +@pytest.mark.parametrize("client_payload", [[WIRED_CLIENT]]) +@pytest.mark.usefixtures("config_entry_setup") +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_wired_client_speed_sensor_wired_tracking_disabled( + hass: HomeAssistant, +) -> None: + """Verify wired client speed sensor is not created when track_wired_clients is disabled.""" + assert hass.states.get("sensor.wired_client_link_speed") is None + + @pytest.mark.parametrize( "config_entry_options", [{CONF_TRACK_CLIENTS: False, CONF_CLIENT_SOURCE: [WIRED_CLIENT["mac"]]}],