def update(self): """Fetch new state data for this light and update local values.""" self._light.update() self._available = self._light.available self._brightness = self._light.brightness self._is_on = self._light.is_on self._hs_color = color_util.color_RGB_to_hs(*self._light.rgb_color)
def preprocess_turn_on_alternatives(params): """Process extra data for turn light on request.""" profile = Profiles.get(params.pop(ATTR_PROFILE, None)) if profile is not None: params.setdefault(ATTR_XY_COLOR, profile[:2]) params.setdefault(ATTR_BRIGHTNESS, profile[2]) color_name = params.pop(ATTR_COLOR_NAME, None) if color_name is not None: try: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) except ValueError: _LOGGER.warning('Got unknown color %s, falling back to white', color_name) params[ATTR_RGB_COLOR] = (255, 255, 255) kelvin = params.pop(ATTR_KELVIN, None) if kelvin is not None: mired = color_util.color_temperature_kelvin_to_mired(kelvin) params[ATTR_COLOR_TEMP] = int(mired) brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None) if brightness_pct is not None: params[ATTR_BRIGHTNESS] = int(255 * brightness_pct/100) xy_color = params.pop(ATTR_XY_COLOR, None) if xy_color is not None: params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color) rgb_color = params.pop(ATTR_RGB_COLOR, None) if rgb_color is not None: params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
def _update(self): """Really update the state.""" # Brightness handling if self._supported_flags & SUPPORT_BRIGHTNESS: self._brightness = float(self.fibaro_device.properties.value) # Fibaro might report 0-99 or 0-100 for brightness, # based on device type, so we round up here if self._brightness > 99: self._brightness = 100 # Color handling if self._supported_flags & SUPPORT_COLOR and \ 'color' in self.fibaro_device.properties and \ ',' in self.fibaro_device.properties.color: # Fibaro communicates the color as an 'R, G, B, W' string rgbw_s = self.fibaro_device.properties.color if rgbw_s == '0,0,0,0' and\ 'lastColorSet' in self.fibaro_device.properties: rgbw_s = self.fibaro_device.properties.lastColorSet rgbw_list = [int(i) for i in rgbw_s.split(",")][:4] if rgbw_list[0] or rgbw_list[1] or rgbw_list[2]: self._color = color_util.color_RGB_to_hs(*rgbw_list[:3]) if (self._supported_flags & SUPPORT_WHITE_VALUE) and \ self.brightness != 0: self._white = min(255, max(0, rgbw_list[3]*100.0 / self._brightness))
def parse_data(self, data, raw_data): """Parse data sent by gateway.""" value = data.get(self._data_key) if value is None: return False if value == 0: if self._state: self._state = False return True rgbhexstr = "%x" % value if len(rgbhexstr) > 8: _LOGGER.error("Light RGB data error." " Can't be more than 8 characters. Received: %s", rgbhexstr) return False rgbhexstr = rgbhexstr.zfill(8) rgbhex = bytes.fromhex(rgbhexstr) rgba = struct.unpack('BBBB', rgbhex) brightness = rgba[0] rgb = rgba[1:] self._brightness = brightness self._hs = color_util.color_RGB_to_hs(*rgb) self._state = True return True
def _update_rgb_or_w(self): """Update the controller with values from RGB or RGBW child.""" value = self._values[self.value_type] color_list = rgb_hex_to_rgb_list(value) if len(color_list) > 3: self._white = color_list.pop() self._hs = color_util.color_RGB_to_hs(*color_list)
def _turn_on_rgb_and_w(self, hex_template, **kwargs): """Turn on RGB or RGBW child device.""" rgb = list(color_util.color_hs_to_RGB(*self._hs)) white = self._white hex_color = self._values.get(self.value_type) hs_color = kwargs.get(ATTR_HS_COLOR) if hs_color is not None: new_rgb = color_util.color_hs_to_RGB(*hs_color) else: new_rgb = None new_white = kwargs.get(ATTR_WHITE_VALUE) if new_rgb is None and new_white is None: return if new_rgb is not None: rgb = list(new_rgb) if hex_template == '%02x%02x%02x%02x': if new_white is not None: rgb.append(new_white) else: rgb.append(white) hex_color = hex_template % tuple(rgb) if len(rgb) > 3: white = rgb.pop() self.gateway.set_child_value( self.node_id, self.child_id, self.value_type, hex_color) if self.gateway.optimistic: # optimistically assume that light has changed state self._hs = color_util.color_RGB_to_hs(*rgb) self._white = white self._values[self.value_type] = hex_color
async def async_update(self): """Fetch state from the device.""" from miio import DeviceException try: state = await self.hass.async_add_executor_job(self._light.status) except DeviceException as ex: self._available = False _LOGGER.error("Got exception while fetching the state: %s", ex) return _LOGGER.debug("Got new state: %s", state) self._available = True self._state = state.is_on self._brightness = ceil((255 / 100.0) * state.brightness) self._color_temp = self.translate( state.color_temperature, CCT_MIN, CCT_MAX, self.max_mireds, self.min_mireds) self._hs_color = color.color_RGB_to_hs(*state.rgb) self._state_attrs.update({ ATTR_SCENE: state.scene, ATTR_SLEEP_ASSISTANT: state.sleep_assistant, ATTR_SLEEP_OFF_TIME: state.sleep_off_time, ATTR_TOTAL_ASSISTANT_SLEEP_TIME: state.total_assistant_sleep_time, ATTR_BRAND_SLEEP: state.brand_sleep, ATTR_BRAND: state.brand, })
def hs_color(self): """Return the HS color value.""" if self.device.supports_color: rgb = self.device.current_color if rgb is None: return None return color_util.color_RGB_to_hs(*rgb) return None
def _observe_update(self, tradfri_device): """Receive new state data for this light.""" self._refresh(tradfri_device) rgb = color_util.rgb_hex_to_rgb_list( self._light_data.hex_color_inferred ) self._hs_color = color_util.color_RGB_to_hs(*rgb) self.async_schedule_update_ha_state()
def hs_color(self) -> Optional[Tuple[float, float]]: """Return the hue and saturation color value [float, float].""" if self._state is None: return None return color_util.color_RGB_to_hs( self._state.red * 255, self._state.green * 255, self._state.blue * 255)
def update(self): """Call to update state.""" self._state = self.vera_device.is_switched_on() if self.vera_device.is_dimmable: # If it is dimmable, both functions exist. In case color # is not supported, it will return None self._brightness = self.vera_device.get_brightness() rgb = self.vera_device.get_color() self._color = color_util.color_RGB_to_hs(*rgb) if rgb else None
def rgb_received(topic, payload, qos): """Handle new MQTT messages for RGB.""" payload = templates[CONF_RGB](payload) if not payload: _LOGGER.debug("Ignoring empty rgb message from '%s'", topic) return rgb = [int(val) for val in payload.split(',')] self._hs = color_util.color_RGB_to_hs(*rgb) self.async_schedule_update_ha_state()
async def execute(self, command, params): """Execute a color spectrum command.""" # Convert integer to hex format and left pad with 0's till length 6 hex_value = "{0:06x}".format(params['color']['spectrumRGB']) color = color_util.color_RGB_to_hs( *color_util.rgb_hex_to_rgb_list(hex_value)) await self.hass.services.async_call(light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: self.state.entity_id, light.ATTR_HS_COLOR: color }, blocking=True)
def update(self): """Synchronise state from the bulb.""" self._bulb.update() if self._bulb.power: self._brightness = self._bulb.brightness self._temp = self._bulb.temperature if self._bulb.colors: self._colormode = True self._hs = color_util.color_RGB_to_hs(*self._bulb.colors) else: self._colormode = False self._state = self._bulb.power
def update(self): """Update status of a light.""" super().update() self._state = self._luminary.on() rgb = self._luminary.rgb() self._hs = color_util.color_RGB_to_hs(*rgb) o_temp = self._luminary.temp() if o_temp == 0: self._temperature = None else: self._temperature = color_temperature_kelvin_to_mired( self._luminary.temp()) self._brightness = int(self._luminary.lum() * 2.55)
def rgb_received(topic, payload, qos): """Handle new MQTT messages for RGB.""" payload = templates[CONF_RGB](payload) if not payload: _LOGGER.debug("Ignoring empty rgb message from '%s'", topic) return rgb = [int(val) for val in payload.split(',')] self._hs = color_util.color_RGB_to_hs(*rgb) if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is None: percent_bright = \ float(color_util.color_RGB_to_hsv(*rgb)[2]) / 100.0 self._brightness = int(percent_bright * 255) self.async_schedule_update_ha_state()
def update(self): """Update group status.""" super().update() self._light_ids = self._luminary.lights() light = self._bridge.lights()[self._light_ids[0]] self._brightness = int(light.lum() * 2.55) rgb = light.rgb() self._hs = color_util.color_RGB_to_hs(*rgb) o_temp = light.temp() if o_temp == 0: self._temperature = None else: self._temperature = color_temperature_kelvin_to_mired(o_temp) self._state = light.on()
async def execute(self, command, data, params, challenge): """Execute a color temperature command.""" if 'temperature' in params['color']: temp = color_util.color_temperature_kelvin_to_mired( params['color']['temperature']) min_temp = self.state.attributes[light.ATTR_MIN_MIREDS] max_temp = self.state.attributes[light.ATTR_MAX_MIREDS] if temp < min_temp or temp > max_temp: raise SmartHomeError( ERR_VALUE_OUT_OF_RANGE, "Temperature should be between {} and {}".format(min_temp, max_temp)) await self.hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: self.state.entity_id, light.ATTR_COLOR_TEMP: temp, }, blocking=True, context=data.context) elif 'spectrumRGB' in params['color']: # Convert integer to hex format and left pad with 0's till length 6 hex_value = "{0:06x}".format(params['color']['spectrumRGB']) color = color_util.color_RGB_to_hs( *color_util.rgb_hex_to_rgb_list(hex_value)) await self.hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: self.state.entity_id, light.ATTR_HS_COLOR: color }, blocking=True, context=data.context) elif 'spectrumHSV' in params['color']: color = params['color']['spectrumHSV'] saturation = color['saturation'] * 100 brightness = color['value'] * 255 await self.hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: self.state.entity_id, light.ATTR_HS_COLOR: [color['hue'], saturation], light.ATTR_BRIGHTNESS: brightness }, blocking=True, context=data.context)
def _get_hs_from_properties(self): rgb = self._properties.get('rgb', None) color_mode = self._properties.get('color_mode', None) if not rgb or not color_mode: return None color_mode = int(color_mode) if color_mode == 2: # color temperature temp_in_k = mired_to_kelvin(self._color_temp) return color_util.color_temperature_to_hs(temp_in_k) if color_mode == 3: # hsv hue = int(self._properties.get('hue')) sat = int(self._properties.get('sat')) return (hue / 360 * 65536, sat / 100 * 255) rgb = int(rgb) blue = rgb & 0xff green = (rgb >> 8) & 0xff red = (rgb >> 16) & 0xff return color_util.color_RGB_to_hs(red, green, blue)
def hs_color(self): if self._prop_color: num = round(self._prop_color.from_dict(self._state_attrs) or 0) rgb = int_to_rgb(num) return color.color_RGB_to_hs(*rgb) return None
def hs_color(self): return color_util.color_RGB_to_hs(self._rgb_color[0], self._rgb_color[1], self._rgb_color[2])
def update_properties(self): """Update internal properties based on zwave values.""" super().update_properties() if self.values.color is None: return if self.values.color_channels is None: return # Color Channels self._color_channels = self.values.color_channels.data # Color Data String data = self.values.color.data # RGB is always present in the openzwave color data string. rgb = [int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)] self._hs = color_util.color_RGB_to_hs(*rgb) # Parse remaining color channels. Openzwave appends white channels # that are present. index = 7 # Warm white if self._color_channels & COLOR_CHANNEL_WARM_WHITE: warm_white = int(data[index:index + 2], 16) index += 2 else: warm_white = 0 # Cold white if self._color_channels & COLOR_CHANNEL_COLD_WHITE: cold_white = int(data[index:index + 2], 16) index += 2 else: cold_white = 0 # Color temperature. With the AEOTEC ZW098 bulb, only two color # temperatures are supported. The warm and cold channel values # indicate brightness for warm/cold color temperature. if self._zw098: if warm_white > 0: self._ct = TEMP_WARM_HASS self._hs = ct_to_hs(self._ct) elif cold_white > 0: self._ct = TEMP_COLD_HASS self._hs = ct_to_hs(self._ct) else: # RGB color is being used. Just report midpoint. self._ct = TEMP_MID_HASS elif self._color_channels & COLOR_CHANNEL_WARM_WHITE: self._white = warm_white elif self._color_channels & COLOR_CHANNEL_COLD_WHITE: self._white = cold_white # If no rgb channels supported, report None. if not (self._color_channels & COLOR_CHANNEL_RED or self._color_channels & COLOR_CHANNEL_GREEN or self._color_channels & COLOR_CHANNEL_BLUE): self._hs = None
def hs_color(self) -> tuple[float, float] | None: """Return the hue and saturation color value [float, float].""" return color_util.color_RGB_to_hs(self._state.red * 255, self._state.green * 255, self._state.blue * 255)
def hs_color(self): """Return the HS color value.""" rgb = None if self.device.supports_rgbw or self.device.supports_color: rgb, _ = self.device.current_color return color_util.color_RGB_to_hs(*rgb) if rgb else None
def state_received(topic, payload, qos): """Handle new MQTT messages.""" state = self._templates[CONF_STATE_TEMPLATE].\ async_render_with_possible_json_value(payload) if state == STATE_ON: self._state = True elif state == STATE_OFF: self._state = False else: _LOGGER.warning("Invalid state value received") if self._brightness is not None: try: self._brightness = int( self._templates[CONF_BRIGHTNESS_TEMPLATE]. async_render_with_possible_json_value(payload) ) except ValueError: _LOGGER.warning("Invalid brightness value received") if self._color_temp is not None: try: self._color_temp = int( self._templates[CONF_COLOR_TEMP_TEMPLATE]. async_render_with_possible_json_value(payload) ) except ValueError: _LOGGER.warning("Invalid color temperature value received") if self._hs is not None: try: red = int( self._templates[CONF_RED_TEMPLATE]. async_render_with_possible_json_value(payload)) green = int( self._templates[CONF_GREEN_TEMPLATE]. async_render_with_possible_json_value(payload)) blue = int( self._templates[CONF_BLUE_TEMPLATE]. async_render_with_possible_json_value(payload)) self._hs = color_util.color_RGB_to_hs(red, green, blue) except ValueError: _LOGGER.warning("Invalid color value received") if self._white_value is not None: try: self._white_value = int( self._templates[CONF_WHITE_VALUE_TEMPLATE]. async_render_with_possible_json_value(payload) ) except ValueError: _LOGGER.warning('Invalid white value received') if self._templates[CONF_EFFECT_TEMPLATE] is not None: effect = self._templates[CONF_EFFECT_TEMPLATE].\ async_render_with_possible_json_value(payload) if effect in self._effect_list: self._effect = effect else: _LOGGER.warning("Unsupported effect value received") self.async_schedule_update_ha_state()
# is not implemented if not supported_color_modes: if (rgb_color := params.pop(ATTR_RGB_COLOR, None)) is not None: params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color) elif (xy_color := params.pop(ATTR_XY_COLOR, None)) is not None: params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color) elif ATTR_HS_COLOR in params and COLOR_MODE_HS not in supported_color_modes: hs_color = params.pop(ATTR_HS_COLOR) if COLOR_MODE_RGB in supported_color_modes: params[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color) elif COLOR_MODE_XY in supported_color_modes: params[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color) elif ATTR_RGB_COLOR in params and COLOR_MODE_RGB not in supported_color_modes: rgb_color = params.pop(ATTR_RGB_COLOR) if COLOR_MODE_HS in supported_color_modes: params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color) elif COLOR_MODE_XY in supported_color_modes: params[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color) elif ATTR_XY_COLOR in params and COLOR_MODE_XY not in supported_color_modes: xy_color = params.pop(ATTR_XY_COLOR) if COLOR_MODE_HS in supported_color_modes: params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color) elif COLOR_MODE_RGB in supported_color_modes: params[ATTR_RGB_COLOR] = color_util.color_xy_to_RGB(*xy_color) # Remove deprecated white value if the light supports color mode if supported_color_modes: params.pop(ATTR_WHITE_VALUE, None) # Zero brightness: Light will be turned off if params.get(ATTR_BRIGHTNESS) == 0:
def hs_color(self): """Return the hue and saturation color value [float, float].""" return color_util.color_RGB_to_hs(self._red, self._green, self._blue)
def hs_color(self): """Return last color value set.""" return color_util.color_RGB_to_hs(*self._rgb_color)
def hs_color(self): return color_util.color_RGB_to_hs( *color_util.rgb_hex_to_rgb_list(self._color))
def hs_color(self): """Return the hs value.""" return color_util.color_RGB_to_hs(*self._lamp.state()['rgb'])
async def test_device_types(hass: HomeAssistant, caplog): """Test different device types.""" mocked_bulb = _mocked_bulb() properties = {**PROPERTIES} properties.pop("active_mode") properties["color_mode"] = "3" # HSV mocked_bulb.last_properties = properties async def _async_setup(config_entry): with patch(f"{MODULE}.AsyncBulb", return_value=mocked_bulb): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() # We use asyncio.create_task now to avoid # blocking starting so we need to block again await hass.async_block_till_done() async def _async_test( bulb_type, model, target_properties, nightlight_properties=None, name=UNIQUE_NAME, entity_id=ENTITY_LIGHT, ): config_entry = MockConfigEntry( domain=DOMAIN, data={**CONFIG_ENTRY_DATA, CONF_NIGHTLIGHT_SWITCH: False} ) config_entry.add_to_hass(hass) mocked_bulb.bulb_type = bulb_type model_specs = _MODEL_SPECS.get(model) type(mocked_bulb).get_model_specs = MagicMock(return_value=model_specs) await _async_setup(config_entry) state = hass.states.get(entity_id) assert state.state == "on" target_properties["friendly_name"] = name target_properties["flowing"] = False target_properties["night_light"] = True target_properties["music_mode"] = False assert dict(state.attributes) == target_properties await hass.config_entries.async_unload(config_entry.entry_id) await config_entry.async_remove(hass) registry = er.async_get(hass) registry.async_clear_config_entry(config_entry.entry_id) # nightlight if nightlight_properties is None: return config_entry = MockConfigEntry( domain=DOMAIN, data={**CONFIG_ENTRY_DATA, CONF_NIGHTLIGHT_SWITCH: True} ) config_entry.add_to_hass(hass) await _async_setup(config_entry) assert hass.states.get(entity_id).state == "off" state = hass.states.get(f"{entity_id}_nightlight") assert state.state == "on" nightlight_properties["friendly_name"] = f"{name} nightlight" nightlight_properties["icon"] = "mdi:weather-night" nightlight_properties["flowing"] = False nightlight_properties["night_light"] = True nightlight_properties["music_mode"] = False assert dict(state.attributes) == nightlight_properties await hass.config_entries.async_unload(config_entry.entry_id) await config_entry.async_remove(hass) registry.async_clear_config_entry(config_entry.entry_id) await hass.async_block_till_done() bright = round(255 * int(PROPERTIES["bright"]) / 100) current_brightness = round(255 * int(PROPERTIES["current_brightness"]) / 100) ct = color_temperature_kelvin_to_mired(int(PROPERTIES["ct"])) hue = int(PROPERTIES["hue"]) sat = int(PROPERTIES["sat"]) rgb = int(PROPERTIES["rgb"]) rgb_color = ((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF) hs_color = (hue, sat) bg_bright = round(255 * int(PROPERTIES["bg_bright"]) / 100) bg_ct = color_temperature_kelvin_to_mired(int(PROPERTIES["bg_ct"])) bg_hue = int(PROPERTIES["bg_hue"]) bg_sat = int(PROPERTIES["bg_sat"]) bg_rgb = int(PROPERTIES["bg_rgb"]) bg_hs_color = (bg_hue, bg_sat) bg_rgb_color = ((bg_rgb >> 16) & 0xFF, (bg_rgb >> 8) & 0xFF, bg_rgb & 0xFF) nl_br = round(255 * int(PROPERTIES["nl_br"]) / 100) # Default await _async_test( None, "mono", { "effect_list": YEELIGHT_MONO_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": bright, "color_mode": "brightness", "supported_color_modes": ["brightness"], }, ) # White await _async_test( BulbType.White, "mono", { "effect_list": YEELIGHT_MONO_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": bright, "color_mode": "brightness", "supported_color_modes": ["brightness"], }, ) # Color - color mode CT mocked_bulb.last_properties["color_mode"] = "2" # CT model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, "color_mode": "color_temp", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) # Color - color mode HS mocked_bulb.last_properties["color_mode"] = "3" # HSV model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "hs_color": hs_color, "rgb_color": color_hs_to_RGB(*hs_color), "xy_color": color_hs_to_xy(*hs_color), "color_mode": "hs", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) # Color - color mode RGB mocked_bulb.last_properties["color_mode"] = "1" # RGB model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "hs_color": color_RGB_to_hs(*rgb_color), "rgb_color": rgb_color, "xy_color": color_RGB_to_xy(*rgb_color), "color_mode": "rgb", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) # Color - color mode HS but no hue mocked_bulb.last_properties["color_mode"] = "3" # HSV mocked_bulb.last_properties["hue"] = None model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_mode": "hs", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) # Color - color mode RGB but no color mocked_bulb.last_properties["color_mode"] = "1" # RGB mocked_bulb.last_properties["rgb"] = None model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_mode": "rgb", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) # Color - unsupported color_mode mocked_bulb.last_properties["color_mode"] = 4 # Unsupported model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "color_mode": "unknown", "supported_color_modes": ["color_temp", "hs", "rgb"], }, { "supported_features": 0, "color_mode": "onoff", "supported_color_modes": ["onoff"], }, ) assert "Light reported unknown color mode: 4" in caplog.text # WhiteTemp model_specs = _MODEL_SPECS["ceiling1"] await _async_test( BulbType.WhiteTemp, "ceiling1", { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, "color_mode": "color_temp", "supported_color_modes": ["color_temp"], }, { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": nl_br, "color_mode": "brightness", "supported_color_modes": ["brightness"], }, ) # WhiteTempMood properties.pop("power") properties["main_power"] = "on" model_specs = _MODEL_SPECS["ceiling4"] await _async_test( BulbType.WhiteTempMood, "ceiling4", { "friendly_name": NAME, "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "flowing": False, "night_light": True, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, "color_mode": "color_temp", "supported_color_modes": ["color_temp"], }, { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": nl_br, "color_mode": "brightness", "supported_color_modes": ["brightness"], }, ) # Background light - color mode CT mocked_bulb.last_properties["bg_lmode"] = "2" # CT await _async_test( BulbType.WhiteTempMood, "ceiling4", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, "color_temp": bg_ct, "color_mode": "color_temp", "supported_color_modes": ["color_temp", "hs", "rgb"], }, name=f"{UNIQUE_NAME} ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight", ) # Background light - color mode HS mocked_bulb.last_properties["bg_lmode"] = "3" # HS await _async_test( BulbType.WhiteTempMood, "ceiling4", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, "hs_color": bg_hs_color, "rgb_color": color_hs_to_RGB(*bg_hs_color), "xy_color": color_hs_to_xy(*bg_hs_color), "color_mode": "hs", "supported_color_modes": ["color_temp", "hs", "rgb"], }, name=f"{UNIQUE_NAME} ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight", ) # Background light - color mode RGB mocked_bulb.last_properties["bg_lmode"] = "1" # RGB await _async_test( BulbType.WhiteTempMood, "ceiling4", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, "hs_color": color_RGB_to_hs(*bg_rgb_color), "rgb_color": bg_rgb_color, "xy_color": color_RGB_to_xy(*bg_rgb_color), "color_mode": "rgb", "supported_color_modes": ["color_temp", "hs", "rgb"], }, name=f"{UNIQUE_NAME} ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight", )
def hs_color(self): """Return the hue and saturation color value [float, float].""" return color_util.color_RGB_to_hs(*self._rgb)
def rgb_received(topic, payload, qos): """Handle new MQTT messages for RGB.""" rgb = [int(val) for val in templates[CONF_RGB](payload).split(',')] self._hs = color_util.color_RGB_to_hs(*rgb) self.async_schedule_update_ha_state()
def hs_color(self) -> tuple[float, float] | None: """Return the hue and saturation color value [float, float].""" color = self.coordinator.data.state.segments[ self._segment].color_primary return color_util.color_RGB_to_hs(*color[:3])
def update_properties(self): """Update internal properties based on zwave values.""" super().update_properties() if self.values.color is None: return if self.values.color_channels is None: return # Color Channels self._color_channels = self.values.color_channels.data # Color Data String data = self.values.color.data # RGB is always present in the openzwave color data string. rgb = [ int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)] self._hs = color_util.color_RGB_to_hs(*rgb) # Parse remaining color channels. Openzwave appends white channels # that are present. index = 7 # Warm white if self._color_channels & COLOR_CHANNEL_WARM_WHITE: warm_white = int(data[index:index+2], 16) index += 2 else: warm_white = 0 # Cold white if self._color_channels & COLOR_CHANNEL_COLD_WHITE: cold_white = int(data[index:index+2], 16) index += 2 else: cold_white = 0 # Color temperature. With the AEOTEC ZW098 bulb, only two color # temperatures are supported. The warm and cold channel values # indicate brightness for warm/cold color temperature. if self._zw098: if warm_white > 0: self._ct = TEMP_WARM_HASS self._hs = ct_to_hs(self._ct) elif cold_white > 0: self._ct = TEMP_COLD_HASS self._hs = ct_to_hs(self._ct) else: # RGB color is being used. Just report midpoint. self._ct = TEMP_MID_HASS elif self._color_channels & COLOR_CHANNEL_WARM_WHITE: self._white = warm_white elif self._color_channels & COLOR_CHANNEL_COLD_WHITE: self._white = cold_white # If no rgb channels supported, report None. if not (self._color_channels & COLOR_CHANNEL_RED or self._color_channels & COLOR_CHANNEL_GREEN or self._color_channels & COLOR_CHANNEL_BLUE): self._hs = None
def hs_color(self) -> tuple[float, float]: """Return last color value set.""" return color_util.color_RGB_to_hs(*self._rgb_color)
def state_received(topic, payload, qos): """Handle new MQTT messages.""" values = json.loads(payload) if values['state'] == 'ON': self._state = True elif values['state'] == 'OFF': self._state = False if self._hs is not None: try: red = int(values['color']['r']) green = int(values['color']['g']) blue = int(values['color']['b']) self._hs = color_util.color_RGB_to_hs(red, green, blue) except KeyError: pass except ValueError: _LOGGER.warning("Invalid RGB color value received") try: x_color = float(values['color']['x']) y_color = float(values['color']['y']) self._hs = color_util.color_xy_to_hs(x_color, y_color) except KeyError: pass except ValueError: _LOGGER.warning("Invalid XY color value received") if self._brightness is not None: try: self._brightness = int(values['brightness'] / float(self._brightness_scale) * 255) except KeyError: pass except ValueError: _LOGGER.warning("Invalid brightness value received") if self._color_temp is not None: try: self._color_temp = int(values['color_temp']) except KeyError: pass except ValueError: _LOGGER.warning("Invalid color temp value received") if self._effect is not None: try: self._effect = values['effect'] except KeyError: pass except ValueError: _LOGGER.warning("Invalid effect value received") if self._white_value is not None: try: self._white_value = int(values['white_value']) except KeyError: pass except ValueError: _LOGGER.warning("Invalid white value received") self.async_schedule_update_ha_state()
def hs_color(self): """Return the color of the light.""" if self._device.is_dimmable and self._device.has_color: return color_util.color_RGB_to_hs(*self._device.color)
def hs_color(self): """Read back the hue-saturation of the light.""" return color_util.color_RGB_to_hs(*self._rgb_color)
def hs_color(self): """Return the color property.""" return color_util.color_RGB_to_hs(*self._bulb.getRgb())
def _update_color(self, values): if not self._config[CONF_COLOR_MODE]: # Deprecated color handling try: red = int(values["color"]["r"]) green = int(values["color"]["g"]) blue = int(values["color"]["b"]) self._hs = color_util.color_RGB_to_hs(red, green, blue) except KeyError: pass except ValueError: _LOGGER.warning("Invalid RGB color value received") return try: x_color = float(values["color"]["x"]) y_color = float(values["color"]["y"]) self._hs = color_util.color_xy_to_hs(x_color, y_color) except KeyError: pass except ValueError: _LOGGER.warning("Invalid XY color value received") return try: hue = float(values["color"]["h"]) saturation = float(values["color"]["s"]) self._hs = (hue, saturation) except KeyError: pass except ValueError: _LOGGER.warning("Invalid HS color value received") return else: color_mode = values["color_mode"] if not self._supports_color_mode(color_mode): _LOGGER.warning("Invalid color mode received") return try: if color_mode == COLOR_MODE_COLOR_TEMP: self._color_temp = int(values["color_temp"]) self._color_mode = COLOR_MODE_COLOR_TEMP elif color_mode == COLOR_MODE_HS: hue = float(values["color"]["h"]) saturation = float(values["color"]["s"]) self._color_mode = COLOR_MODE_HS self._hs = (hue, saturation) elif color_mode == COLOR_MODE_RGB: r = int(values["color"]["r"]) # pylint: disable=invalid-name g = int(values["color"]["g"]) # pylint: disable=invalid-name b = int(values["color"]["b"]) # pylint: disable=invalid-name self._color_mode = COLOR_MODE_RGB self._rgb = (r, g, b) elif color_mode == COLOR_MODE_RGBW: r = int(values["color"]["r"]) # pylint: disable=invalid-name g = int(values["color"]["g"]) # pylint: disable=invalid-name b = int(values["color"]["b"]) # pylint: disable=invalid-name w = int(values["color"]["w"]) # pylint: disable=invalid-name self._color_mode = COLOR_MODE_RGBW self._rgbw = (r, g, b, w) elif color_mode == COLOR_MODE_RGBWW: r = int(values["color"]["r"]) # pylint: disable=invalid-name g = int(values["color"]["g"]) # pylint: disable=invalid-name b = int(values["color"]["b"]) # pylint: disable=invalid-name c = int(values["color"]["c"]) # pylint: disable=invalid-name w = int(values["color"]["w"]) # pylint: disable=invalid-name self._color_mode = COLOR_MODE_RGBWW self._rgbww = (r, g, b, c, w) elif color_mode == COLOR_MODE_XY: x = float(values["color"]["x"]) # pylint: disable=invalid-name y = float(values["color"]["y"]) # pylint: disable=invalid-name self._color_mode = COLOR_MODE_XY self._xy = (x, y) except (KeyError, ValueError): _LOGGER.warning("Invalid or incomplete color value received")
def hs_color(self): """Return the color of the light.""" return color_util.color_RGB_to_hs(*self._device.led_rgb)
def state_received(msg): """Handle new MQTT messages.""" values = json.loads(msg.payload) if values["state"] == "ON": self._state = True elif values["state"] == "OFF": self._state = False if self._supported_features and SUPPORT_COLOR: try: red = int(values["color"]["r"]) green = int(values["color"]["g"]) blue = int(values["color"]["b"]) self._hs = color_util.color_RGB_to_hs(red, green, blue) except KeyError: pass except ValueError: _LOGGER.warning("Invalid RGB color value received") try: x_color = float(values["color"]["x"]) y_color = float(values["color"]["y"]) self._hs = color_util.color_xy_to_hs(x_color, y_color) except KeyError: pass except ValueError: _LOGGER.warning("Invalid XY color value received") try: hue = float(values["color"]["h"]) saturation = float(values["color"]["s"]) self._hs = (hue, saturation) except KeyError: pass except ValueError: _LOGGER.warning("Invalid HS color value received") if self._supported_features and SUPPORT_BRIGHTNESS: try: self._brightness = int( values["brightness"] / float(self._config[CONF_BRIGHTNESS_SCALE]) * 255) except KeyError: pass except (TypeError, ValueError): _LOGGER.warning("Invalid brightness value received") if self._supported_features and SUPPORT_COLOR_TEMP: try: self._color_temp = int(values["color_temp"]) except KeyError: pass except ValueError: _LOGGER.warning("Invalid color temp value received") if self._supported_features and SUPPORT_EFFECT: try: self._effect = values["effect"] except KeyError: pass if self._supported_features and SUPPORT_WHITE_VALUE: try: self._white_value = int(values["white_value"]) except KeyError: pass except ValueError: _LOGGER.warning("Invalid white value received") self.async_write_ha_state()
def hs_color(self): """Return the hs color value.""" if self.device["status"]["mode"] == "COLOUR": rgb = self.device["status"].get("hs_color") return color_util.color_RGB_to_hs(*rgb) return None
def state_received(msg): """Handle new MQTT messages.""" state = self._templates[ CONF_STATE_TEMPLATE].async_render_with_possible_json_value( msg.payload) if state == STATE_ON: self._state = True elif state == STATE_OFF: self._state = False else: _LOGGER.warning("Invalid state value received") if self._templates[CONF_BRIGHTNESS_TEMPLATE] is not None: try: self._brightness = int( self._templates[CONF_BRIGHTNESS_TEMPLATE]. async_render_with_possible_json_value(msg.payload)) except ValueError: _LOGGER.warning("Invalid brightness value received") if self._templates[CONF_COLOR_TEMP_TEMPLATE] is not None: try: self._color_temp = int( self._templates[CONF_COLOR_TEMP_TEMPLATE]. async_render_with_possible_json_value(msg.payload)) except ValueError: _LOGGER.warning("Invalid color temperature value received") if (self._templates[CONF_RED_TEMPLATE] is not None and self._templates[CONF_GREEN_TEMPLATE] is not None and self._templates[CONF_BLUE_TEMPLATE] is not None): try: red = int(self._templates[CONF_RED_TEMPLATE]. async_render_with_possible_json_value( msg.payload)) green = int(self._templates[CONF_GREEN_TEMPLATE]. async_render_with_possible_json_value( msg.payload)) blue = int(self._templates[CONF_BLUE_TEMPLATE]. async_render_with_possible_json_value( msg.payload)) self._hs = color_util.color_RGB_to_hs(red, green, blue) except ValueError: _LOGGER.warning("Invalid color value received") if self._templates[CONF_WHITE_VALUE_TEMPLATE] is not None: try: self._white_value = int( self._templates[CONF_WHITE_VALUE_TEMPLATE]. async_render_with_possible_json_value(msg.payload)) except ValueError: _LOGGER.warning("Invalid white value received") if self._templates[CONF_EFFECT_TEMPLATE] is not None: effect = self._templates[ CONF_EFFECT_TEMPLATE].async_render_with_possible_json_value( msg.payload) if effect in self._config.get(CONF_EFFECT_LIST): self._effect = effect else: _LOGGER.warning("Unsupported effect value received") self.async_write_ha_state()
def hs_color(self): """Return the hs_color of the light.""" # return tuple(map(int, self.tuya.hs_color())) return color_util.color_RGB_to_hs(self._light.r, self._light.g, self._light.b)
def hs_color(self) -> tuple[float, float]: """Return the color property.""" return color_util.color_RGB_to_hs(*self._rgb_color)
def hs_color(self): """Return the HS color value.""" if self._rgb: return color_util.color_RGB_to_hs(*self._rgb) else: return None
def rgbhex_to_hs(self): rgbhex = self._kettler._rgb rgb = color_util.rgb_hex_to_rgb_list(rgbhex) self._hs = color_util.color_RGB_to_hs(*rgb)
def _calculate_color_values(self) -> None: """Calculate light colors.""" # NOTE: We lookup all values here (instead of relying on the multicolor one) # to find out what colors are supported # as this is a simple lookup by key, this not heavy red_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=ColorComponent.RED.value, ) green_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=ColorComponent.GREEN.value, ) blue_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=ColorComponent.BLUE.value, ) ww_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=ColorComponent.WARM_WHITE.value, ) cw_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=ColorComponent.COLD_WHITE.value, ) # prefer the (new) combined color property # https://github.com/zwave-js/node-zwave-js/pull/1782 combined_color_val = self.get_zwave_value( "currentColor", CommandClass.SWITCH_COLOR, value_property_key=None, ) if combined_color_val and isinstance(combined_color_val.value, dict): multi_color = combined_color_val.value else: multi_color = {} # Default: Brightness (no color) self._color_mode = COLOR_MODE_BRIGHTNESS # RGB support if red_val and green_val and blue_val: # prefer values from the multicolor property red = multi_color.get("red", red_val.value) green = multi_color.get("green", green_val.value) blue = multi_color.get("blue", blue_val.value) self._supports_color = True if None not in (red, green, blue): # convert to HS self._hs_color = color_util.color_RGB_to_hs(red, green, blue) # Light supports color, set color mode to hs self._color_mode = COLOR_MODE_HS # color temperature support if ww_val and cw_val: self._supports_color_temp = True warm_white = multi_color.get("warmWhite", ww_val.value) cold_white = multi_color.get("coldWhite", cw_val.value) # Calculate color temps based on whites if cold_white or warm_white: self._color_temp = round(self._max_mireds - ( (cold_white / 255) * (self._max_mireds - self._min_mireds))) # White channels turned on, set color mode to color_temp self._color_mode = COLOR_MODE_COLOR_TEMP else: self._color_temp = None # only one white channel (warm white) = rgbw support elif red_val and green_val and blue_val and ww_val: self._supports_rgbw = True white = multi_color.get("warmWhite", ww_val.value) self._rgbw_color = (red, green, blue, white) # Light supports rgbw, set color mode to rgbw self._color_mode = COLOR_MODE_RGBW # only one white channel (cool white) = rgbw support elif cw_val: self._supports_rgbw = True white = multi_color.get("coldWhite", cw_val.value) self._rgbw_color = (red, green, blue, white) # Light supports rgbw, set color mode to rgbw self._color_mode = COLOR_MODE_RGBW
async def test_device_types(hass: HomeAssistant): """Test different device types.""" mocked_bulb = _mocked_bulb() properties = {**PROPERTIES} properties.pop("active_mode") properties["color_mode"] = "3" mocked_bulb.last_properties = properties async def _async_setup(config_entry): with patch(f"{MODULE}.Bulb", return_value=mocked_bulb): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() async def _async_test( bulb_type, model, target_properties, nightlight_properties=None, name=UNIQUE_NAME, entity_id=ENTITY_LIGHT, ): config_entry = MockConfigEntry( domain=DOMAIN, data={ **CONFIG_ENTRY_DATA, CONF_NIGHTLIGHT_SWITCH: False, }, ) config_entry.add_to_hass(hass) mocked_bulb.bulb_type = bulb_type model_specs = _MODEL_SPECS.get(model) type(mocked_bulb).get_model_specs = MagicMock(return_value=model_specs) await _async_setup(config_entry) state = hass.states.get(entity_id) assert state.state == "on" target_properties["friendly_name"] = name target_properties["flowing"] = False target_properties["night_light"] = True target_properties["music_mode"] = False assert dict(state.attributes) == target_properties await hass.config_entries.async_unload(config_entry.entry_id) await config_entry.async_remove(hass) registry = await entity_registry.async_get_registry(hass) registry.async_clear_config_entry(config_entry.entry_id) # nightlight if nightlight_properties is None: return config_entry = MockConfigEntry( domain=DOMAIN, data={ **CONFIG_ENTRY_DATA, CONF_NIGHTLIGHT_SWITCH: True, }, ) config_entry.add_to_hass(hass) await _async_setup(config_entry) assert hass.states.get(entity_id).state == "off" state = hass.states.get(f"{entity_id}_nightlight") assert state.state == "on" nightlight_properties["friendly_name"] = f"{name} nightlight" nightlight_properties["icon"] = "mdi:weather-night" nightlight_properties["flowing"] = False nightlight_properties["night_light"] = True nightlight_properties["music_mode"] = False assert dict(state.attributes) == nightlight_properties await hass.config_entries.async_unload(config_entry.entry_id) await config_entry.async_remove(hass) registry.async_clear_config_entry(config_entry.entry_id) bright = round(255 * int(PROPERTIES["bright"]) / 100) current_brightness = round(255 * int(PROPERTIES["current_brightness"]) / 100) ct = color_temperature_kelvin_to_mired(int(PROPERTIES["ct"])) hue = int(PROPERTIES["hue"]) sat = int(PROPERTIES["sat"]) hs_color = (round(hue / 360 * 65536, 3), round(sat / 100 * 255, 3)) rgb_color = color_hs_to_RGB(*hs_color) xy_color = color_hs_to_xy(*hs_color) bg_bright = round(255 * int(PROPERTIES["bg_bright"]) / 100) bg_ct = color_temperature_kelvin_to_mired(int(PROPERTIES["bg_ct"])) bg_rgb = int(PROPERTIES["bg_rgb"]) bg_rgb_color = ((bg_rgb >> 16) & 0xFF, (bg_rgb >> 8) & 0xFF, bg_rgb & 0xFF) bg_hs_color = color_RGB_to_hs(*bg_rgb_color) bg_xy_color = color_RGB_to_xy(*bg_rgb_color) nl_br = round(255 * int(PROPERTIES["nl_br"]) / 100) # Default await _async_test( None, "mono", { "effect_list": YEELIGHT_MONO_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": bright, }, ) # White await _async_test( BulbType.White, "mono", { "effect_list": YEELIGHT_MONO_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": bright, }, ) # Color model_specs = _MODEL_SPECS["color"] await _async_test( BulbType.Color, "color", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT_RGB, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, "hs_color": hs_color, "rgb_color": rgb_color, "xy_color": xy_color, }, {"supported_features": 0}, ) # WhiteTemp model_specs = _MODEL_SPECS["ceiling1"] await _async_test( BulbType.WhiteTemp, "ceiling1", { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT_WHITE_TEMP, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, }, { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": nl_br, }, ) # WhiteTempMood properties.pop("power") properties["main_power"] = "on" model_specs = _MODEL_SPECS["ceiling4"] await _async_test( BulbType.WhiteTempMood, "ceiling4", { "friendly_name": NAME, "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "flowing": False, "night_light": True, "supported_features": SUPPORT_YEELIGHT_WHITE_TEMP, "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), "max_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "brightness": current_brightness, "color_temp": ct, }, { "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT, "brightness": nl_br, }, ) await _async_test( BulbType.WhiteTempMood, "ceiling4", { "effect_list": YEELIGHT_COLOR_EFFECT_LIST, "supported_features": SUPPORT_YEELIGHT_RGB, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, "color_temp": bg_ct, "hs_color": bg_hs_color, "rgb_color": bg_rgb_color, "xy_color": bg_xy_color, }, name=f"{UNIQUE_NAME} ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight", )
def hs_color(self): """Return the HS color value.""" rgb = self._vantage_device.rgb hs = color_RGB_to_hs(*rgb) return hs # self._vantage_device.hs
def state_received(topic, payload, qos): """Handle new MQTT messages.""" values = json.loads(payload) if values['state'] == 'ON': self._state = True elif values['state'] == 'OFF': self._state = False if self._hs is not None: try: red = int(values['color']['r']) green = int(values['color']['g']) blue = int(values['color']['b']) self._hs = color_util.color_RGB_to_hs(red, green, blue) except KeyError: pass except ValueError: _LOGGER.warning("Invalid RGB color value received") try: x_color = float(values['color']['x']) y_color = float(values['color']['y']) self._hs = color_util.color_xy_to_hs(x_color, y_color) except KeyError: pass except ValueError: _LOGGER.warning("Invalid XY color value received") try: hue = float(values['color']['h']) saturation = float(values['color']['s']) self._hs = (hue, saturation) except KeyError: pass except ValueError: _LOGGER.warning("Invalid HS color value received") if self._brightness is not None: try: self._brightness = int(values['brightness'] / float(self._brightness_scale) * 255) except KeyError: pass except ValueError: _LOGGER.warning("Invalid brightness value received") if self._color_temp is not None: try: self._color_temp = int(values['color_temp']) except KeyError: pass except ValueError: _LOGGER.warning("Invalid color temp value received") if self._effect is not None: try: self._effect = values['effect'] except KeyError: pass except ValueError: _LOGGER.warning("Invalid effect value received") if self._white_value is not None: try: self._white_value = int(values['white_value']) except KeyError: pass except ValueError: _LOGGER.warning("Invalid white value received") self.async_schedule_update_ha_state()