Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions homeassistant/components/homeassistant/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -72,7 +74,12 @@ def _convert_states(states):
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__)

Expand Down Expand Up @@ -129,6 +136,20 @@ async def apply_service(call):
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."""
Expand Down
14 changes: 14 additions & 0 deletions homeassistant/components/scene/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment thread
Santobert marked this conversation as resolved.
light.tv_back_light: "on"
light.ceiling:
state: "on"
brightness: 200
39 changes: 39 additions & 0 deletions tests/components/homeassistant/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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