From 0726bc2cabd98639214e2cd14c49d30262e75d5e Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sun, 24 Apr 2022 22:58:36 +0200 Subject: [PATCH] Streamline setup of deCONZ button platform (#70593) * Streamline setup of deCONZ button platform * Update homeassistant/components/deconz/button.py Co-authored-by: J. Nick Koston Co-authored-by: J. Nick Koston --- homeassistant/components/deconz/button.py | 33 ++++++++--------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/deconz/button.py b/homeassistant/components/deconz/button.py index d5e6351b599..ed61750af6f 100644 --- a/homeassistant/components/deconz/button.py +++ b/homeassistant/components/deconz/button.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass +from pydeconz.models.event import EventType from pydeconz.models.scene import Scene as PydeconzScene from homeassistant.components.button import ( @@ -13,7 +14,6 @@ from homeassistant.components.button import ( ) 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 @@ -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) class DeconzButton(DeconzSceneMixin, ButtonEntity):