Improve scene.create service (#28533)
* Improve scene.create service * Typo * Improve coverage * Add from_yaml attribute * from_service instead of from_yamlpull/28552/head
parent
fb0e20543e
commit
ef20f0985a
|
@ -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:
|
||||
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
|
||||
|
||||
async_add_entities([HomeAssistantScene(hass, scene_config)])
|
||||
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):
|
||||
|
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in New Issue