Hi,
My first try at a custom component to save the state of a group as a scene.
It would be nice to add an icon to the group gui front-end to save the group as scene.
import logging
import yaml
from homeassistant.helpers import entity, service, event
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'scene'
ATTR_NAME = 'group'
ATTR_SCENE = 'scene'
GROUP = None
SCENE = None
def setup(hass, config):
"""Set up is called when Home Assistant is loading our component."""
def handle_save(call):
"""Handle the service call."""
base = "/config/scene/"
name = call.data.get(ATTR_NAME, GROUP)
scene = call.data.get(ATTR_SCENE, SCENE)
groupstate = hass.states.get(name)
if groupstate is not None and groupstate.state == "on" and groupstate.domain == "group":
attributes = groupstate.attributes
entity = groupstate.object_id
if scene is not None:
base = base + scene + ".yaml"
data = [{ 'name': scene.title(), 'entities': {} }]
else:
base = base + entity + ".yaml"
data = [{ 'name': groupstate.name, 'entities': {} }]
for id in attributes['entity_id']:
idstate = hass.states.get( id )
if idstate is not None:
attributes2 = idstate.attributes
if idstate.state == 'on':
data[0]['entities'][id] = {}
data[0]['entities'][id]['state'] = True
if 'brightness' in attributes2:
data[0]['entities'][id]['brightness'] = attributes2['brightness']
if 'rgb_color' in attributes2:
data[0]['entities'][id]['rgb_color'] = list(attributes2['rgb_color'])
elif 'color_temp' in attributes2:
data[0]['entities'][id]['color_temp'] = attributes2['color_temp']
else:
_LOGGER.info( 'No temp or rgb' )
else:
data[0]['entities'][id] = {}
data[0]['entities'][id]['state'] = False
scenefile = open(base, 'w')
yaml.dump(data, scenefile)
scenefile.close()
else:
_LOGGER.info( "Group is off" )
hass.services.register(DOMAIN, 'save', handle_save)
_LOGGER.info("The 'scene_save' component is ready!")
# Return boolean to indicate that initialization was successfully.
return True