Skip to content

Commit 734e07f

Browse files
authored
Light updates (#110)
* Attempt to upgrade the lights. * Attempt to upgrade the lights.
1 parent d0b2654 commit 734e07f

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.9.0b11:
2+
Fix up light support.
23
Fix race condition in meta data write
34
Fix reload issue that just doubled up the devices
45
0.9.0b10:

custom_components/virtual/light.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
ColorMode,
2121
DOMAIN as PLATFORM_DOMAIN,
2222
LightEntity,
23-
SUPPORT_BRIGHTNESS,
24-
SUPPORT_COLOR,
25-
SUPPORT_COLOR_TEMP,
23+
LightEntityFeature,
2624
SUPPORT_EFFECT,
2725
)
2826
from homeassistant.config_entries import ConfigEntry
@@ -103,18 +101,20 @@ def __init__(self, config):
103101
"""Initialize a Virtual light."""
104102
super().__init__(config, PLATFORM_DOMAIN)
105103

106-
self._attr_supported_color_modes = {ColorMode.ONOFF}
107-
self._attr_supported_features = 0
104+
self._attr_supported_features = LightEntityFeature(0)
105+
self._attr_supported_color_modes = set()
106+
self._attr_color_mode = ColorMode.UNKNOWN
108107

109-
if config.get(CONF_SUPPORT_BRIGHTNESS):
110-
self._attr_supported_features |= SUPPORT_BRIGHTNESS
111-
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
112-
if config.get(CONF_SUPPORT_COLOR):
113-
self._attr_supported_features |= SUPPORT_COLOR
114-
self._attr_supported_color_modes.add(ColorMode.HS)
115108
if config.get(CONF_SUPPORT_COLOR_TEMP):
116-
self._attr_supported_features |= SUPPORT_COLOR_TEMP
117109
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
110+
if config.get(CONF_SUPPORT_COLOR):
111+
self._attr_supported_color_modes.add(ColorMode.HS)
112+
if config.get(CONF_SUPPORT_BRIGHTNESS):
113+
if not self._attr_supported_color_modes:
114+
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
115+
if not self._attr_supported_color_modes:
116+
self._attr_supported_color_modes.add(ColorMode.ONOFF)
117+
118118
if config.get(CONF_SUPPORT_EFFECT):
119119
self._attr_supported_features |= SUPPORT_EFFECT
120120
self._attr_effect_list = self._config.get(CONF_INITIAL_EFFECT_LIST)
@@ -124,14 +124,17 @@ def _create_state(self, config):
124124

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

127-
if self._attr_supported_features & SUPPORT_BRIGHTNESS:
127+
if ColorMode.BRIGHTNESS in self._attr_supported_color_modes:
128+
self._attr_color_mode = ColorMode.BRIGHTNESS
128129
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
129-
if self._attr_supported_features & SUPPORT_COLOR:
130+
if ColorMode.HS in self._attr_supported_color_modes:
130131
self._attr_color_mode = ColorMode.HS
131132
self._attr_hs_color = config.get(CONF_INITIAL_COLOR)
132-
if self._attr_supported_features & SUPPORT_COLOR_TEMP:
133+
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
134+
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
133135
self._attr_color_mode = ColorMode.COLOR_TEMP
134136
self._attr_color_temp = config.get(CONF_INITIAL_COLOR_TEMP)
137+
self._attr_brightness = config.get(CONF_INITIAL_BRIGHTNESS)
135138
if self._attr_supported_features & SUPPORT_EFFECT:
136139
self._attr_effect = config.get(CONF_INITIAL_EFFECT)
137140

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

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

143-
if self._attr_supported_features & SUPPORT_BRIGHTNESS:
146+
if ColorMode.BRIGHTNESS in self._attr_supported_color_modes:
147+
self._attr_color_mode = ColorMode.BRIGHTNESS
144148
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
145-
if self._attr_supported_features & SUPPORT_COLOR:
149+
if ColorMode.HS in self._attr_supported_color_modes:
146150
self._attr_color_mode = ColorMode.HS
147151
self._attr_hs_color = state.attributes.get(ATTR_HS_COLOR, config.get(CONF_INITIAL_COLOR))
148-
if self._attr_supported_features & SUPPORT_COLOR_TEMP:
152+
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
153+
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
149154
self._attr_color_mode = ColorMode.COLOR_TEMP
150155
self._attr_color_temp = state.attributes.get(ATTR_COLOR_TEMP, config.get(CONF_INITIAL_COLOR_TEMP))
156+
self._attr_brightness = state.attributes.get(ATTR_BRIGHTNESS, config.get(CONF_INITIAL_BRIGHTNESS))
151157
if self._attr_supported_features & SUPPORT_EFFECT:
152158
self._attr_effect = state.attributes.get(ATTR_EFFECT, config.get(CONF_INITIAL_EFFECT))
153159

154160
def _update_attributes(self):
155161
"""Return the state attributes."""
156-
super()._update_attributes();
162+
super()._update_attributes()
157163
self._attr_extra_state_attributes.update({
158164
name: value for name, value in (
159165
(ATTR_BRIGHTNESS, self._attr_brightness),
@@ -169,14 +175,12 @@ def turn_on(self, **kwargs):
169175
"""Turn the light on."""
170176
_LOGGER.debug(f"turning {self.name} on {pprint.pformat(kwargs)}")
171177
hs_color = kwargs.get(ATTR_HS_COLOR, None)
172-
if hs_color is not None and self._attr_supported_features & SUPPORT_COLOR:
173-
self._attr_color_mode = ColorMode.HS
178+
if hs_color is not None and ColorMode.HS in self._attr_supported_color_modes:
174179
self._attr_hs_color = hs_color
175180
self._attr_color_temp = None
176181

177182
ct = kwargs.get(ATTR_COLOR_TEMP, None)
178-
if ct is not None and self._attr_supported_features & SUPPORT_COLOR_TEMP:
179-
self._attr_color_mode = ColorMode.COLOR_TEMP
183+
if ct is not None and ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
180184
self._attr_color_temp = ct
181185
self._attr_hs_color = None
182186

0 commit comments

Comments
 (0)