Add config endpoint for scene (#28429)
* Add config endpoint for scene * Add scenes to default config * Fix test and add context to websocket service call * Update scene.py * Add tests
This commit is contained in:
1 parent
bbe0cf3a0c
commit
e419689229
7 files changed
+235
-4
No files matched your search
@@ -25,6 +25,7 @@ SECTIONS = (
|
||||
"entity_registry",
|
||||
"group",
|
||||
"script",
|
||||
"scene",
|
||||
)
|
||||
ON_DEMAND = ("zwave",)
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Provide configuration end points for Scenes."""
|
||||
from collections import OrderedDict
|
||||
import uuid
|
||||
|
||||
from homeassistant.components.scene import DOMAIN, PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
||||
from homeassistant.config import SCENE_CONFIG_PATH
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from . import EditIdBasedConfigView
|
||||
|
||||
|
||||
async def async_setup(hass):
|
||||
"""Set up the Scene config API."""
|
||||
|
||||
async def hook(hass):
|
||||
"""post_write_hook for Config View that reloads scenes."""
|
||||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
|
||||
|
||||
hass.http.register_view(
|
||||
EditSceneConfigView(
|
||||
DOMAIN,
|
||||
"config",
|
||||
SCENE_CONFIG_PATH,
|
||||
cv.string,
|
||||
PLATFORM_SCHEMA,
|
||||
post_write_hook=hook,
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
class EditSceneConfigView(EditIdBasedConfigView):
|
||||
"""Edit scene config."""
|
||||
|
||||
def _write_value(self, hass, data, config_key, new_value):
|
||||
"""Set value."""
|
||||
index = None
|
||||
for index, cur_value in enumerate(data):
|
||||
# When people copy paste their scenes to the config file,
|
||||
# they sometimes forget to add IDs. Fix it here.
|
||||
if CONF_ID not in cur_value:
|
||||
cur_value[CONF_ID] = uuid.uuid4().hex
|
||||
|
||||
elif cur_value[CONF_ID] == config_key:
|
||||
break
|
||||
else:
|
||||
cur_value = dict()
|
||||
cur_value[CONF_ID] = config_key
|
||||
index = len(data)
|
||||
data.append(cur_value)
|
||||
|
||||
# Iterate through some keys that we want to have ordered in the output
|
||||
updated_value = OrderedDict()
|
||||
for key in ("id", "name", "entities"):
|
||||
if key in cur_value:
|
||||
updated_value[key] = cur_value[key]
|
||||
if key in new_value:
|
||||
updated_value[key] = new_value[key]
|
||||
|
||||
# We cover all current fields above, but just in case we start
|
||||
# supporting more fields in the future.
|
||||
updated_value.update(cur_value)
|
||||
updated_value.update(new_value)
|
||||
data[index] = updated_value
|
||||
@@ -8,6 +8,7 @@ from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_STATE,
|
||||
CONF_ENTITIES,
|
||||
CONF_ID,
|
||||
CONF_NAME,
|
||||
CONF_PLATFORM,
|
||||
STATE_OFF,
|
||||
@@ -160,7 +161,11 @@ def _process_scenes_config(hass, async_add_entities, config):
|
||||
return
|
||||
|
||||
async_add_entities(
|
||||
HomeAssistantScene(hass, SCENECONFIG(scene[CONF_NAME], scene[CONF_ENTITIES]))
|
||||
HomeAssistantScene(
|
||||
hass,
|
||||
SCENECONFIG(scene[CONF_NAME], scene[CONF_ENTITIES]),
|
||||
scene.get(CONF_ID),
|
||||
)
|
||||
for scene in scene_config
|
||||
)
|
||||
|
||||
@@ -168,8 +173,9 @@ 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):
|
||||
def __init__(self, hass, scene_config, scene_id=None):
|
||||
"""Initialize the scene."""
|
||||
self._id = scene_id
|
||||
self.hass = hass
|
||||
self.scene_config = scene_config
|
||||
|
||||
@@ -181,7 +187,10 @@ class HomeAssistantScene(Scene):
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the scene state attributes."""
|
||||
return {ATTR_ENTITY_ID: list(self.scene_config.states)}
|
||||
attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)}
|
||||
if self._id is not None:
|
||||
attributes[CONF_ID] = self._id
|
||||
return attributes
|
||||
|
||||
async def async_activate(self):
|
||||
"""Activate scene. Try to get entities into requested state."""
|
||||
|
||||
@@ -132,7 +132,9 @@ async def handle_call_service(hass, connection, msg):
|
||||
blocking,
|
||||
connection.context(msg),
|
||||
)
|
||||
connection.send_message(messages.result_message(msg["id"]))
|
||||
connection.send_message(
|
||||
messages.result_message(msg["id"], {"context": connection.context(msg)})
|
||||
)
|
||||
except ServiceNotFound as err:
|
||||
if err.domain == msg["domain"] and err.service == msg["service"]:
|
||||
connection.send_message(
|
||||
|
||||
Reference in new issue
Block a user