diff --git a/tests/components/unifi/test_button.py b/tests/components/unifi/test_button.py index 4518393226c..d5be861139b 100644 --- a/tests/components/unifi/test_button.py +++ b/tests/components/unifi/test_button.py @@ -1,8 +1,13 @@ """UniFi Network button platform tests.""" from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, ButtonDeviceClass -from homeassistant.components.unifi.const import DOMAIN as UNIFI_DOMAIN -from homeassistant.const import ATTR_DEVICE_CLASS, STATE_UNAVAILABLE, EntityCategory +from homeassistant.components.unifi.const import CONF_SITE_ID +from homeassistant.const import ( + ATTR_DEVICE_CLASS, + CONF_HOST, + STATE_UNAVAILABLE, + EntityCategory, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -36,8 +41,6 @@ async def test_restart_device_button( } ], ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] - assert len(hass.states.async_entity_ids(BUTTON_DOMAIN)) == 1 ent_reg_entry = entity_registry.async_get("button.switch_restart") @@ -52,7 +55,8 @@ async def test_restart_device_button( # Send restart device command aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/devmgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/devmgr", ) await hass.services.async_call( @@ -120,8 +124,6 @@ async def test_power_cycle_poe( } ], ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] - assert len(hass.states.async_entity_ids(BUTTON_DOMAIN)) == 2 ent_reg_entry = entity_registry.async_get("button.switch_port_1_power_cycle") @@ -136,7 +138,8 @@ async def test_power_cycle_poe( # Send restart device command aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/devmgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/devmgr", ) await hass.services.async_call( diff --git a/tests/components/unifi/test_device_tracker.py b/tests/components/unifi/test_device_tracker.py index 9a4b406c364..ebf9823fb6d 100644 --- a/tests/components/unifi/test_device_tracker.py +++ b/tests/components/unifi/test_device_tracker.py @@ -9,11 +9,13 @@ from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN from homeassistant.components.unifi.const import ( CONF_BLOCK_CLIENT, CONF_CLIENT_SOURCE, + CONF_DETECTION_TIME, CONF_IGNORE_WIRED_BUG, CONF_SSID_FILTER, CONF_TRACK_CLIENTS, CONF_TRACK_DEVICES, CONF_TRACK_WIRED_CLIENTS, + DEFAULT_DETECTION_TIME, DOMAIN as UNIFI_DOMAIN, ) from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE @@ -55,7 +57,6 @@ async def test_tracked_wireless_clients( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=[client] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 assert hass.states.get("device_tracker.client").state == STATE_NOT_HOME @@ -70,7 +71,9 @@ async def test_tracked_wireless_clients( # Change time to mark client as away - new_time = dt_util.utcnow() + hub.option_detection_time + new_time = dt_util.utcnow() + timedelta( + seconds=config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() @@ -194,7 +197,7 @@ async def test_tracked_wireless_clients_event_source( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=[client] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] + assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 assert hass.states.get("device_tracker.client").state == STATE_NOT_HOME @@ -243,7 +246,14 @@ async def test_tracked_wireless_clients_event_source( assert hass.states.get("device_tracker.client").state == STATE_HOME # Change time to mark client as away - freezer.tick(hub.option_detection_time + timedelta(seconds=1)) + freezer.tick( + timedelta( + seconds=( + config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + + 1 + ) + ) + ) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -282,7 +292,14 @@ async def test_tracked_wireless_clients_event_source( assert hass.states.get("device_tracker.client").state == STATE_HOME # Change time to mark client as away - freezer.tick(hub.option_detection_time + timedelta(seconds=1)) + freezer.tick( + timedelta( + seconds=( + config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + + 1 + ) + ) + ) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -682,10 +699,8 @@ async def test_option_ssid_filter( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=[client, client_on_ssid2] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2 - assert hass.states.get("device_tracker.client").state == STATE_HOME assert hass.states.get("device_tracker.client_on_ssid2").state == STATE_NOT_HOME @@ -711,7 +726,11 @@ async def test_option_ssid_filter( mock_unifi_websocket(message=MessageKey.CLIENT, data=client_on_ssid2) await hass.async_block_till_done() - new_time = dt_util.utcnow() + hub.option_detection_time + new_time = dt_util.utcnow() + timedelta( + seconds=( + config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + 1 + ) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() @@ -739,7 +758,11 @@ async def test_option_ssid_filter( # Time pass to mark client as away - new_time += hub.option_detection_time + new_time += timedelta( + seconds=( + config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + 1 + ) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() @@ -758,7 +781,9 @@ async def test_option_ssid_filter( mock_unifi_websocket(message=MessageKey.CLIENT, data=client_on_ssid2) await hass.async_block_till_done() - new_time += hub.option_detection_time + new_time += timedelta( + seconds=(config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() @@ -788,7 +813,6 @@ async def test_wireless_client_go_wired_issue( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=[client] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 @@ -807,7 +831,9 @@ async def test_wireless_client_go_wired_issue( assert client_state.state == STATE_HOME # Pass time - new_time = dt_util.utcnow() + hub.option_detection_time + new_time = dt_util.utcnow() + timedelta( + seconds=(config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() @@ -859,7 +885,6 @@ async def test_option_ignore_wired_bug( options={CONF_IGNORE_WIRED_BUG: True}, clients_response=[client], ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 1 # Client is wireless @@ -876,7 +901,9 @@ async def test_option_ignore_wired_bug( assert client_state.state == STATE_HOME # pass time - new_time = dt_util.utcnow() + hub.option_detection_time + new_time = dt_util.utcnow() + timedelta( + seconds=config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() diff --git a/tests/components/unifi/test_sensor.py b/tests/components/unifi/test_sensor.py index 8adc379dde8..99cc12eb3f1 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -18,10 +18,11 @@ from homeassistant.components.sensor import ( from homeassistant.components.unifi.const import ( CONF_ALLOW_BANDWIDTH_SENSORS, CONF_ALLOW_UPTIME_SENSORS, + CONF_DETECTION_TIME, CONF_TRACK_CLIENTS, CONF_TRACK_DEVICES, + DEFAULT_DETECTION_TIME, DEVICE_STATES, - DOMAIN as UNIFI_DOMAIN, ) from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY from homeassistant.const import ATTR_DEVICE_CLASS, STATE_UNAVAILABLE, EntityCategory @@ -395,7 +396,6 @@ async def test_bandwidth_sensors( # Verify reset sensor after heartbeat expires - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] new_time = dt_util.utcnow() wireless_client["last_seen"] = dt_util.as_timestamp(new_time) @@ -409,8 +409,11 @@ async def test_bandwidth_sensors( assert hass.states.get("sensor.wireless_client_rx").state == "3456.0" assert hass.states.get("sensor.wireless_client_tx").state == "7891.0" - new_time = new_time + hub.option_detection_time + timedelta(seconds=1) - + new_time += timedelta( + seconds=( + config_entry.options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME) + 1 + ) + ) with freeze_time(new_time): async_fire_time_changed(hass, new_time) await hass.async_block_till_done() diff --git a/tests/components/unifi/test_services.py b/tests/components/unifi/test_services.py index 613c492a490..0de7d35608f 100644 --- a/tests/components/unifi/test_services.py +++ b/tests/components/unifi/test_services.py @@ -1,13 +1,13 @@ """deCONZ service tests.""" from unittest.mock import patch -from homeassistant.components.unifi.const import DOMAIN as UNIFI_DOMAIN +from homeassistant.components.unifi.const import CONF_SITE_ID, DOMAIN as UNIFI_DOMAIN from homeassistant.components.unifi.services import ( SERVICE_RECONNECT_CLIENT, SERVICE_REMOVE_CLIENTS, SUPPORTED_SERVICES, ) -from homeassistant.const import ATTR_DEVICE_ID +from homeassistant.const import ATTR_DEVICE_ID, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr @@ -66,11 +66,11 @@ async def test_reconnect_client( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_response=clients ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/stamgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/stamgr", ) device_entry = device_registry.async_get_or_create( @@ -148,7 +148,8 @@ async def test_reconnect_client_hub_unavailable( aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/stamgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/stamgr", ) device_entry = device_registry.async_get_or_create( @@ -261,11 +262,11 @@ async def test_remove_clients( config_entry = await setup_unifi_integration( hass, aioclient_mock, clients_all_response=clients ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/stamgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/stamgr", ) await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) diff --git a/tests/components/unifi/test_switch.py b/tests/components/unifi/test_switch.py index 35b2327bcdc..c4fb18018f1 100644 --- a/tests/components/unifi/test_switch.py +++ b/tests/components/unifi/test_switch.py @@ -15,6 +15,7 @@ from homeassistant.components.switch import ( from homeassistant.components.unifi.const import ( CONF_BLOCK_CLIENT, CONF_DPI_RESTRICTIONS, + CONF_SITE_ID, CONF_TRACK_CLIENTS, CONF_TRACK_DEVICES, DOMAIN as UNIFI_DOMAIN, @@ -23,6 +24,7 @@ from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, + CONF_HOST, STATE_OFF, STATE_ON, STATE_UNAVAILABLE, @@ -829,7 +831,6 @@ async def test_switches( dpigroup_response=DPI_GROUPS, dpiapp_response=DPI_APPS, ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 3 @@ -857,7 +858,8 @@ async def test_switches( # Block and unblock client aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/stamgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/stamgr", ) await hass.services.async_call( @@ -881,7 +883,8 @@ async def test_switches( # Enable and disable DPI aioclient_mock.clear_requests() aioclient_mock.put( - f"https://{hub.host}:1234/api/s/{hub.site}/rest/dpiapp/5f976f62e3c58f018ec7e17d", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/dpiapp/{DPI_APPS[0]['_id']}", ) await hass.services.async_call( @@ -951,7 +954,6 @@ async def test_block_switches( clients_response=[UNBLOCKED], clients_all_response=[BLOCKED], ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2 @@ -981,7 +983,8 @@ async def test_block_switches( aioclient_mock.clear_requests() aioclient_mock.post( - f"https://{hub.host}:1234/api/s/{hub.site}/cmd/stamgr", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/stamgr", ) await hass.services.async_call( @@ -1142,7 +1145,6 @@ async def test_outlet_switches( config_entry = await setup_unifi_integration( hass, aioclient_mock, devices_response=[test_data] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == expected_switches # Validate state object switch_1 = hass.states.get(f"switch.{entity_id}") @@ -1161,7 +1163,8 @@ async def test_outlet_switches( device_id = test_data["device_id"] aioclient_mock.clear_requests() aioclient_mock.put( - f"https://{hub.host}:1234/api/s/{hub.site}/rest/device/{device_id}", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/device/{device_id}", ) await hass.services.async_call( @@ -1333,7 +1336,6 @@ async def test_poe_port_switches( config_entry = await setup_unifi_integration( hass, aioclient_mock, devices_response=[DEVICE_1] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0 @@ -1372,7 +1374,8 @@ async def test_poe_port_switches( # Turn off PoE aioclient_mock.clear_requests() aioclient_mock.put( - f"https://{hub.host}:1234/api/s/{hub.site}/rest/device/mock-id", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/device/mock-id", ) await hass.services.async_call( @@ -1445,7 +1448,6 @@ async def test_wlan_switches( config_entry = await setup_unifi_integration( hass, aioclient_mock, wlans_response=[WLAN] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1 @@ -1469,7 +1471,8 @@ async def test_wlan_switches( # Disable WLAN aioclient_mock.clear_requests() aioclient_mock.put( - f"https://{hub.host}:1234/api/s/{hub.site}" + f"/rest/wlanconf/{WLAN['_id']}", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/wlanconf/{WLAN['_id']}", ) await hass.services.async_call( @@ -1525,7 +1528,6 @@ async def test_port_forwarding_switches( config_entry = await setup_unifi_integration( hass, aioclient_mock, port_forward_response=[_data.copy()] ) - hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id] assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1 @@ -1549,8 +1551,8 @@ async def test_port_forwarding_switches( # Disable port forward aioclient_mock.clear_requests() aioclient_mock.put( - f"https://{hub.host}:1234/api/s/{hub.site}" - + f"/rest/portforward/{data['_id']}", + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/portforward/{data['_id']}", ) await hass.services.async_call( diff --git a/tests/components/unifi/test_update.py b/tests/components/unifi/test_update.py index a9440e20339..2c6852629ea 100644 --- a/tests/components/unifi/test_update.py +++ b/tests/components/unifi/test_update.py @@ -162,7 +162,10 @@ async def test_install( device_state = hass.states.get("update.device_1") assert device_state.state == STATE_ON - url = f"https://{config_entry.data[CONF_HOST]}:1234/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/devmgr" + url = ( + f"https://{config_entry.data[CONF_HOST]}:1234" + f"/api/s/{config_entry.data[CONF_SITE_ID]}/cmd/devmgr" + ) aioclient_mock.clear_requests() aioclient_mock.post(url)