1
mirror of https://github.com/home-assistant/core synced 2024-07-24 16:42:06 +02:00

Switch frequently used unifi controller properties to attributes (#45365)

This commit is contained in:
J. Nick Koston 2021-01-20 16:58:02 -06:00 committed by GitHub
parent 243014bff1
commit 536e835b39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 86 deletions

View File

@ -103,8 +103,60 @@ class UniFiController:
self._heartbeat_dispatch = {}
self._heartbeat_time = {}
self.load_config_entry_options()
self.entities = {}
def load_config_entry_options(self):
"""Store attributes to avoid property call overhead since they are called frequently."""
# Device tracker options
options = self.config_entry.options
# Config entry option to not track clients.
self.option_track_clients = options.get(
CONF_TRACK_CLIENTS, DEFAULT_TRACK_CLIENTS
)
# Config entry option to not track wired clients.
self.option_track_wired_clients = options.get(
CONF_TRACK_WIRED_CLIENTS, DEFAULT_TRACK_WIRED_CLIENTS
)
# Config entry option to not track devices.
self.option_track_devices = options.get(
CONF_TRACK_DEVICES, DEFAULT_TRACK_DEVICES
)
# Config entry option listing what SSIDs are being used to track clients.
self.option_ssid_filter = set(options.get(CONF_SSID_FILTER, []))
# Config entry option defining number of seconds from last seen to away
self.option_detection_time = timedelta(
seconds=options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)
)
# Config entry option to ignore wired bug.
self.option_ignore_wired_bug = options.get(
CONF_IGNORE_WIRED_BUG, DEFAULT_IGNORE_WIRED_BUG
)
# Client control options
# Config entry option to control poe clients.
self.option_poe_clients = options.get(CONF_POE_CLIENTS, DEFAULT_POE_CLIENTS)
# Config entry option with list of clients to control network access.
self.option_block_clients = options.get(CONF_BLOCK_CLIENT, [])
# Config entry option to control DPI restriction groups.
self.option_dpi_restrictions = options.get(
CONF_DPI_RESTRICTIONS, DEFAULT_DPI_RESTRICTIONS
)
# Statistics sensor options
# Config entry option to allow bandwidth sensors.
self.option_allow_bandwidth_sensors = options.get(
CONF_ALLOW_BANDWIDTH_SENSORS, DEFAULT_ALLOW_BANDWIDTH_SENSORS
)
# Config entry option to allow uptime sensors.
self.option_allow_uptime_sensors = options.get(
CONF_ALLOW_UPTIME_SENSORS, DEFAULT_ALLOW_UPTIME_SENSORS
)
@property
def controller_id(self):
"""Return the controller ID."""
@ -138,81 +190,6 @@ class UniFiController:
return client.mac
return None
# Device tracker options
@property
def option_track_clients(self):
"""Config entry option to not track clients."""
return self.config_entry.options.get(CONF_TRACK_CLIENTS, DEFAULT_TRACK_CLIENTS)
@property
def option_track_wired_clients(self):
"""Config entry option to not track wired clients."""
return self.config_entry.options.get(
CONF_TRACK_WIRED_CLIENTS, DEFAULT_TRACK_WIRED_CLIENTS
)
@property
def option_track_devices(self):
"""Config entry option to not track devices."""
return self.config_entry.options.get(CONF_TRACK_DEVICES, DEFAULT_TRACK_DEVICES)
@property
def option_ssid_filter(self):
"""Config entry option listing what SSIDs are being used to track clients."""
return self.config_entry.options.get(CONF_SSID_FILTER, [])
@property
def option_detection_time(self):
"""Config entry option defining number of seconds from last seen to away."""
return timedelta(
seconds=self.config_entry.options.get(
CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME
)
)
@property
def option_ignore_wired_bug(self):
"""Config entry option to ignore wired bug."""
return self.config_entry.options.get(
CONF_IGNORE_WIRED_BUG, DEFAULT_IGNORE_WIRED_BUG
)
# Client control options
@property
def option_poe_clients(self):
"""Config entry option to control poe clients."""
return self.config_entry.options.get(CONF_POE_CLIENTS, DEFAULT_POE_CLIENTS)
@property
def option_block_clients(self):
"""Config entry option with list of clients to control network access."""
return self.config_entry.options.get(CONF_BLOCK_CLIENT, [])
@property
def option_dpi_restrictions(self):
"""Config entry option to control DPI restriction groups."""
return self.config_entry.options.get(
CONF_DPI_RESTRICTIONS, DEFAULT_DPI_RESTRICTIONS
)
# Statistics sensor options
@property
def option_allow_bandwidth_sensors(self):
"""Config entry option to allow bandwidth sensors."""
return self.config_entry.options.get(
CONF_ALLOW_BANDWIDTH_SENSORS, DEFAULT_ALLOW_BANDWIDTH_SENSORS
)
@property
def option_allow_uptime_sensors(self):
"""Config entry option to allow uptime sensors."""
return self.config_entry.options.get(
CONF_ALLOW_UPTIME_SENSORS, DEFAULT_ALLOW_UPTIME_SENSORS
)
@callback
def async_unifi_signalling_callback(self, signal, data):
"""Handle messages back from UniFi library."""
@ -434,6 +411,7 @@ class UniFiController:
if config_entry.entry_id not in hass.data[UNIFI_DOMAIN]:
return
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
controller.load_config_entry_options()
async_dispatcher_send(hass, controller.signal_options_update)
@callback

View File

@ -289,13 +289,9 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity):
"""Set up tracked device."""
super().__init__(device, controller)
self.device = self._item
self._is_connected = device.state == 1
@property
def device(self):
"""Wrap item."""
return self._item
async def async_added_to_hass(self) -> None:
"""Watch object when added."""
self.async_on_remove(

View File

@ -12,11 +12,7 @@ class UniFiClient(UniFiBase):
super().__init__(client, controller)
self._is_wired = client.mac not in controller.wireless_clients
@property
def client(self):
"""Wrap item."""
return self._item
self.client = self._item
@property
def is_wired(self):
@ -29,6 +25,7 @@ class UniFiClient(UniFiBase):
if self.controller.option_ignore_wired_bug:
return self.client.is_wired
return self._is_wired
@property

View File

@ -197,7 +197,7 @@ async def test_controller_setup(hass):
assert controller.option_track_devices == DEFAULT_TRACK_DEVICES
assert controller.option_track_wired_clients == DEFAULT_TRACK_WIRED_CLIENTS
assert controller.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME)
assert isinstance(controller.option_ssid_filter, list)
assert isinstance(controller.option_ssid_filter, set)
assert controller.mac is None

View File

@ -606,6 +606,7 @@ async def test_option_ssid_filter(hass):
controller.config_entry,
options={CONF_SSID_FILTER: []},
)
await hass.async_block_till_done()
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_1_copy]}
controller.api.message_handler(event)
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_3_copy]}
@ -626,6 +627,9 @@ async def test_option_ssid_filter(hass):
client_1 = hass.states.get("device_tracker.client_1")
assert client_1.state == "not_home"
event = {"meta": {"message": MESSAGE_CLIENT}, "data": [client_3_copy]}
controller.api.message_handler(event)
await hass.async_block_till_done()
# Client won't go away until after next update
client_3 = hass.states.get("device_tracker.client_3")
assert client_3.state == "home"