def test_color_name_to_rgb_valid_name(self): """Test color_name_to_rgb.""" self.assertEqual((255, 0, 0), color_util.color_name_to_rgb('red')) self.assertEqual((0, 0, 255), color_util.color_name_to_rgb('blue')) self.assertEqual((0, 255, 0), color_util.color_name_to_rgb('green'))
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 find_hsbk(self, **kwargs): """Find the desired color from a number of possible inputs.""" changed_color = False hsbk = kwargs.pop(ATTR_HSBK, None) if hsbk is not None: return [hsbk, True] color_name = kwargs.pop(ATTR_COLOR_NAME, None) if color_name is not None: kwargs[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) if ATTR_RGB_COLOR in kwargs: hue, saturation, brightness = \ convert_rgb_to_hsv(kwargs[ATTR_RGB_COLOR]) changed_color = True else: hue = self._hue saturation = self._sat brightness = self._bri if ATTR_XY_COLOR in kwargs: hue, saturation = color_util.color_xy_to_hs(*kwargs[ATTR_XY_COLOR]) saturation = saturation * (BYTE_MAX + 1) changed_color = True # When color or temperature is set, use a default value for the other if ATTR_COLOR_TEMP in kwargs: kelvin = int(color_temperature_mired_to_kelvin( kwargs[ATTR_COLOR_TEMP])) if not changed_color: saturation = 0 changed_color = True else: if changed_color: kelvin = 3500 else: kelvin = self._kel if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] * (BYTE_MAX + 1) changed_color = True else: brightness = self._bri return [[hue, saturation, brightness, kelvin], changed_color]
def async_handle_light_service(service): """Hande a turn light on or off service call.""" # Get the validated data params = service.data.copy() # Convert the entity ids to valid light ids target_lights = component.async_extract_from_service(service) params.pop(ATTR_ENTITY_ID, None) # Processing extra data for turn light on request. profile = profiles.get(params.pop(ATTR_PROFILE, None)) if profile: 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: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) for light in target_lights: if service.service == SERVICE_TURN_ON: yield from light.async_turn_on(**params) elif service.service == SERVICE_TURN_OFF: yield from light.async_turn_off(**params) else: yield from light.async_toggle(**params) update_tasks = [] for light in target_lights: if not light.should_poll: continue update_coro = hass.async_add_job( light.async_update_ha_state(True)) if hasattr(light, 'async_update'): update_tasks.append(update_coro) else: yield from update_coro if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop)
def preprocess_turn_on_alternatives(params): """Processing 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: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) 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)
def find_hsbk(self, **kwargs): """Find the desired color from a number of possible inputs.""" changed_color = False hsbk = kwargs.pop(ATTR_HSBK, None) if hsbk is not None: return [hsbk, True] color_name = kwargs.pop(ATTR_COLOR_NAME, None) if color_name is not None: kwargs[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) if ATTR_RGB_COLOR in kwargs: hue, saturation, brightness = \ convert_rgb_to_hsv(kwargs[ATTR_RGB_COLOR]) changed_color = True else: hue = self._hue saturation = self._sat brightness = self._bri if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] * (BYTE_MAX + 1) changed_color = True else: brightness = self._bri if ATTR_XY_COLOR in kwargs: hue, saturation, _ = \ color_util.color_xy_brightness_to_hsv( *kwargs[ATTR_XY_COLOR], ibrightness=(brightness // (BYTE_MAX + 1))) saturation = saturation * (BYTE_MAX + 1) changed_color = True if ATTR_COLOR_TEMP in kwargs: kelvin = int(color_temperature_mired_to_kelvin( kwargs[ATTR_COLOR_TEMP])) changed_color = True else: kelvin = self._kel return [[hue, saturation, brightness, kelvin], changed_color]
def handle_light_service(service): """Hande a turn light on or off service call.""" # Get the validated data params = service.data.copy() # Convert the entity ids to valid light ids target_lights = component.extract_from_service(service) params.pop(ATTR_ENTITY_ID, None) service_fun = None if service.service == SERVICE_TURN_OFF: service_fun = 'turn_off' elif service.service == SERVICE_TOGGLE: service_fun = 'toggle' if service_fun: for light in target_lights: getattr(light, service_fun)(**params) for light in target_lights: if light.should_poll: light.update_ha_state(True) return # Processing extra data for turn light on request. profile = profiles.get(params.pop(ATTR_PROFILE, None)) if profile: 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: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) for light in target_lights: light.turn_on(**params) for light in target_lights: if light.should_poll: light.update_ha_state(True)
def test_color_name_to_rgb_valid_name(self): """Test color_name_to_rgb.""" self.assertEqual((255, 0, 0), color_util.color_name_to_rgb('red')) self.assertEqual((0, 0, 255), color_util.color_name_to_rgb('blue')) self.assertEqual((0, 128, 0), color_util.color_name_to_rgb('green')) # spaces in the name self.assertEqual((72, 61, 139), color_util.color_name_to_rgb('dark slate blue')) # spaces removed from name self.assertEqual((72, 61, 139), color_util.color_name_to_rgb('darkslateblue')) self.assertEqual((72, 61, 139), color_util.color_name_to_rgb('dark slateblue')) self.assertEqual((72, 61, 139), color_util.color_name_to_rgb('darkslate blue'))
def test_color_name_to_rgb_unknown_name_raises_value_error(self): """Test color_name_to_rgb.""" with pytest.raises(ValueError): color_util.color_name_to_rgb('not a color')
def test_color_name_to_rgb_unknown_name_default_white(self): """Test color_name_to_rgb.""" self.assertEqual((255, 255, 255), color_util.color_name_to_rgb('not a color'))
async def controlDevice(hass, action, payload): deviceResponseList = [] deviceIds = payload['deviceIds'] params = payload['params'] powerstate = params.get('powerstate') brightness = params.get('brightness') motorControl = params.get('motorControl') volume = params.get('volume') playControl = params.get('playControl') # 颜色转换 color = get_color_name(params.get('color')) # 根据设备ID,找到对应的实体ID for entity_id in deviceIds: service_name = None service_data = {'entity_id': entity_id} state = hass.states.get(entity_id) attributes = state.attributes domain = state.domain # 查询属性 if action == 'thing.attribute.get': print('查询') else: # 设置属性 if action == 'thing.attribute.set': if powerstate == 1: service_name = 'turn_on' elif powerstate == 0: service_name = 'turn_off' # 设置亮度 if brightness is not None: service_name = 'turn_on' service_data.update({'brightness_pct': brightness}) # 设置颜色 if color is not None: service_name = 'turn_on' service_data.update( {'rgb_color': color_util.color_name_to_rgb(color)}) # 设置电机控制 if motorControl is not None: # 判断是否晾衣架 if domain == 'cover': if motorControl == 0: service_name = 'stop_cover' elif motorControl == 1: service_name = 'open_cover' elif motorControl == 2: service_name = 'close_cover' # 设置音量 if volume is not None: if domain == 'media_player': service_name = 'volume_set' service_data.update({'volume_level': volume / 100}) # 播放控制 ''' if playControl is not None: if domain == 'media_player' if playControl == 1: service_name = 'media_play' elif playControl == 2: service_name = 'media_pause' ''' elif action == 'thing.attribute.adjust': # 增加/减少亮度 if brightness is not None: service_name = 'turn_on' service_data.update({ 'brightness_pct': int(attributes.get('brightness', 255) / 255 * 100) + brightness }) if service_name is not None: # 脚本执行 if domain == 'script': service_name = entity_id.split('.')[1] service_data = {} _LOGGER.debug('执行服务:' + domain + '.' + service_name) hass.async_create_task( hass.services.async_call(domain, service_name, service_data)) else: # 天猫事件数据 tmall_data = { 'type': action, 'domain': domain, 'entity_id': entity_id } tmall_data.update(params) hass.bus.async_fire("tmall_event", tmall_data) # 返回结果 deviceResponseList.append({ "deviceId": entity_id, "errorCode": "SUCCESS", "message": "SUCCESS" }) return {"deviceResponseList": deviceResponseList}
def test_color_name_to_rgb_unknown_name_raises_value_error(): """Test color_name_to_rgb.""" with pytest.raises(ValueError): color_util.color_name_to_rgb("not a color")
def alexa_color_name_to_rgb(color_name: Text) -> Tuple[int, int, int]: """Convert an alexa color name into RGB""" return color_name_to_rgb(color_name.replace("_", ""))