Remove (2021) backwards supporting functionality from UniFi (#81981)

This commit is contained in:
Robert Svensson 2022-11-16 12:49:10 +01:00 committed by GitHub
parent 9b8f94363c
commit af6338343e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 60 deletions

View File

@ -1,6 +1,4 @@
"""Integration to UniFi Network and its various features.""" """Integration to UniFi Network and its various features."""
from collections.abc import Mapping
from typing import Any
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STOP
@ -10,12 +8,7 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import DOMAIN as UNIFI_DOMAIN, PLATFORMS, UNIFI_WIRELESS_CLIENTS
CONF_CONTROLLER,
DOMAIN as UNIFI_DOMAIN,
PLATFORMS,
UNIFI_WIRELESS_CLIENTS,
)
from .controller import UniFiController, get_unifi_controller from .controller import UniFiController, get_unifi_controller
from .errors import AuthenticationRequired, CannotConnect from .errors import AuthenticationRequired, CannotConnect
from .services import async_setup_services, async_unload_services from .services import async_setup_services, async_unload_services
@ -40,9 +33,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
# Removal of legacy PoE control was introduced with 2022.12 # Removal of legacy PoE control was introduced with 2022.12
async_remove_poe_client_entities(hass, config_entry) async_remove_poe_client_entities(hass, config_entry)
# Flat configuration was introduced with 2021.3
await async_flatten_entry_data(hass, config_entry)
try: try:
api = await get_unifi_controller(hass, config_entry.data) api = await get_unifi_controller(hass, config_entry.data)
controller = UniFiController(hass, config_entry, api) controller = UniFiController(hass, config_entry, api)
@ -54,12 +44,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
except AuthenticationRequired as err: except AuthenticationRequired as err:
raise ConfigEntryAuthFailed from err raise ConfigEntryAuthFailed from err
# Unique ID was introduced with 2021.3
if config_entry.unique_id is None:
hass.config_entries.async_update_entry(
config_entry, unique_id=controller.site_id
)
hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
await controller.async_update_device_registry() await controller.async_update_device_registry()
@ -104,52 +88,36 @@ def async_remove_poe_client_entities(
ent_reg.async_remove(entity_id) ent_reg.async_remove(entity_id)
async def async_flatten_entry_data(
hass: HomeAssistant, config_entry: ConfigEntry
) -> None:
"""Simpler configuration structure for entry data.
Keep controller key layer in case user rollbacks.
"""
data: Mapping[str, Any] = {
**config_entry.data,
**config_entry.data[CONF_CONTROLLER],
}
if config_entry.data != data:
hass.config_entries.async_update_entry(config_entry, data=data)
class UnifiWirelessClients: class UnifiWirelessClients:
"""Class to store clients known to be wireless. """Class to store clients known to be wireless.
This is needed since wireless devices going offline might get marked as wired by UniFi. This is needed since wireless devices going offline might get marked as wired by UniFi.
""" """
def __init__(self, hass): def __init__(self, hass: HomeAssistant) -> None:
"""Set up client storage.""" """Set up client storage."""
self.hass = hass self.hass = hass
self.data = {} self.data: dict[str, dict[str, list[str]]] = {}
self._store = Store(hass, STORAGE_VERSION, STORAGE_KEY) self._store: Store = Store(hass, STORAGE_VERSION, STORAGE_KEY)
async def async_load(self): async def async_load(self) -> None:
"""Load data from file.""" """Load data from file."""
if (data := await self._store.async_load()) is not None: if (data := await self._store.async_load()) is not None:
self.data = data self.data = data
@callback @callback
def get_data(self, config_entry): def get_data(self, config_entry: ConfigEntry) -> set[str]:
"""Get data related to a specific controller.""" """Get data related to a specific controller."""
data = self.data.get(config_entry.entry_id, {"wireless_devices": []}) data = self.data.get(config_entry.entry_id, {"wireless_devices": []})
return set(data["wireless_devices"]) return set(data["wireless_devices"])
@callback @callback
def update_data(self, data, config_entry): def update_data(self, data: set[str], config_entry: ConfigEntry) -> None:
"""Update data and schedule to save to file.""" """Update data and schedule to save to file."""
self.data[config_entry.entry_id] = {"wireless_devices": list(data)} self.data[config_entry.entry_id] = {"wireless_devices": list(data)}
self._store.async_delay_save(self._data_to_save, SAVE_DELAY) self._store.async_delay_save(self._data_to_save, SAVE_DELAY)
@callback @callback
def _data_to_save(self): def _data_to_save(self) -> dict[str, dict[str, list[str]]]:
"""Return data of UniFi wireless clients to store in a file.""" """Return data of UniFi wireless clients to store in a file."""
return self.data return self.data

View File

@ -2,19 +2,13 @@
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components import unifi from homeassistant.components import unifi
from homeassistant.components.unifi import async_flatten_entry_data from homeassistant.components.unifi.const import DOMAIN as UNIFI_DOMAIN
from homeassistant.components.unifi.const import CONF_CONTROLLER, DOMAIN as UNIFI_DOMAIN
from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from .test_controller import ( from .test_controller import DEFAULT_CONFIG_ENTRY_ID, setup_unifi_integration
CONTROLLER_DATA,
DEFAULT_CONFIG_ENTRY_ID,
ENTRY_CONFIG,
setup_unifi_integration,
)
from tests.common import MockConfigEntry, flush_store from tests.common import flush_store
async def test_setup_with_no_config(hass): async def test_setup_with_no_config(hass):
@ -52,17 +46,6 @@ async def test_setup_entry_fails_trigger_reauth_flow(hass):
assert hass.data[UNIFI_DOMAIN] == {} assert hass.data[UNIFI_DOMAIN] == {}
async def test_flatten_entry_data(hass):
"""Verify entry data can be flattened."""
entry = MockConfigEntry(
domain=UNIFI_DOMAIN,
data={CONF_CONTROLLER: CONTROLLER_DATA},
)
await async_flatten_entry_data(hass, entry)
assert entry.data == ENTRY_CONFIG
async def test_unload_entry(hass, aioclient_mock): async def test_unload_entry(hass, aioclient_mock):
"""Test being able to unload an entry.""" """Test being able to unload an entry."""
config_entry = await setup_unifi_integration(hass, aioclient_mock) config_entry = await setup_unifi_integration(hass, aioclient_mock)