Ok, it took longer than expected. When they say, “The scripts are run in a sandboxed environment,” they’re not kidding!
In any case, you can now pass the following data to the scripts, both of which are optional:
store_name - Domain name used for switch/light store. Defaults to "light_store".
entity_id - Entity IDs of switches and/or lights to save/restore.
Defaults to all existing switches and lights.
Note that entity_id can be a single item, a list of items, or a comma separated string. E.g.:
"entity_id": "light.bulb1"
"entity_id": ["switch.switch1", "light.bulb1"]
"entity_id": "switch.switch1, light.bulb1"
Updated save_lights.py:
DOMAIN = 'light_store'
# Select light attributes to save.
ATTR_BRIGHTNESS = "brightness"
ATTR_HS_COLOR = "hs_color"
LIGHT_ATTRS = [ATTR_BRIGHTNESS, ATTR_HS_COLOR]
def store_entity_id(store_name, entity_id):
return '{}.{}'.format(store_name, entity_id.replace('.', '_'))
# Get optional store name (default to DOMAIN.)
store_name = data.get('store_name', DOMAIN)
# Get optional list (or comma separated string) of switches & lights to
# save. If not provided, default to all.
entity_id = data.get('entity_id')
if isinstance(entity_id, str):
entity_id = [e.strip() for e in entity_id.split(',')]
# Get lists of switches and lights to save that actually exist.
switches = hass.states.entity_ids('switch')
lights = hass.states.entity_ids('light')
if entity_id:
switches = tuple(set(entity_id).intersection(set(switches)))
lights = tuple(set(entity_id).intersection(set(lights)))
# Clear out any previously saved states.
saved = hass.states.entity_ids(store_name)
for entity_id in saved:
hass.states.remove(entity_id)
for entity_id in switches:
cur_state = hass.states.get(entity_id)
if cur_state is None:
logger.error('Could not get current state for {}.'.format(entity_id))
else:
hass.states.set(store_entity_id(store_name, entity_id), cur_state.state)
for entity_id in lights:
cur_state = hass.states.get(entity_id)
if cur_state is None:
logger.error('Could not get current state for {}.'.format(entity_id))
else:
attributes = {}
for attr in LIGHT_ATTRS:
value = cur_state.attributes.get(attr)
if value is not None:
attributes[attr] = value
hass.states.set(store_entity_id(store_name, entity_id), cur_state.state,
attributes)
Updated restore_lights.py:
DOMAIN = 'light_store'
# Select light attributes to restore.
ATTR_BRIGHTNESS = "brightness"
ATTR_HS_COLOR = "hs_color"
LIGHT_ATTRS = [ATTR_BRIGHTNESS, ATTR_HS_COLOR]
def store_entity_id(store_name, entity_id):
return '{}.{}'.format(store_name, entity_id.replace('.', '_'))
# Get optional store name (default to DOMAIN.)
store_name = data.get('store_name', DOMAIN)
# Get optional list (or comma separated string) of switches & lights to
# restore. If not provided, default to all.
entity_id = data.get('entity_id')
if isinstance(entity_id, str):
entity_id = [e.strip() for e in entity_id.split(',')]
# Get lists of switches and lights to restore that actually exist.
switches = hass.states.entity_ids('switch')
lights = hass.states.entity_ids('light')
if entity_id:
switches = tuple(set(entity_id).intersection(set(switches)))
lights = tuple(set(entity_id).intersection(set(lights)))
# Retrieve saved states.
saved = hass.states.entity_ids(store_name)
for entity_id in switches:
old_state = hass.states.get(store_entity_id(store_name, entity_id))
if old_state is None:
logger.error('No saved state for {}.'.format(entity_id))
else:
turn_on = old_state.state == 'on'
service_data = {'entity_id': entity_id}
hass.services.call('switch', 'turn_on' if turn_on else 'turn_off',
service_data)
for entity_id in lights:
old_state = hass.states.get(store_entity_id(store_name, entity_id))
if old_state is None:
logger.error('No saved state for {}.'.format(entity_id))
else:
turn_on = old_state.state == 'on'
service_data = {'entity_id': entity_id}
if turn_on:
for attr in LIGHT_ATTRS:
value = old_state.attributes.get(attr)
if value is not None:
service_data[attr] = value
hass.services.call('light', 'turn_on' if turn_on else 'turn_off',
service_data)
# Remove saved states now that we're done with them.
for entity_id in saved:
hass.states.remove(entity_id)
Hope you find this useful!