So here’s an update that stores multiple attributes for lights (and makes it easy to add others. See below.)
FWIW, the light component seems to translate all color variants (xy_color, rgb_color, color_name) into hs_color (and back again when reporting attributes.) So I figured it was best to save/restore hs_color, but of course, feel free to modify the scripts if you feel otherwise.
I’ll work on adding support for a list of entitiy_id’s to save/restore next.
Updated save_lights.py:
DOMAIN = 'light_store'
STORE_ENTITY_ID = '{}.{{}}'.format(DOMAIN)
# Select light attributes to save/restore.
ATTR_BRIGHTNESS = "brightness"
ATTR_HS_COLOR = "hs_color"
LIGHT_ATTRS = [ATTR_BRIGHTNESS, ATTR_HS_COLOR]
def store_entity_id(entity_id):
global STORE_ENTITY_ID
return STORE_ENTITY_ID.format(entity_id.replace('.', '_'))
# Clear out any previously saved states.
saved = hass.states.entity_ids(DOMAIN)
for entity_id in saved:
hass.states.remove(entity_id)
all_switches = hass.states.entity_ids('switch')
all_lights = hass.states.entity_ids('light')
for entity_id in all_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(entity_id), cur_state.state)
for entity_id in all_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(entity_id), cur_state.state, attributes)
Updated restore_lights.py:
DOMAIN = 'light_store'
STORE_ENTITY_ID = '{}.{{}}'.format(DOMAIN)
# Select light attributes to save/restore.
ATTR_BRIGHTNESS = "brightness"
ATTR_HS_COLOR = "hs_color"
LIGHT_ATTRS = [ATTR_BRIGHTNESS, ATTR_HS_COLOR]
def store_entity_id(entity_id):
global STORE_ENTITY_ID
return STORE_ENTITY_ID.format(entity_id.replace('.', '_'))
# Retrieve saved states.
saved = hass.states.entity_ids(DOMAIN)
all_switches = hass.states.entity_ids('switch')
all_lights = hass.states.entity_ids('light')
for entity_id in all_switches:
old_state = hass.states.get(store_entity_id(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 all_lights:
old_state = hass.states.get(store_entity_id(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)