UniFi - Improve controller tests (#27261)

* Improve controller tests and harmonize setup_unifi_integration to one
* Store listeners to dispatchers to be used during reset
This commit is contained in:
Robert Svensson authored and GitHub committed 2019-10-07 21:55:35 +02:00
1 parent 1febb32dd9
commit 6565c17828
10 files changed
+401 -583

No files matched your search

+3 -2
View File
@@ -77,12 +77,13 @@ async def async_setup_entry(hass, config_entry):
hass.data[DOMAIN] = {}
controller = UniFiController(hass, config_entry)
controller_id = get_controller_id_from_config_entry(config_entry)
hass.data[DOMAIN][controller_id] = controller
if not await controller.async_setup():
return False
controller_id = get_controller_id_from_config_entry(config_entry)
hass.data[DOMAIN][controller_id] = controller
if controller.mac is None:
return True
+14 -11
View File
@@ -57,6 +57,7 @@ class UniFiController:
self.progress = None
self.wireless_clients = None
self.listeners = []
self._site_name = None
self._site_role = None
@@ -258,13 +259,14 @@ class UniFiController:
def import_configuration(self):
"""Import configuration to config entry options."""
unifi_config = {}
import_config = {}
for config in self.hass.data[UNIFI_CONFIG]:
if (
self.host == config[CONF_HOST]
and self.site_name == config[CONF_SITE_ID]
):
unifi_config = config
import_config = config
break
old_options = dict(self.config_entry.options)
@@ -278,16 +280,17 @@ class UniFiController:
(CONF_DETECTION_TIME, CONF_DETECTION_TIME),
(CONF_SSID_FILTER, CONF_SSID_FILTER),
):
if config in unifi_config:
if config == option and unifi_config[
if config in import_config:
print(config)
if config == option and import_config[
config
] != self.config_entry.options.get(option):
new_options[option] = unifi_config[config]
new_options[option] = import_config[config]
elif config != option and (
option not in self.config_entry.options
or unifi_config[config] == self.config_entry.options.get(option)
or import_config[config] == self.config_entry.options.get(option)
):
new_options[option] = not unifi_config[config]
new_options[option] = not import_config[config]
if new_options:
options = {**old_options, **new_options}
@@ -301,15 +304,15 @@ class UniFiController:
Will cancel any scheduled setup retry and will unload
the config entry.
"""
# If the authentication was wrong.
if self.api is None:
return True
for platform in SUPPORTED_PLATFORMS:
await self.hass.config_entries.async_forward_entry_unload(
self.config_entry, platform
)
for unsub_dispatcher in self.listeners:
unsub_dispatcher()
self.listeners = []
return True
@@ -67,7 +67,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Update the values of the controller."""
update_items(controller, async_add_entities, tracked)
async_dispatcher_connect(hass, controller.signal_update, update_controller)
controller.listeners.append(
async_dispatcher_connect(hass, controller.signal_update, update_controller)
)
@callback
def update_disable_on_entities():
@@ -82,8 +84,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entity.registry_entry.entity_id, disabled_by=disabled_by
)
async_dispatcher_connect(
hass, controller.signal_options_update, update_disable_on_entities
controller.listeners.append(
async_dispatcher_connect(
hass, controller.signal_options_update, update_disable_on_entities
)
)
update_controller()
+7 -3
View File
@@ -31,7 +31,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Update the values of the controller."""
update_items(controller, async_add_entities, sensors)
async_dispatcher_connect(hass, controller.signal_update, update_controller)
controller.listeners.append(
async_dispatcher_connect(hass, controller.signal_update, update_controller)
)
@callback
def update_disable_on_entities():
@@ -46,8 +48,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entity.registry_entry.entity_id, disabled_by=disabled_by
)
async_dispatcher_connect(
hass, controller.signal_options_update, update_disable_on_entities
controller.listeners.append(
async_dispatcher_connect(
hass, controller.signal_options_update, update_disable_on_entities
)
)
update_controller()
+3 -1
View File
@@ -53,7 +53,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Update the values of the controller."""
update_items(controller, async_add_entities, switches, switches_off)
async_dispatcher_connect(hass, controller.signal_update, update_controller)
controller.listeners.append(
async_dispatcher_connect(hass, controller.signal_update, update_controller)
)
update_controller()
switches_off.clear()