async def async_setup_entry(hass, config_entry, async_add_entities): """Set up sensors for UniFi integration.""" controller = get_controller_from_config_entry(hass, config_entry) sensors = {} registry = await entity_registry.async_get_registry(hass) @callback def update_controller(): """Update the values of the controller.""" update_items(controller, async_add_entities, sensors) async_dispatcher_connect(hass, controller.signal_update, update_controller) @callback def update_disable_on_entities(): """Update the values of the controller.""" for entity in sensors.values(): disabled_by = None if not entity.entity_registry_enabled_default and entity.enabled: disabled_by = DISABLED_CONFIG_ENTRY registry.async_update_entity(entity.registry_entry.entity_id, disabled_by=disabled_by) async_dispatcher_connect(hass, controller.signal_options_update, update_disable_on_entities) update_controller()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up device tracker for UniFi component.""" controller = get_controller_from_config_entry(hass, config_entry) tracked = {} registry = await entity_registry.async_get_registry(hass) # Restore clients that is not a part of active clients list. for entity in registry.entities.values(): if ( entity.config_entry_id == config_entry.entry_id and entity.domain == DEVICE_TRACKER_DOMAIN and "-" in entity.unique_id ): mac, _ = entity.unique_id.split("-", 1) if mac in controller.api.clients or mac not in controller.api.clients_all: continue client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) @callback def update_controller(): """Update the values of the controller.""" update_items(controller, async_add_entities, tracked) controller.listeners.append( async_dispatcher_connect(hass, controller.signal_update, update_controller) ) @callback def update_disable_on_entities(): """Update the values of the controller.""" for entity in tracked.values(): if entity.entity_registry_enabled_default == entity.enabled: continue disabled_by = None if not entity.entity_registry_enabled_default and entity.enabled: disabled_by = DISABLED_CONFIG_ENTRY registry.async_update_entity( entity.registry_entry.entity_id, disabled_by=disabled_by ) controller.listeners.append( async_dispatcher_connect( hass, controller.signal_options_update, update_disable_on_entities ) ) update_controller()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up sensors for UniFi integration.""" controller = get_controller_from_config_entry(hass, config_entry) sensors = {} option_allow_bandwidth_sensors = controller.option_allow_bandwidth_sensors entity_registry = await hass.helpers.entity_registry.async_get_registry() @callback def update_controller(): """Update the values of the controller.""" nonlocal option_allow_bandwidth_sensors if not option_allow_bandwidth_sensors: return add_entities(controller, async_add_entities, sensors) controller.listeners.append( async_dispatcher_connect(hass, controller.signal_update, update_controller) ) @callback def options_updated(): """Update the values of the controller.""" nonlocal option_allow_bandwidth_sensors if option_allow_bandwidth_sensors != controller.option_allow_bandwidth_sensors: option_allow_bandwidth_sensors = controller.option_allow_bandwidth_sensors if option_allow_bandwidth_sensors: update_controller() else: for sensor in sensors.values(): if entity_registry.async_is_registered(sensor.entity_id): entity_registry.async_remove(sensor.entity_id) hass.async_create_task(sensor.async_remove()) sensors.clear() controller.listeners.append( async_dispatcher_connect( hass, controller.signal_options_update, options_updated ) ) update_controller()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up sensors for UniFi integration.""" controller = get_controller_from_config_entry(hass, config_entry) controller.entities[DOMAIN] = {RX_SENSOR: set(), TX_SENSOR: set()} @callback def items_added(): """Update the values of the controller.""" if controller.option_allow_bandwidth_sensors: add_entities(controller, async_add_entities) for signal in (controller.signal_update, controller.signal_options_update): controller.listeners.append( async_dispatcher_connect(hass, signal, items_added)) items_added()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up switches for UniFi component. Switches are controlling network switch ports with Poe. """ controller = get_controller_from_config_entry(hass, config_entry) if controller.site_role != "admin": return switches = {} switches_off = [] registry = await entity_registry.async_get_registry(hass) # Restore clients that is not a part of active clients list. for entity in registry.entities.values(): if (entity.config_entry_id == config_entry.entry_id and entity.unique_id.startswith("poe-")): _, mac = entity.unique_id.split("-", 1) if mac in controller.api.clients: switches_off.append(entity.unique_id) continue if mac in controller.api.clients_all: client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) switches_off.append(entity.unique_id) continue @callback def update_controller(): """Update the values of the controller.""" update_items(controller, async_add_entities, switches, switches_off) controller.listeners.append( async_dispatcher_connect(hass, controller.signal_update, update_controller)) update_controller() switches_off.clear()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up switches for UniFi component. Switches are controlling network access and switch ports with POE. """ controller = get_controller_from_config_entry(hass, config_entry) controller.entities[DOMAIN] = {BLOCK_SWITCH: set(), POE_SWITCH: set()} if controller.site_role != "admin": return switches_off = [] # Restore clients that is not a part of active clients list. entity_registry = await hass.helpers.entity_registry.async_get_registry() for entity in entity_registry.entities.values(): if (entity.config_entry_id == config_entry.entry_id and entity.unique_id.startswith(f"{POE_SWITCH}-")): _, mac = entity.unique_id.split("-", 1) if mac in controller.api.clients: switches_off.append(entity.unique_id) continue if mac in controller.api.clients_all: client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) switches_off.append(entity.unique_id) continue @callback def items_added(): """Update the values of the controller.""" if controller.option_block_clients or controller.option_poe_clients: add_entities(controller, async_add_entities, switches_off) for signal in (controller.signal_update, controller.signal_options_update): controller.listeners.append( async_dispatcher_connect(hass, signal, items_added)) items_added() switches_off.clear()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up device tracker for UniFi component.""" controller = get_controller_from_config_entry(hass, config_entry) controller.entities[DOMAIN] = { CLIENT_TRACKER: set(), DEVICE_TRACKER: set() } # Restore clients that is not a part of active clients list. entity_registry = await hass.helpers.entity_registry.async_get_registry() for entity in entity_registry.entities.values(): if (entity.config_entry_id == config_entry.entry_id and entity.domain == DOMAIN and "-" in entity.unique_id): mac, _ = entity.unique_id.split("-", 1) if mac in controller.api.clients or mac not in controller.api.clients_all: continue client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) LOGGER.debug( "Restore disconnected client %s (%s)", entity.entity_id, client.mac, ) @callback def items_added(): """Update the values of the controller.""" if controller.option_track_clients or controller.option_track_devices: add_entities(controller, async_add_entities) for signal in (controller.signal_update, controller.signal_options_update): controller.listeners.append( async_dispatcher_connect(hass, signal, items_added)) items_added()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up device tracker for UniFi component.""" controller = get_controller_from_config_entry(hass, config_entry) tracked = {} option_track_clients = controller.option_track_clients option_track_devices = controller.option_track_devices option_track_wired_clients = controller.option_track_wired_clients registry = await hass.helpers.entity_registry.async_get_registry() # Restore clients that is not a part of active clients list. for entity in registry.entities.values(): if (entity.config_entry_id == config_entry.entry_id and entity.domain == DEVICE_TRACKER_DOMAIN and "-" in entity.unique_id): mac, _ = entity.unique_id.split("-", 1) if mac in controller.api.clients or mac not in controller.api.clients_all: continue client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) @callback def update_controller(): """Update the values of the controller.""" nonlocal option_track_clients nonlocal option_track_devices if not option_track_clients and not option_track_devices: return add_entities(controller, async_add_entities, tracked) controller.listeners.append( async_dispatcher_connect(hass, controller.signal_update, update_controller)) @callback def options_updated(): """Manage entities affected by config entry options.""" nonlocal option_track_clients nonlocal option_track_devices nonlocal option_track_wired_clients update = False remove = set() for current_option, config_entry_option, tracker_class in ( (option_track_clients, controller.option_track_clients, UniFiClientTracker), (option_track_devices, controller.option_track_devices, UniFiDeviceTracker), ): if current_option == config_entry_option: continue if config_entry_option: update = True else: for mac, entity in tracked.items(): if isinstance(entity, tracker_class): remove.add(mac) if (controller.option_track_clients and option_track_wired_clients != controller.option_track_wired_clients): if controller.option_track_wired_clients: update = True else: for mac, entity in tracked.items(): if isinstance(entity, UniFiClientTracker) and entity.is_wired: remove.add(mac) option_track_clients = controller.option_track_clients option_track_devices = controller.option_track_devices option_track_wired_clients = controller.option_track_wired_clients for mac in remove: entity = tracked.pop(mac) if registry.async_is_registered(entity.entity_id): registry.async_remove(entity.entity_id) hass.async_create_task(entity.async_remove()) if update: update_controller() controller.listeners.append( async_dispatcher_connect(hass, controller.signal_options_update, options_updated)) update_controller()
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up switches for UniFi component. Switches are controlling network access and switch ports with POE. """ controller = get_controller_from_config_entry(hass, config_entry) if controller.site_role != "admin": return switches = {} switches_off = [] option_block_clients = controller.option_block_clients entity_registry = await hass.helpers.entity_registry.async_get_registry() # Restore clients that is not a part of active clients list. for entity in entity_registry.entities.values(): if ( entity.config_entry_id == config_entry.entry_id and entity.unique_id.startswith("poe-") ): _, mac = entity.unique_id.split("-", 1) if mac in controller.api.clients: switches_off.append(entity.unique_id) continue if mac in controller.api.clients_all: client = controller.api.clients_all[mac] controller.api.clients.process_raw([client.raw]) switches_off.append(entity.unique_id) continue @callback def update_controller(): """Update the values of the controller.""" add_entities(controller, async_add_entities, switches, switches_off) controller.listeners.append( async_dispatcher_connect(hass, controller.signal_update, update_controller) ) @callback def options_updated(): """Manage entities affected by config entry options.""" nonlocal option_block_clients update = set() remove = set() if option_block_clients != controller.option_block_clients: option_block_clients = controller.option_block_clients for block_client_id, entity in switches.items(): if not isinstance(entity, UniFiBlockClientSwitch): continue if entity.client.mac in option_block_clients: update.add(block_client_id) else: remove.add(block_client_id) for block_client_id in remove: entity = switches.pop(block_client_id) if entity_registry.async_is_registered(entity.entity_id): entity_registry.async_remove(entity.entity_id) hass.async_create_task(entity.async_remove()) if len(update) != len(option_block_clients): update_controller() controller.listeners.append( async_dispatcher_connect( hass, controller.signal_options_update, options_updated ) ) update_controller() switches_off.clear()