Skip to content

Instantly share code, notes, and snippets.

@hordurk
Forked from evelant/grouped_light.py
Last active January 28, 2024 15:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hordurk/76458d5b6282857b80aed039dfbb6054 to your computer and use it in GitHub Desktop.
Save hordurk/76458d5b6282857b80aed039dfbb6054 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, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
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
return False
def turn_on(self, **kwargs):
"""Forward the turn_on command to all lights in the group"""
for entity_id in self._entity_ids:
kwargs[ATTR_ENTITY_ID] = entity_id
self.hass.services.call('light', SERVICE_TURN_ON, kwargs, blocking=True)
def turn_off(self, **kwargs):
"""Forward the turn_off command to all lights in the group"""
for entity_id in self._entity_ids:
kwargs[ATTR_ENTITY_ID] = entity_id
self.hass.services.call('light', SERVICE_TURN_OFF, kwargs, blocking=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment