Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions homeassistant/components/deconz/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dataclasses import dataclass

from pydeconz.models.event import EventType
from pydeconz.models.scene import Scene as PydeconzScene

from homeassistant.components.button import (
Expand All @@ -13,7 +14,6 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand Down Expand Up @@ -57,34 +57,23 @@ async def async_setup_entry(
gateway.entities[DOMAIN] = set()

@callback
def async_add_scene(scenes: list[PydeconzScene] | None = None) -> None:
def async_add_scene(_: EventType, scene_id: str) -> None:
"""Add scene button from deCONZ."""
entities = []

if scenes is None:
scenes = list(gateway.api.scenes.values())

for scene in scenes:

known_entities = set(gateway.entities[DOMAIN])
for description in ENTITY_DESCRIPTIONS.get(PydeconzScene, []):

new_entity = DeconzButton(scene, gateway, description)
if new_entity.unique_id not in known_entities:
entities.append(new_entity)

if entities:
async_add_entities(entities)
scene = gateway.api.scenes[scene_id]
async_add_entities(
DeconzButton(scene, gateway, description)
for description in ENTITY_DESCRIPTIONS.get(PydeconzScene, [])
)

config_entry.async_on_unload(
async_dispatcher_connect(
hass,
gateway.signal_new_scene,
gateway.api.scenes.subscribe(
async_add_scene,
EventType.ADDED,
)
)

async_add_scene()
for scene_id in gateway.api.scenes:
async_add_scene(EventType.ADDED, scene_id)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there will ever be a lot of scenes at startup....you could make async_add_scene a wrapper around new async_add_scenes and accept multiple so you only call async_add_entities once with all the entities

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it that big of a deal for upstart? I'm not questioning your reasoning but Id really like to understand what you're suggesting

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time you call async_add_entities its going to call https://github.com/home-assistant/core/blob/dev/homeassistant/helpers/entity_platform.py#L326 which creates a task in the event loop.

If you had 100s of scenes (hopefully someone doesn't actually have that many), this can start to add up quickly

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say for other users. But I could definitely see myself using maybe 4 scenes per group that are relevant to control over various times of the day. So maybe at least 40 scenes would be expected.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will consider this going forward



class DeconzButton(DeconzSceneMixin, ButtonEntity):
Expand Down