UniFi integration move to push messaging (#31086)
* Rewrite UniFi integration to use push messaging * Add signalling for new clients/devices * Update list of known wireless clients when we get events of them connecting * Reconnection logic for websocket * Fix failing tests * Bump requirement to v12 * Add new tests * Update homeassistant/components/unifi/controller.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
06c8e53323
commit
958a867c11
14 changed files with 296 additions and 377 deletions
|
@ -4,11 +4,11 @@ import logging
|
|||
from homeassistant.components.unifi.config_flow import get_controller_from_config_entry
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_registry import DISABLED_CONFIG_ENTRY
|
||||
|
||||
from .unifi_client import UniFiClient
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_RECEIVING = "receiving"
|
||||
|
@ -29,7 +29,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
@callback
|
||||
def update_controller():
|
||||
"""Update the values of the controller."""
|
||||
update_items(controller, async_add_entities, sensors)
|
||||
add_entities(controller, async_add_entities, sensors)
|
||||
|
||||
controller.listeners.append(
|
||||
async_dispatcher_connect(hass, controller.signal_update, update_controller)
|
||||
|
@ -61,8 +61,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
|
||||
|
||||
@callback
|
||||
def update_items(controller, async_add_entities, sensors):
|
||||
"""Update sensors from the controller."""
|
||||
def add_entities(controller, async_add_entities, sensors):
|
||||
"""Add new sensor entities from the controller."""
|
||||
new_sensors = []
|
||||
|
||||
for client_id in controller.api.clients:
|
||||
|
@ -73,9 +73,6 @@ def update_items(controller, async_add_entities, sensors):
|
|||
item_id = f"{direction}-{client_id}"
|
||||
|
||||
if item_id in sensors:
|
||||
sensor = sensors[item_id]
|
||||
if sensor.enabled:
|
||||
sensor.async_schedule_update_ha_state()
|
||||
continue
|
||||
|
||||
sensors[item_id] = sensor_class(
|
||||
|
@ -87,14 +84,8 @@ def update_items(controller, async_add_entities, sensors):
|
|||
async_add_entities(new_sensors)
|
||||
|
||||
|
||||
class UniFiBandwidthSensor(Entity):
|
||||
"""UniFi Bandwidth sensor base class."""
|
||||
|
||||
def __init__(self, client, controller):
|
||||
"""Set up client."""
|
||||
self.client = client
|
||||
self.controller = controller
|
||||
self.is_wired = self.client.mac not in controller.wireless_clients
|
||||
class UniFiRxBandwidthSensor(UniFiClient):
|
||||
"""Receiving bandwidth sensor."""
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self):
|
||||
|
@ -103,37 +94,6 @@ class UniFiBandwidthSensor(Entity):
|
|||
return True
|
||||
return False
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Client entity created."""
|
||||
LOGGER.debug("New UniFi bandwidth sensor %s (%s)", self.name, self.client.mac)
|
||||
|
||||
async def async_update(self):
|
||||
"""Synchronize state with controller.
|
||||
|
||||
Make sure to update self.is_wired if client is wireless, there is an issue when clients go offline that they get marked as wired.
|
||||
"""
|
||||
LOGGER.debug(
|
||||
"Updating UniFi bandwidth sensor %s (%s)", self.entity_id, self.client.mac
|
||||
)
|
||||
await self.controller.request_update()
|
||||
|
||||
if self.is_wired and self.client.mac in self.controller.wireless_clients:
|
||||
self.is_wired = False
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if controller is available."""
|
||||
return self.controller.available
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return a device description for device registry."""
|
||||
return {"connections": {(CONNECTION_NETWORK_MAC, self.client.mac)}}
|
||||
|
||||
|
||||
class UniFiRxBandwidthSensor(UniFiBandwidthSensor):
|
||||
"""Receiving bandwidth sensor."""
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
|
@ -153,7 +113,7 @@ class UniFiRxBandwidthSensor(UniFiBandwidthSensor):
|
|||
return f"rx-{self.client.mac}"
|
||||
|
||||
|
||||
class UniFiTxBandwidthSensor(UniFiBandwidthSensor):
|
||||
class UniFiTxBandwidthSensor(UniFiRxBandwidthSensor):
|
||||
"""Transmitting bandwidth sensor."""
|
||||
|
||||
@property
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue