mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Make UniFi services handle unloaded config entry (#120028)
This commit is contained in:
parent
f30b20b4df
commit
6fb5a12ef1
@ -6,6 +6,7 @@ from typing import Any
|
|||||||
from aiounifi.models.client import ClientReconnectRequest, ClientRemoveRequest
|
from aiounifi.models.client import ClientReconnectRequest, ClientRemoveRequest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import ATTR_DEVICE_ID
|
from homeassistant.const import ATTR_DEVICE_ID
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
@ -66,9 +67,9 @@ async def async_reconnect_client(hass: HomeAssistant, data: Mapping[str, Any]) -
|
|||||||
if mac == "":
|
if mac == "":
|
||||||
return
|
return
|
||||||
|
|
||||||
for entry in hass.config_entries.async_entries(UNIFI_DOMAIN):
|
for config_entry in hass.config_entries.async_entries(UNIFI_DOMAIN):
|
||||||
if (
|
if config_entry.state is not ConfigEntryState.LOADED or (
|
||||||
(hub := entry.runtime_data)
|
(hub := config_entry.runtime_data)
|
||||||
and not hub.available
|
and not hub.available
|
||||||
or (client := hub.api.clients.get(mac)) is None
|
or (client := hub.api.clients.get(mac)) is None
|
||||||
or client.is_wired
|
or client.is_wired
|
||||||
@ -85,8 +86,12 @@ async def async_remove_clients(hass: HomeAssistant, data: Mapping[str, Any]) ->
|
|||||||
- Total time between first seen and last seen is less than 15 minutes.
|
- Total time between first seen and last seen is less than 15 minutes.
|
||||||
- Neither IP, hostname nor name is configured.
|
- Neither IP, hostname nor name is configured.
|
||||||
"""
|
"""
|
||||||
for entry in hass.config_entries.async_entries(UNIFI_DOMAIN):
|
for config_entry in hass.config_entries.async_entries(UNIFI_DOMAIN):
|
||||||
if (hub := entry.runtime_data) and not hub.available:
|
if (
|
||||||
|
config_entry.state is not ConfigEntryState.LOADED
|
||||||
|
or (hub := config_entry.runtime_data)
|
||||||
|
and not hub.available
|
||||||
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
clients_to_remove = []
|
clients_to_remove = []
|
||||||
|
@ -270,3 +270,44 @@ async def test_remove_clients_no_call_on_empty_list(
|
|||||||
aioclient_mock.clear_requests()
|
aioclient_mock.clear_requests()
|
||||||
await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True)
|
await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True)
|
||||||
assert aioclient_mock.call_count == 0
|
assert aioclient_mock.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"clients_all_payload",
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"first_seen": 100,
|
||||||
|
"last_seen": 500,
|
||||||
|
"mac": "00:00:00:00:00:01",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_services_handle_unloaded_config_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
aioclient_mock: AiohttpClientMocker,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
config_entry_setup: ConfigEntry,
|
||||||
|
clients_all_payload,
|
||||||
|
) -> None:
|
||||||
|
"""Verify no call is made if config entry is unloaded."""
|
||||||
|
await hass.config_entries.async_unload(config_entry_setup.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
aioclient_mock.clear_requests()
|
||||||
|
|
||||||
|
await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True)
|
||||||
|
assert aioclient_mock.call_count == 0
|
||||||
|
|
||||||
|
device_entry = device_registry.async_get_or_create(
|
||||||
|
config_entry_id=config_entry_setup.entry_id,
|
||||||
|
connections={(dr.CONNECTION_NETWORK_MAC, clients_all_payload[0]["mac"])},
|
||||||
|
)
|
||||||
|
await hass.services.async_call(
|
||||||
|
UNIFI_DOMAIN,
|
||||||
|
SERVICE_RECONNECT_CLIENT,
|
||||||
|
service_data={ATTR_DEVICE_ID: device_entry.id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert aioclient_mock.call_count == 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user