|
| 1 | +"""Support for Ruckus Unleashed devices.""" |
| 2 | +from homeassistant.components.device_tracker import ( |
| 3 | + ATTR_MAC, |
| 4 | + ATTR_SOURCE_TYPE, |
| 5 | + SOURCE_TYPE_ROUTER, |
| 6 | +) |
| 7 | +from homeassistant.components.device_tracker.config_entry import ScannerEntity |
| 8 | +from homeassistant.config_entries import ConfigEntry |
| 9 | +from homeassistant.const import CONF_NAME |
| 10 | +from homeassistant.core import callback |
| 11 | +from homeassistant.helpers import entity_registry |
| 12 | +from homeassistant.helpers.typing import HomeAssistantType |
| 13 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 14 | + |
| 15 | +from .const import CLIENTS, COORDINATOR, DOMAIN, UNDO_UPDATE_LISTENERS |
| 16 | + |
| 17 | + |
| 18 | +async def async_setup_entry( |
| 19 | + hass: HomeAssistantType, entry: ConfigEntry, async_add_entities |
| 20 | +) -> None: |
| 21 | + """Set up device tracker for Ruckus Unleashed component.""" |
| 22 | + coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR] |
| 23 | + |
| 24 | + tracked = set() |
| 25 | + |
| 26 | + @callback |
| 27 | + def router_update(): |
| 28 | + """Update the values of the router.""" |
| 29 | + add_new_entities(coordinator, async_add_entities, tracked) |
| 30 | + |
| 31 | + router_update() |
| 32 | + |
| 33 | + hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENERS].append( |
| 34 | + coordinator.async_add_listener(router_update) |
| 35 | + ) |
| 36 | + |
| 37 | + registry = await entity_registry.async_get_registry(hass) |
| 38 | + restore_entities(registry, coordinator, entry, async_add_entities, tracked) |
| 39 | + |
| 40 | + |
| 41 | +@callback |
| 42 | +def add_new_entities(coordinator, async_add_entities, tracked): |
| 43 | + """Add new tracker entities from the router.""" |
| 44 | + new_tracked = [] |
| 45 | + |
| 46 | + for mac in coordinator.data[CLIENTS].keys(): |
| 47 | + if mac in tracked: |
| 48 | + continue |
| 49 | + |
| 50 | + device = coordinator.data[CLIENTS][mac] |
| 51 | + new_tracked.append(RuckusUnleashedDevice(coordinator, mac, device[CONF_NAME])) |
| 52 | + tracked.add(mac) |
| 53 | + |
| 54 | + if new_tracked: |
| 55 | + async_add_entities(new_tracked, True) |
| 56 | + |
| 57 | + |
| 58 | +@callback |
| 59 | +def restore_entities(registry, coordinator, entry, async_add_entities, tracked): |
| 60 | + """Restore clients that are not a part of active clients list.""" |
| 61 | + missing = [] |
| 62 | + |
| 63 | + for entity in registry.entities.values(): |
| 64 | + if entity.config_entry_id == entry.entry_id and entity.platform == DOMAIN: |
| 65 | + if entity.unique_id not in coordinator.data[CLIENTS]: |
| 66 | + missing.append( |
| 67 | + RuckusUnleashedDevice( |
| 68 | + coordinator, entity.unique_id, entity.original_name |
| 69 | + ) |
| 70 | + ) |
| 71 | + tracked.add(entity.unique_id) |
| 72 | + |
| 73 | + if missing: |
| 74 | + async_add_entities(missing, True) |
| 75 | + |
| 76 | + |
| 77 | +class RuckusUnleashedDevice(CoordinatorEntity, ScannerEntity): |
| 78 | + """Representation of a Ruckus Unleashed client.""" |
| 79 | + |
| 80 | + def __init__(self, coordinator, mac, name) -> None: |
| 81 | + """Initialize a Ruckus Unleashed client.""" |
| 82 | + super().__init__(coordinator) |
| 83 | + self._mac = mac |
| 84 | + self._name = name |
| 85 | + |
| 86 | + @property |
| 87 | + def unique_id(self) -> str: |
| 88 | + """Return a unique ID.""" |
| 89 | + return self._mac |
| 90 | + |
| 91 | + @property |
| 92 | + def name(self) -> str: |
| 93 | + """Return the name.""" |
| 94 | + if self.is_connected: |
| 95 | + return ( |
| 96 | + self.coordinator.data[CLIENTS][self._mac][CONF_NAME] |
| 97 | + or f"Ruckus {self._mac}" |
| 98 | + ) |
| 99 | + return self._name |
| 100 | + |
| 101 | + @property |
| 102 | + def is_connected(self) -> bool: |
| 103 | + """Return true if the device is connected to the network.""" |
| 104 | + return self._mac in self.coordinator.data[CLIENTS] |
| 105 | + |
| 106 | + @property |
| 107 | + def source_type(self) -> str: |
| 108 | + """Return the source type.""" |
| 109 | + return SOURCE_TYPE_ROUTER |
| 110 | + |
| 111 | + @property |
| 112 | + def state_attributes(self) -> dict: |
| 113 | + """Return the state attributes.""" |
| 114 | + return { |
| 115 | + ATTR_SOURCE_TYPE: self.source_type, |
| 116 | + ATTR_MAC: self._mac, |
| 117 | + } |
0 commit comments