Improve scene.create service (#28533)

* Improve scene.create service

* Typo

* Improve coverage

* Add from_yaml attribute

* from_service instead of from_yaml
pull/28552/head
Santobert 2019-11-05 06:15:58 +01:00 committed by Paulus Schoutsen
parent fb0e20543e
commit ef20f0985a
2 changed files with 42 additions and 12 deletions

View File

@ -141,11 +141,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"""Create a scene.""" """Create a scene."""
scene_config = SCENECONFIG(call.data[CONF_SCENE_ID], call.data[CONF_ENTITIES]) scene_config = SCENECONFIG(call.data[CONF_SCENE_ID], call.data[CONF_ENTITIES])
entity_id = f"{SCENE_DOMAIN}.{scene_config.name}" entity_id = f"{SCENE_DOMAIN}.{scene_config.name}"
if hass.states.get(entity_id) is not None: 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) _LOGGER.warning("The scene %s already exists", entity_id)
return return
await platform.async_remove_entity(entity_id)
async_add_entities([HomeAssistantScene(hass, scene_config)]) async_add_entities([HomeAssistantScene(hass, scene_config, from_service=True)])
hass.services.async_register( hass.services.async_register(
SCENE_DOMAIN, SERVICE_CREATE, create_service, CREATE_SCENE_SCHEMA 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): class HomeAssistantScene(Scene):
"""A scene is a group of entities and the states we want them to be.""" """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.""" """Initialize the scene."""
self._id = scene_id self._id = scene_id
self.hass = hass self.hass = hass
self.scene_config = scene_config self.scene_config = scene_config
self.from_service = from_service
@property @property
def name(self): def name(self):

View File

@ -55,8 +55,13 @@ async def test_apply_service(hass):
async def test_create_service(hass, caplog): async def test_create_service(hass, caplog):
"""Test the create service.""" """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") is None
assert hass.states.get("scene.hallo_2") is not None
assert await hass.services.async_call( assert await hass.services.async_call(
"scene", "scene",
@ -67,8 +72,8 @@ async def test_create_service(hass, caplog):
}, },
blocking=True, blocking=True,
) )
await hass.async_block_till_done() await hass.async_block_till_done()
scene = hass.states.get("scene.hallo") scene = hass.states.get("scene.hallo")
assert scene is not None assert scene is not None
assert scene.domain == "scene" assert scene.domain == "scene"
@ -81,12 +86,34 @@ async def test_create_service(hass, caplog):
"create", "create",
{ {
"scene_id": "hallo", "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}}, "entities": {"light.bed_light": {"state": "on", "brightness": 50}},
}, },
blocking=True, blocking=True,
) )
await hass.async_block_till_done() 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 "The scene scene.hallo_2 already exists" in caplog.text
assert hass.states.get("scene.hallo_2") is None 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"]