From 6655b7db2c158297bcc2f8e5a1f708188ab6bd2a Mon Sep 17 00:00:00 2001 From: Santobert Date: Fri, 1 Nov 2019 23:53:42 +0100 Subject: [PATCH] Add scene.create service (#28300) * Initial commit * Fix scene.create --- .../components/homeassistant/scene.py | 21 ++++++++++ homeassistant/components/scene/services.yaml | 14 +++++++ tests/components/homeassistant/test_scene.py | 39 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/homeassistant/components/homeassistant/scene.py b/homeassistant/components/homeassistant/scene.py index 39b04f6d3ea..45560d30edb 100644 --- a/homeassistant/components/homeassistant/scene.py +++ b/homeassistant/components/homeassistant/scene.py @@ -54,6 +54,8 @@ def _convert_states(states): return result +CONF_SCENE_ID = "scene_id" + STATES_SCHEMA = vol.All(dict, _convert_states) PLATFORM_SCHEMA = vol.Schema( @@ -72,7 +74,12 @@ PLATFORM_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) +CREATE_SCENE_SCHEMA = vol.Schema( + {vol.Required(CONF_SCENE_ID): cv.slug, vol.Required(CONF_ENTITIES): STATES_SCHEMA} +) + SERVICE_APPLY = "apply" +SERVICE_CREATE = "create" SCENECONFIG = namedtuple("SceneConfig", [CONF_NAME, STATES]) _LOGGER = logging.getLogger(__name__) @@ -129,6 +136,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= vol.Schema({vol.Required(CONF_ENTITIES): STATES_SCHEMA}), ) + async def create_service(call): + """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)]) + + hass.services.async_register( + SCENE_DOMAIN, SERVICE_CREATE, create_service, CREATE_SCENE_SCHEMA + ) + def _process_scenes_config(hass, async_add_entities, config): """Process multiple scenes and add them.""" diff --git a/homeassistant/components/scene/services.yaml b/homeassistant/components/scene/services.yaml index 0f1e7103aaf..9cf1b9010a8 100644 --- a/homeassistant/components/scene/services.yaml +++ b/homeassistant/components/scene/services.yaml @@ -20,3 +20,17 @@ apply: light.ceiling: state: "on" brightness: 80 + +create: + description: Creates a new scene. + fields: + scene_id: + description: The entity_id of the new scene. + example: all_lights + entities: + description: The entities to control with the scene. + example: + light.tv_back_light: "on" + light.ceiling: + state: "on" + brightness: 200 diff --git a/tests/components/homeassistant/test_scene.py b/tests/components/homeassistant/test_scene.py index c7c3f2bc5d5..08e40e23d12 100644 --- a/tests/components/homeassistant/test_scene.py +++ b/tests/components/homeassistant/test_scene.py @@ -51,3 +51,42 @@ async def test_apply_service(hass): state = hass.states.get("light.bed_light") assert state.state == "on" assert state.attributes["brightness"] == 50 + + +async def test_create_service(hass, caplog): + """Test the create service.""" + assert await async_setup_component(hass, "scene", {}) + assert hass.states.get("scene.hallo") is None + + assert await hass.services.async_call( + "scene", + "create", + { + "scene_id": "hallo", + "entities": {"light.bed_light": {"state": "on", "brightness": 50}}, + }, + 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.bed_light"] + + assert await hass.services.async_call( + "scene", + "create", + { + "scene_id": "hallo", + "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