mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
UniFi - use entity registry disabled_by to control available entities (#26141)
* Move ignoring logic to entity registry enabled default * Handle config to option import better * Properly enable and disable entity registry entries on changes from config entry options * Fix balloobs comments * Fix some tests * Fix tests * Simplify updating disable on entities * Simplify device tracker update function * Local entity disabled replaced on rebase * Only alter entities with changed options * Proper tracking of changed options * Back to straightforward updating of disabled
This commit is contained in:
@@ -18,6 +18,9 @@ from .const import (
|
||||
CONF_BLOCK_CLIENT,
|
||||
CONF_CONTROLLER,
|
||||
CONF_DETECTION_TIME,
|
||||
CONF_DONT_TRACK_CLIENTS,
|
||||
CONF_DONT_TRACK_DEVICES,
|
||||
CONF_DONT_TRACK_WIRED_CLIENTS,
|
||||
CONF_TRACK_CLIENTS,
|
||||
CONF_TRACK_DEVICES,
|
||||
CONF_TRACK_WIRED_CLIENTS,
|
||||
@@ -30,6 +33,7 @@ from .const import (
|
||||
DEFAULT_TRACK_WIRED_CLIENTS,
|
||||
DEFAULT_DETECTION_TIME,
|
||||
DEFAULT_SSID_FILTER,
|
||||
DOMAIN,
|
||||
LOGGER,
|
||||
UNIFI_CONFIG,
|
||||
)
|
||||
@@ -49,7 +53,6 @@ class UniFiController:
|
||||
|
||||
self._site_name = None
|
||||
self._site_role = None
|
||||
self.unifi_config = {}
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
@@ -116,11 +119,14 @@ class UniFiController:
|
||||
return None
|
||||
|
||||
@property
|
||||
def event_update(self):
|
||||
def signal_update(self):
|
||||
"""Event specific per UniFi entry to signal new data."""
|
||||
return "unifi-update-{}".format(
|
||||
CONTROLLER_ID.format(host=self.host, site=self.site)
|
||||
)
|
||||
return f"unifi-update-{CONTROLLER_ID.format(host=self.host, site=self.site)}"
|
||||
|
||||
@property
|
||||
def signal_options_update(self):
|
||||
"""Event specific per UniFi entry to signal new options."""
|
||||
return f"unifi-options-{CONTROLLER_ID.format(host=self.host, site=self.site)}"
|
||||
|
||||
async def request_update(self):
|
||||
"""Request an update."""
|
||||
@@ -164,7 +170,7 @@ class UniFiController:
|
||||
LOGGER.info("Reconnected to controller %s", self.host)
|
||||
self.available = True
|
||||
|
||||
async_dispatcher_send(self.hass, self.event_update)
|
||||
async_dispatcher_send(self.hass, self.signal_update)
|
||||
|
||||
async def async_setup(self):
|
||||
"""Set up a UniFi controller."""
|
||||
@@ -191,37 +197,9 @@ class UniFiController:
|
||||
LOGGER.error("Unknown error connecting with UniFi controller: %s", err)
|
||||
return False
|
||||
|
||||
for unifi_config in hass.data[UNIFI_CONFIG]:
|
||||
if (
|
||||
self.host == unifi_config[CONF_HOST]
|
||||
and self.site_name == unifi_config[CONF_SITE_ID]
|
||||
):
|
||||
self.unifi_config = unifi_config
|
||||
break
|
||||
self.import_configuration()
|
||||
|
||||
options = dict(self.config_entry.options)
|
||||
|
||||
if CONF_BLOCK_CLIENT in self.unifi_config:
|
||||
options[CONF_BLOCK_CLIENT] = self.unifi_config[CONF_BLOCK_CLIENT]
|
||||
|
||||
if CONF_TRACK_CLIENTS in self.unifi_config:
|
||||
options[CONF_TRACK_CLIENTS] = self.unifi_config[CONF_TRACK_CLIENTS]
|
||||
|
||||
if CONF_TRACK_DEVICES in self.unifi_config:
|
||||
options[CONF_TRACK_DEVICES] = self.unifi_config[CONF_TRACK_DEVICES]
|
||||
|
||||
if CONF_TRACK_WIRED_CLIENTS in self.unifi_config:
|
||||
options[CONF_TRACK_WIRED_CLIENTS] = self.unifi_config[
|
||||
CONF_TRACK_WIRED_CLIENTS
|
||||
]
|
||||
|
||||
if CONF_DETECTION_TIME in self.unifi_config:
|
||||
options[CONF_DETECTION_TIME] = self.unifi_config[CONF_DETECTION_TIME]
|
||||
|
||||
if CONF_SSID_FILTER in self.unifi_config:
|
||||
options[CONF_SSID_FILTER] = self.unifi_config[CONF_SSID_FILTER]
|
||||
|
||||
hass.config_entries.async_update_entry(self.config_entry, options=options)
|
||||
self.config_entry.add_update_listener(self.async_options_updated)
|
||||
|
||||
for platform in ["device_tracker", "switch"]:
|
||||
hass.async_create_task(
|
||||
@@ -232,6 +210,56 @@ class UniFiController:
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
async def async_options_updated(hass, entry):
|
||||
"""Triggered by config entry options updates."""
|
||||
controller_id = CONTROLLER_ID.format(
|
||||
host=entry.data[CONF_CONTROLLER][CONF_HOST],
|
||||
site=entry.data[CONF_CONTROLLER][CONF_SITE_ID],
|
||||
)
|
||||
controller = hass.data[DOMAIN][controller_id]
|
||||
|
||||
async_dispatcher_send(hass, controller.signal_options_update)
|
||||
|
||||
def import_configuration(self):
|
||||
"""Import configuration to config entry options."""
|
||||
unifi_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
|
||||
break
|
||||
|
||||
old_options = dict(self.config_entry.options)
|
||||
new_options = {}
|
||||
|
||||
for config, option in (
|
||||
(CONF_BLOCK_CLIENT, CONF_BLOCK_CLIENT),
|
||||
(CONF_DONT_TRACK_CLIENTS, CONF_TRACK_CLIENTS),
|
||||
(CONF_DONT_TRACK_WIRED_CLIENTS, CONF_TRACK_WIRED_CLIENTS),
|
||||
(CONF_DONT_TRACK_DEVICES, CONF_TRACK_DEVICES),
|
||||
(CONF_DETECTION_TIME, CONF_DETECTION_TIME),
|
||||
(CONF_SSID_FILTER, CONF_SSID_FILTER),
|
||||
):
|
||||
if config in unifi_config:
|
||||
if config == option and unifi_config[
|
||||
config
|
||||
] != self.config_entry.options.get(option):
|
||||
new_options[option] = unifi_config[config]
|
||||
elif config != option and (
|
||||
option not in self.config_entry.options
|
||||
or unifi_config[config] == self.config_entry.options.get(option)
|
||||
):
|
||||
new_options[option] = not unifi_config[config]
|
||||
|
||||
if new_options:
|
||||
options = {**old_options, **new_options}
|
||||
self.hass.config_entries.async_update_entry(
|
||||
self.config_entry, options=options
|
||||
)
|
||||
|
||||
async def async_reset(self):
|
||||
"""Reset this controller to default state.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user