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..d1a7943ede3d3 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -107,6 +107,10 @@ 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 or not hub.config.option_track_wired_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..2def1ea7407b6 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -20,9 +20,11 @@ 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, + CONF_TRACK_WIRED_CLIENTS, DEFAULT_DETECTION_TIME, DEVICE_STATES, ) @@ -579,6 +581,42 @@ 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_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"]]}], +) +@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}],