From b62c7abc5de2731a78533e30bc73ba31d080678e Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 9 May 2020 00:34:18 +0200 Subject: [PATCH] UniFi - Additional consolidation for cleanliness sake (#35395) * Some additional consolidation for cleanliness sake * Remove unnecessary mac property --- .../components/unifi/device_tracker.py | 25 +++++++----------- homeassistant/components/unifi/switch.py | 4 +-- .../components/unifi/unifi_client.py | 21 ++++----------- .../components/unifi/unifi_entity_base.py | 26 ++++++++++--------- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/unifi/device_tracker.py b/homeassistant/components/unifi/device_tracker.py index 834f470b6fd..919d8e873c0 100644 --- a/homeassistant/components/unifi/device_tracker.py +++ b/homeassistant/components/unifi/device_tracker.py @@ -136,12 +136,12 @@ class UniFiClientTracker(UniFiClient, ScannerEntity): self.schedule_update = False self.cancel_scheduled_update = None self._is_connected = False - if self.client.last_seen: + if client.last_seen: self._is_connected = ( - self.is_wired == self.client.is_wired + self.is_wired == client.is_wired and dt_util.utcnow() - - dt_util.utc_from_timestamp(float(self.client.last_seen)) - < self.controller.option_detection_time + - dt_util.utc_from_timestamp(float(client.last_seen)) + < controller.option_detection_time ) if self._is_connected: self.schedule_update = True @@ -260,25 +260,18 @@ class UniFiDeviceTracker(UniFiBase, ScannerEntity): def __init__(self, device, controller): """Set up tracked device.""" - self.device = device - super().__init__(controller) + super().__init__(device, controller) - self._is_connected = self.device.state == 1 + self._is_connected = device.state == 1 self.cancel_scheduled_update = None @property - def mac(self): - """Return MAC of device.""" - return self.device.mac - - async def async_added_to_hass(self): - """Subscribe to device events.""" - await super().async_added_to_hass() - self.device.register_callback(self.async_update_callback) + def device(self): + """Wrap item.""" + return self._item async def async_will_remove_from_hass(self) -> None: """Disconnect device object when removed.""" - self.device.remove_callback(self.async_update_callback) if self.cancel_scheduled_update: self.cancel_scheduled_update() await super().async_will_remove_from_hass() diff --git a/homeassistant/components/unifi/switch.py b/homeassistant/components/unifi/switch.py index d99ed29cd44..a216daaf9bb 100644 --- a/homeassistant/components/unifi/switch.py +++ b/homeassistant/components/unifi/switch.py @@ -158,7 +158,7 @@ class UniFiPOEClientSwitch(UniFiClient, SwitchEntity, RestoreEntity): super().__init__(client, controller) self.poe_mode = None - if self.client.sw_port and self.port.poe_mode != "off": + if client.sw_port and self.port.poe_mode != "off": self.poe_mode = self.port.poe_mode async def async_added_to_hass(self): @@ -252,7 +252,7 @@ class UniFiBlockClientSwitch(UniFiClient, SwitchEntity): """Set up block switch.""" super().__init__(client, controller) - self._is_blocked = self.client.blocked + self._is_blocked = client.blocked @callback def async_update_callback(self) -> None: diff --git a/homeassistant/components/unifi/unifi_client.py b/homeassistant/components/unifi/unifi_client.py index 4efae7c2574..9842cdc0777 100644 --- a/homeassistant/components/unifi/unifi_client.py +++ b/homeassistant/components/unifi/unifi_client.py @@ -14,25 +14,14 @@ class UniFiClient(UniFiBase): def __init__(self, client, controller) -> None: """Set up client.""" - self.client = client - super().__init__(controller) + super().__init__(client, controller) - self._is_wired = self.client.mac not in controller.wireless_clients + self._is_wired = client.mac not in controller.wireless_clients @property - def mac(self): - """Return MAC of client.""" - return self.client.mac - - async def async_added_to_hass(self) -> None: - """Client entity created.""" - await super().async_added_to_hass() - self.client.register_callback(self.async_update_callback) - - async def async_will_remove_from_hass(self) -> None: - """Disconnect client object when removed.""" - self.client.remove_callback(self.async_update_callback) - await super().async_will_remove_from_hass() + def client(self): + """Wrap item.""" + return self._item @property def is_wired(self): diff --git a/homeassistant/components/unifi/unifi_entity_base.py b/homeassistant/components/unifi/unifi_entity_base.py index b50a57c83ec..6fbc18f75d3 100644 --- a/homeassistant/components/unifi/unifi_entity_base.py +++ b/homeassistant/components/unifi/unifi_entity_base.py @@ -15,33 +15,33 @@ class UniFiBase(Entity): DOMAIN = "" TYPE = "" - def __init__(self, controller) -> None: + def __init__(self, item, controller) -> None: """Set up UniFi entity base. Register mac to controller entities to cover disabled entities. """ + self._item = item self.controller = controller - self.controller.entities[self.DOMAIN][self.TYPE].add(self.mac) - - @property - def mac(self): - """Return MAC of entity.""" - raise NotImplementedError + self.controller.entities[self.DOMAIN][self.TYPE].add(item.mac) async def async_added_to_hass(self) -> None: """Entity created.""" - LOGGER.debug("New %s entity %s (%s)", self.TYPE, self.entity_id, self.mac) + LOGGER.debug("New %s entity %s (%s)", self.TYPE, self.entity_id, self._item.mac) for signal, method in ( (self.controller.signal_reachable, self.async_update_callback), (self.controller.signal_options_update, self.options_updated), (self.controller.signal_remove, self.remove_item), ): self.async_on_remove(async_dispatcher_connect(self.hass, signal, method)) + self._item.register_callback(self.async_update_callback) async def async_will_remove_from_hass(self) -> None: """Disconnect object when removed.""" - LOGGER.debug("Removing %s entity %s (%s)", self.TYPE, self.entity_id, self.mac) - self.controller.entities[self.DOMAIN][self.TYPE].remove(self.mac) + LOGGER.debug( + "Removing %s entity %s (%s)", self.TYPE, self.entity_id, self._item.mac + ) + self._item.remove_callback(self.async_update_callback) + self.controller.entities[self.DOMAIN][self.TYPE].remove(self._item.mac) async def async_remove(self): """Clean up when removing entity. @@ -72,7 +72,9 @@ class UniFiBase(Entity): @callback def async_update_callback(self) -> None: """Update the entity's state.""" - LOGGER.debug("Updating %s entity %s (%s)", self.TYPE, self.entity_id, self.mac) + LOGGER.debug( + "Updating %s entity %s (%s)", self.TYPE, self.entity_id, self._item.mac + ) self.async_write_ha_state() async def options_updated(self) -> None: @@ -81,7 +83,7 @@ class UniFiBase(Entity): async def remove_item(self, mac_addresses: set) -> None: """Remove entity if MAC is part of set.""" - if self.mac in mac_addresses: + if self._item.mac in mac_addresses: await self.async_remove() @property