Skip to content

Commit

Permalink
Remove unnecessary method in UniFi entity loader (home-assistant#113597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored Mar 16, 2024
1 parent ccd2e98 commit 00361f5
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions homeassistant/components/unifi/hub/entity_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from __future__ import annotations

import asyncio
from collections.abc import Iterable
from datetime import timedelta
from functools import partial
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -143,42 +142,36 @@ def _load_entities(
"""Subscribe to UniFi API handlers and create entities."""

@callback
def async_load_entities(descriptions: Iterable[UnifiEntityDescription]) -> None:
"""Load and subscribe to UniFi endpoints."""
def _add_unifi_entities() -> None:
"""Add UniFi entity."""
async_add_entities(
unifi_platform_entity(obj_id, self.hub, description)
for description in descriptions
for obj_id in description.api_handler_fn(self.hub.api)
if self._should_add_entity(description, obj_id)
)

@callback
def _add_unifi_entities() -> None:
"""Add UniFi entity."""
async_add_entities(
unifi_platform_entity(obj_id, self.hub, description)
for description in descriptions
for obj_id in description.api_handler_fn(self.hub.api)
if self._should_add_entity(description, obj_id)
)
_add_unifi_entities()

_add_unifi_entities()

@callback
def _create_unifi_entity(
description: UnifiEntityDescription, event: ItemEvent, obj_id: str
) -> None:
"""Create new UniFi entity on event."""
if self._should_add_entity(description, obj_id):
async_add_entities(
[unifi_platform_entity(obj_id, self.hub, description)]
)

for description in descriptions:
description.api_handler_fn(self.hub.api).subscribe(
partial(_create_unifi_entity, description), ItemEvent.ADDED
@callback
def _create_unifi_entity(
description: UnifiEntityDescription, event: ItemEvent, obj_id: str
) -> None:
"""Create new UniFi entity on event."""
if self._should_add_entity(description, obj_id):
async_add_entities(
[unifi_platform_entity(obj_id, self.hub, description)]
)

self.hub.config.entry.async_on_unload(
async_dispatcher_connect(
self.hub.hass,
self.hub.signal_options_update,
_add_unifi_entities,
)
for description in descriptions:
description.api_handler_fn(self.hub.api).subscribe(
partial(_create_unifi_entity, description), ItemEvent.ADDED
)

async_load_entities(descriptions)
self.hub.config.entry.async_on_unload(
async_dispatcher_connect(
self.hub.hass,
self.hub.signal_options_update,
_add_unifi_entities,
)
)

0 comments on commit 00361f5

Please sign in to comment.