Skip to content

Commit

Permalink
Light updates (#110)
Browse files Browse the repository at this point in the history
* Attempt to upgrade the lights.

* Attempt to upgrade the lights.
  • Loading branch information
twrecked authored Jun 28, 2024
1 parent d0b2654 commit 734e07f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
0.9.0b11:
Fix up light support.
Fix race condition in meta data write
Fix reload issue that just doubled up the devices
0.9.0b10:
Expand Down
50 changes: 27 additions & 23 deletions custom_components/virtual/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
ColorMode,
DOMAIN as PLATFORM_DOMAIN,
LightEntity,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
LightEntityFeature,
SUPPORT_EFFECT,
)
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -103,18 +101,20 @@ def __init__(self, config):
"""Initialize a Virtual light."""
super().__init__(config, PLATFORM_DOMAIN)

self._attr_supported_color_modes = {ColorMode.ONOFF}
self._attr_supported_features = 0
self._attr_supported_features = LightEntityFeature(0)
self._attr_supported_color_modes = set()
self._attr_color_mode = ColorMode.UNKNOWN

if config.get(CONF_SUPPORT_BRIGHTNESS):
self._attr_supported_features |= SUPPORT_BRIGHTNESS
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
if config.get(CONF_SUPPORT_COLOR):
self._attr_supported_features |= SUPPORT_COLOR
self._attr_supported_color_modes.add(ColorMode.HS)
if config.get(CONF_SUPPORT_COLOR_TEMP):
self._attr_supported_features |= SUPPORT_COLOR_TEMP
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
if config.get(CONF_SUPPORT_COLOR):
self._attr_supported_color_modes.add(ColorMode.HS)
if config.get(CONF_SUPPORT_BRIGHTNESS):
if not self._attr_supported_color_modes:
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
if not self._attr_supported_color_modes:
self._attr_supported_color_modes.add(ColorMode.ONOFF)

if config.get(CONF_SUPPORT_EFFECT):
self._attr_supported_features |= SUPPORT_EFFECT
self._attr_effect_list = self._config.get(CONF_INITIAL_EFFECT_LIST)
Expand All @@ -124,14 +124,17 @@ def _create_state(self, config):

self._attr_is_on = config.get(CONF_INITIAL_VALUE).lower() == STATE_ON

if self._attr_supported_features & SUPPORT_BRIGHTNESS:
if ColorMode.BRIGHTNESS in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.BRIGHTNESS
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
if self._attr_supported_features & SUPPORT_COLOR:
if ColorMode.HS in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.HS
self._attr_hs_color = config.get(CONF_INITIAL_COLOR)
if self._attr_supported_features & SUPPORT_COLOR_TEMP:
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.COLOR_TEMP
self._attr_color_temp = config.get(CONF_INITIAL_COLOR_TEMP)
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
if self._attr_supported_features & SUPPORT_EFFECT:
self._attr_effect = config.get(CONF_INITIAL_EFFECT)

Expand All @@ -140,20 +143,23 @@ def _restore_state(self, state, config):

self._attr_is_on = state.state.lower() == STATE_ON

if self._attr_supported_features & SUPPORT_BRIGHTNESS:
if ColorMode.BRIGHTNESS in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.BRIGHTNESS
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
if self._attr_supported_features & SUPPORT_COLOR:
if ColorMode.HS in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.HS
self._attr_hs_color = state.attributes.get(ATTR_HS_COLOR, config.get(CONF_INITIAL_COLOR))
if self._attr_supported_features & SUPPORT_COLOR_TEMP:
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._attr_color_mode = ColorMode.COLOR_TEMP
self._attr_color_temp = state.attributes.get(ATTR_COLOR_TEMP, config.get(CONF_INITIAL_COLOR_TEMP))
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
if self._attr_supported_features & SUPPORT_EFFECT:
self._attr_effect = state.attributes.get(ATTR_EFFECT, config.get(CONF_INITIAL_EFFECT))

def _update_attributes(self):
"""Return the state attributes."""
super()._update_attributes();
super()._update_attributes()
self._attr_extra_state_attributes.update({
name: value for name, value in (
(ATTR_BRIGHTNESS, self._attr_brightness),
Expand All @@ -169,14 +175,12 @@ def turn_on(self, **kwargs):
"""Turn the light on."""
_LOGGER.debug(f"turning {self.name} on {pprint.pformat(kwargs)}")
hs_color = kwargs.get(ATTR_HS_COLOR, None)
if hs_color is not None and self._attr_supported_features & SUPPORT_COLOR:
self._attr_color_mode = ColorMode.HS
if hs_color is not None and ColorMode.HS in self._attr_supported_color_modes:
self._attr_hs_color = hs_color
self._attr_color_temp = None

ct = kwargs.get(ATTR_COLOR_TEMP, None)
if ct is not None and self._attr_supported_features & SUPPORT_COLOR_TEMP:
self._attr_color_mode = ColorMode.COLOR_TEMP
if ct is not None and ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._attr_color_temp = ct
self._attr_hs_color = None

Expand Down

0 comments on commit 734e07f

Please sign in to comment.