diff --git a/homeassistant/components/deconz/scene.py b/homeassistant/components/deconz/scene.py index 3d8e1aa27ba..9fcccc52386 100644 --- a/homeassistant/components/deconz/scene.py +++ b/homeassistant/components/deconz/scene.py @@ -7,7 +7,7 @@ from typing import Any from pydeconz.group import DeconzScene as PydeconzScene -from homeassistant.components.scene import Scene +from homeassistant.components.scene import DOMAIN, Scene from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -23,6 +23,7 @@ async def async_setup_entry( ) -> None: """Set up scenes for deCONZ component.""" gateway = get_gateway_from_config_entry(hass, config_entry) + gateway.entities[DOMAIN] = set() @callback def async_add_scene( @@ -30,7 +31,11 @@ async def async_setup_entry( | ValuesView[PydeconzScene] = gateway.api.scenes.values(), ) -> None: """Add scene from deCONZ.""" - entities = [DeconzScene(scene, gateway) for scene in scenes] + entities = [ + DeconzScene(scene, gateway) + for scene in scenes + if scene.deconz_id not in gateway.entities[DOMAIN] + ] if entities: async_add_entities(entities) @@ -59,10 +64,12 @@ class DeconzScene(Scene): async def async_added_to_hass(self) -> None: """Subscribe to sensors events.""" self.gateway.deconz_ids[self.entity_id] = self._scene.deconz_id + self.gateway.entities[DOMAIN].add(self._scene.deconz_id) async def async_will_remove_from_hass(self) -> None: """Disconnect scene object when removed.""" del self.gateway.deconz_ids[self.entity_id] + self.gateway.entities[DOMAIN].remove(self._scene.deconz_id) self._scene = None async def async_activate(self, **kwargs: Any) -> None: diff --git a/tests/components/deconz/test_diagnostics.py b/tests/components/deconz/test_diagnostics.py index 7d351a333cf..17da9f1141a 100644 --- a/tests/components/deconz/test_diagnostics.py +++ b/tests/components/deconz/test_diagnostics.py @@ -55,6 +55,7 @@ async def test_entry_diagnostics( str(Platform.LIGHT): [], str(Platform.LOCK): [], str(Platform.NUMBER): [], + str(Platform.SCENE): [], str(Platform.SENSOR): [], str(Platform.SIREN): [], str(Platform.SWITCH): [], diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index 189eb1e6eb7..e6f74cd0529 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -2,8 +2,10 @@ from unittest.mock import patch +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.helpers.dispatcher import async_dispatcher_send from .test_gateway import ( DECONZ_WEB_REQUEST, @@ -58,3 +60,30 @@ async def test_scenes(hass, aioclient_mock): await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 + + +async def test_only_new_scenes_are_created(hass, aioclient_mock): + """Test that scenes works.""" + data = { + "groups": { + "1": { + "id": "Light group id", + "name": "Light group", + "type": "LightGroup", + "state": {"all_on": False, "any_on": True}, + "action": {}, + "scenes": [{"id": "1", "name": "Scene"}], + "lights": [], + } + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) + + assert len(hass.states.async_all()) == 1 + + gateway = get_gateway_from_config_entry(hass, config_entry) + async_dispatcher_send(hass, gateway.signal_new_scene) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1