Skip to content

Instantly share code, notes, and snippets.

@evelant
Created May 7, 2016 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evelant/ace6271e097adcc62e824c2ec6184f51 to your computer and use it in GitHub Desktop.
Save evelant/ace6271e097adcc62e824c2ec6184f51 to your computer and use it in GitHub Desktop.
Grouped light platform for Home Assistant
import logging
# Import the device class from the component that you want to support
from homeassistant.components import light
from homeassistant.const import (STATE_OFF, STATE_ON)
CONF_NAME = 'name'
CONF_ENTITIES = 'entities'
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Initialize grouped light platform."""
name = config.get(CONF_NAME)
entity_ids = config.get(CONF_ENTITIES)
if name is None or entity_ids is None or len(entity_ids) == 0:
_LOGGER.error('Invalid config. Excepted %s and %s', CONF_NAME, CONF_ENTITIES)
return False
add_devices([GroupedLight(hass, name, entity_ids)])
class GroupedLight(light.Light):
"""Represents an Grouped Light in Home Assistant."""
def __init__(self, hass, name, entity_ids):
"""Initialize a Grouped Light."""
self.hass = hass
self._name = name
self._entity_ids = entity_ids
@property
def name(self):
return self._name
def _light_states(self):
"""The states that the group is tracking."""
states = []
for entity_id in self._entity_ids:
state = self.hass.states.get(entity_id)
_LOGGER.error("Got state for entity " + str(state))
if state is not None:
states.append(state)
return states
@property
def brightness(self):
"""Brightness of the light group"""
brightness = 0
for state in self._light_states():
if not 'brightness' in state.attributes:
return None
brightness += state.attributes.get('brightness')
brightness = brightness / float(len(self._entity_ids))
return brightness
@property
def color_temp(self):
"""Return the CT color value."""
for state in self._light_states():
if not 'color_temp' in state.attributes:
return None
return state.attributes.get('color_temp')
@property
def xy_color(self):
"""Return the XY color value."""
for state in self._light_states():
if not 'xy_color' in state.attributes:
return None
#return the first value we get since merging color values does not make sense
return state.attributes.get('xy_color')
@property
def is_on(self):
"""If light is on."""
for state in self._light_states():
_LOGGER.error("Checking is on " + str(state))
if state.state == STATE_ON:
return True
def turn_on(self, **kwargs):
"""Forward the turn_on command to all lights in the group"""
for entity_id in self._entity_ids:
light.turn_on(self.hass, entity_id, **kwargs)
def turn_off(self, **kwargs):
"""Forward the turn_off command to all lights in the group"""
for entity_id in self._entity_ids:
light.turn_off(self.hass, entity_id, **kwargs)
@jjensn
Copy link

jjensn commented Oct 25, 2016

Thanks. I don't know how this isn't an option.

@markeby
Copy link

markeby commented Nov 13, 2016

Please build this in and make a pull request so that it is included.

@claudioita
Copy link

would love to have it included in HA

@ajdezigns
Copy link

Where and how do I install this into my HA?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment