parent
c0e1b97119
commit
6655b7db2c
|
@ -54,6 +54,8 @@ def _convert_states(states):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
CONF_SCENE_ID = "scene_id"
|
||||||
|
|
||||||
STATES_SCHEMA = vol.All(dict, _convert_states)
|
STATES_SCHEMA = vol.All(dict, _convert_states)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = vol.Schema(
|
PLATFORM_SCHEMA = vol.Schema(
|
||||||
|
@ -72,7 +74,12 @@ PLATFORM_SCHEMA = vol.Schema(
|
||||||
extra=vol.ALLOW_EXTRA,
|
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_APPLY = "apply"
|
||||||
|
SERVICE_CREATE = "create"
|
||||||
SCENECONFIG = namedtuple("SceneConfig", [CONF_NAME, STATES])
|
SCENECONFIG = namedtuple("SceneConfig", [CONF_NAME, STATES])
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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}),
|
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):
|
def _process_scenes_config(hass, async_add_entities, config):
|
||||||
"""Process multiple scenes and add them."""
|
"""Process multiple scenes and add them."""
|
||||||
|
|
|
@ -20,3 +20,17 @@ apply:
|
||||||
light.ceiling:
|
light.ceiling:
|
||||||
state: "on"
|
state: "on"
|
||||||
brightness: 80
|
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
|
||||||
|
|
|
@ -51,3 +51,42 @@ async def test_apply_service(hass):
|
||||||
state = hass.states.get("light.bed_light")
|
state = hass.states.get("light.bed_light")
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
assert state.attributes["brightness"] == 50
|
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
|
||||||
|
|
Loading…
Reference in New Issue