diff --git a/homeassistant/components/homeassistant/scene.py b/homeassistant/components/homeassistant/scene.py index f011dae150f..c505d1534de 100644 --- a/homeassistant/components/homeassistant/scene.py +++ b/homeassistant/components/homeassistant/scene.py @@ -141,11 +141,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= """Create a scene.""" scene_config = SCENECONFIG(call.data[CONF_SCENE_ID], call.data[CONF_ENTITIES]) entity_id = f"{SCENE_DOMAIN}.{scene_config.name}" - if hass.states.get(entity_id) is not None: - _LOGGER.warning("The scene %s already exists", entity_id) - return - - async_add_entities([HomeAssistantScene(hass, scene_config)]) + old = platform.entities.get(entity_id) + if old is not None: + if not old.from_service: + _LOGGER.warning("The scene %s already exists", entity_id) + return + await platform.async_remove_entity(entity_id) + async_add_entities([HomeAssistantScene(hass, scene_config, from_service=True)]) hass.services.async_register( SCENE_DOMAIN, SERVICE_CREATE, create_service, CREATE_SCENE_SCHEMA @@ -173,11 +175,12 @@ def _process_scenes_config(hass, async_add_entities, config): class HomeAssistantScene(Scene): """A scene is a group of entities and the states we want them to be.""" - def __init__(self, hass, scene_config, scene_id=None): + def __init__(self, hass, scene_config, scene_id=None, from_service=False): """Initialize the scene.""" self._id = scene_id self.hass = hass self.scene_config = scene_config + self.from_service = from_service @property def name(self): diff --git a/tests/components/homeassistant/test_scene.py b/tests/components/homeassistant/test_scene.py index 08e40e23d12..25ce6088a51 100644 --- a/tests/components/homeassistant/test_scene.py +++ b/tests/components/homeassistant/test_scene.py @@ -55,8 +55,13 @@ async def test_apply_service(hass): async def test_create_service(hass, caplog): """Test the create service.""" - assert await async_setup_component(hass, "scene", {}) + assert await async_setup_component( + hass, + "scene", + {"scene": {"name": "hallo_2", "entities": {"light.kitchen": "on"}}}, + ) assert hass.states.get("scene.hallo") is None + assert hass.states.get("scene.hallo_2") is not None assert await hass.services.async_call( "scene", @@ -67,8 +72,8 @@ async def test_create_service(hass, caplog): }, blocking=True, ) - await hass.async_block_till_done() + scene = hass.states.get("scene.hallo") assert scene is not None assert scene.domain == "scene" @@ -81,12 +86,34 @@ async def test_create_service(hass, caplog): "create", { "scene_id": "hallo", + "entities": {"light.kitchen_light": {"state": "on", "brightness": 100}}, + }, + blocking=True, + ) + await hass.async_block_till_done() + + scene = hass.states.get("scene.hallo") + assert scene is not None + assert scene.domain == "scene" + assert scene.name == "hallo" + assert scene.state == "scening" + assert scene.attributes.get("entity_id") == ["light.kitchen_light"] + + assert await hass.services.async_call( + "scene", + "create", + { + "scene_id": "hallo_2", "entities": {"light.bed_light": {"state": "on", "brightness": 50}}, }, blocking=True, ) - await hass.async_block_till_done() - assert "The scene scene.hallo already exists" in caplog.text - assert hass.states.get("scene.hallo") is not None - assert hass.states.get("scene.hallo_2") is None + + assert "The scene scene.hallo_2 already exists" in caplog.text + scene = hass.states.get("scene.hallo_2") + assert scene is not None + assert scene.domain == "scene" + assert scene.name == "hallo_2" + assert scene.state == "scening" + assert scene.attributes.get("entity_id") == ["light.kitchen"]