def test_color_hsb_to_RGB(): """Test color_hsb_to_RGB.""" assert color_util.color_hsb_to_RGB(0, 0, 0) == (0, 0, 0) assert color_util.color_hsb_to_RGB(0, 0, 1.0) == (255, 255, 255) assert color_util.color_hsb_to_RGB(240, 1.0, 1.0) == (0, 0, 255) assert color_util.color_hsb_to_RGB(120, 1.0, 1.0) == (0, 255, 0) assert color_util.color_hsb_to_RGB(0, 1.0, 1.0) == (255, 0, 0)
def test_color_hsb_to_RGB(self): """Test color_hsb_to_RGB.""" self.assertEqual((0, 0, 0), color_util.color_hsb_to_RGB(0, 0, 0)) self.assertEqual((255, 255, 255), color_util.color_hsb_to_RGB(0, 0, 1.0)) self.assertEqual((0, 0, 255), color_util.color_hsb_to_RGB(240, 1.0, 1.0)) self.assertEqual((0, 255, 0), color_util.color_hsb_to_RGB(120, 1.0, 1.0)) self.assertEqual((255, 0, 0), color_util.color_hsb_to_RGB(0, 1.0, 1.0))
def test_color_hsb_to_RGB(self): """Test color_hsb_to_RGB.""" assert (0, 0, 0) == \ color_util.color_hsb_to_RGB(0, 0, 0) assert (255, 255, 255) == \ color_util.color_hsb_to_RGB(0, 0, 1.0) assert (0, 0, 255) == \ color_util.color_hsb_to_RGB(240, 1.0, 1.0) assert (0, 255, 0) == \ color_util.color_hsb_to_RGB(120, 1.0, 1.0) assert (255, 0, 0) == \ color_util.color_hsb_to_RGB(0, 1.0, 1.0)
def async_api_set_color(hass, config, request, entity): """Process a set color request.""" supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES) rgb = color_util.color_hsb_to_RGB( float(request[API_PAYLOAD]['color']['hue']), float(request[API_PAYLOAD]['color']['saturation']), float(request[API_PAYLOAD]['color']['brightness'])) if supported & light.SUPPORT_RGB_COLOR > 0: yield from hass.services.async_call( entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_RGB_COLOR: rgb, }, blocking=False) else: xyz = color_util.color_RGB_to_xy(*rgb) yield from hass.services.async_call( entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_XY_COLOR: (xyz[0], xyz[1]), light.ATTR_BRIGHTNESS: xyz[2], }, blocking=False) return api_message(request)
def hsb_to_alexa_color( hsb: Optional[Tuple[float, float, float]] ) -> Tuple[Optional[Tuple[float, float]], Optional[Text]]: """Convert a given hue/saturation/brightness value into the closest Alexa color.""" if hsb is None: return None, None hue, saturation, brightness = hsb return rgb_to_alexa_color(color_hsb_to_RGB(hue, saturation, brightness))
def async_api_set_color(hass, config, request, entity): """Process a set color request.""" rgb = color_util.color_hsb_to_RGB( float(request[API_PAYLOAD]['color']['hue']), float(request[API_PAYLOAD]['color']['saturation']), float(request[API_PAYLOAD]['color']['brightness']) ) yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_RGB_COLOR: rgb, }, blocking=False) return api_message(request)
async def async_api_set_color(hass, config, directive, context): """Process a set color request.""" entity = directive.entity rgb = color_util.color_hsb_to_RGB( float(directive.payload['color']['hue']), float(directive.payload['color']['saturation']), float(directive.payload['color']['brightness'])) await hass.services.async_call(entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_RGB_COLOR: rgb, }, blocking=False, context=context) return directive.response()
def async_api_set_color(hass, config, request, entity): """Process a set color request.""" supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES) rgb = color_util.color_hsb_to_RGB( float(request[API_PAYLOAD]['color']['hue']), float(request[API_PAYLOAD]['color']['saturation']), float(request[API_PAYLOAD]['color']['brightness']) ) if supported & light.SUPPORT_RGB_COLOR > 0: yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_RGB_COLOR: rgb, }, blocking=False) else: xyz = color_util.color_RGB_to_xy(*rgb) yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, { ATTR_ENTITY_ID: entity.entity_id, light.ATTR_XY_COLOR: (xyz[0], xyz[1]), light.ATTR_BRIGHTNESS: xyz[2], }, blocking=False) return api_message(request)
async def controlDevice(hass, action, payload): applianceDic = payload['appliance'] additionalApplianceDetails = applianceDic.get('additionalApplianceDetails', {}) if 'service' in additionalApplianceDetails: call_service(hass, additionalApplianceDetails['service'], additionalApplianceDetails['data']) return {'attributes': []} # 实体ID entity_id = applianceDic['applianceId'] # 服务数据 service_data = {'entity_id': entity_id} state = hass.states.get(entity_id) domain = entity_id.split('.')[0] # 小度事件数据 xiaodu_data = {'type': action, 'domain': domain, 'entity_id': entity_id} # 高度 if 'deltValue' in payload: deltValue = payload.get('deltValue') if isinstance(deltValue, dict): deltValue = deltValue['value'] xiaodu_data.update({'deltValue': deltValue}) # 温度 if 'deltaValue' in payload: deltaValue = payload.get('deltaValue') if isinstance(deltaValue, dict): deltaValue = deltaValue['value'] xiaodu_data.update({'deltaValue': deltaValue}) # 颜色 if 'color' in payload: xiaodu_data.update({'color': payload['color']}) # 色温 if 'colorTemperatureInKelvin' in payload: xiaodu_data.update( {'colorTemperatureInKelvin': payload['colorTemperatureInKelvin']}) # 单位秒 if 'timestamp' in payload: xiaodu_data.update({'timestamp': payload['timestamp']}) # 定时 if 'timeInterval' in payload: xiaodu_data.update({'timeInterval': payload['timeInterval']}) # 亮度 if 'brightness' in payload: xiaodu_data.update({'brightness': payload['brightness']['value']}) # 增量百分比 if 'deltaPercentage' in payload: xiaodu_data.update( {'deltaPercentage': payload['deltaPercentage']['value']}) # 模式 if 'mode' in payload: xiaodu_data.update({'mode': payload['mode']['value']}) # 风速 if 'fanSpeed' in payload: xiaodu_data.update({'fanSpeed': payload['fanSpeed']['value']}) # 温度 if 'targetTemperature' in payload: xiaodu_data.update( {'targetTemperature': payload['targetTemperature']['value']}) # 服务名称 ################ 打开关闭设备 if action == 'TurnOnRequest': return call_service(hass, domain + '.turn_on', service_data) elif action == 'TurnOffRequest': return call_service(hass, domain + '.turn_off', service_data) elif action == 'TimingTurnOnRequest': return call_service(hass, domain + '.turn_on', service_data) elif action == 'TimingTurnOffRequest': return call_service(hass, domain + '.turn_off', service_data) elif action == 'PauseRequest': # 暂停 print('暂停') if domain == 'media_player': return call_service(hass, 'media_player.media_pause', service_data) elif action == 'ContinueRequest': # 继续 print('继续') if domain == 'media_player': return call_service(hass, 'media_player.media_play', service_data) elif action == 'StartUpRequest': # 启动 print('启动') ################ 可控灯光设备 elif action == 'SetBrightnessPercentageRequest': # 亮度 service_data.update({'brightness_pct': xiaodu_data['brightness']}) result = call_service(hass, 'light.turn_on', service_data) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": 100 } }) return result elif action == 'IncrementBrightnessPercentageRequest': # 增加亮度 service_data.update( {'brightness_step_pct': xiaodu_data['deltaPercentage']}) result = call_service(hass, 'light.turn_on', service_data) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": int(state.attributes.get('brightness', 255) / 255 * 100) } }) return result elif action == 'DecrementBrightnessPercentageRequest': # 减少亮度 service_data.update( {'brightness_step_pct': -xiaodu_data['deltaPercentage']}) result = call_service(hass, 'light.turn_on', service_data) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": int(state.attributes.get('brightness', 255) / 255 * 100) } }) return result elif action == 'SetColorRequest': color = xiaodu_data['color'] service_data.update({ 'rgb_color': color_util.color_hsb_to_RGB(color['hue'], color['saturation'], color['brightness']) }) return call_service(hass, 'light.turn_on', service_data) elif action == 'IncrementColorTemperatureRequest': print('增加色温') elif action == 'DecrementColorTemperatureRequest': print('减少色温') elif action == 'SetColorTemperatureRequest': # 设置色温 print('设置色温', xiaodu_data['colorTemperatureInKelvin']) ################ 可控温度设备 elif action == 'IncrementTemperatureRequest': service_data.update( {'temperature': state.attributes.get('temperature') + deltaValue}) return call_service(hass, 'climate.set_temperature', service_data) elif action == 'DecrementTemperatureRequest': service_data.update( {'temperature': state.attributes.get('temperature') - deltaValue}) return call_service(hass, 'climate.set_temperature', service_data) elif action == 'SetTemperatureRequest': service_data.update({'temperature': xiaodu_data['targetTemperature']}) return call_service(hass, 'climate.set_temperature', service_data) ################ 设备模式设置 elif action == 'SetModeRequest': mode = xiaodu_data['mode'] # 空调 if domain == 'climate': ''' # 上下摆风 if mode == 'UP_DOWN_SWING': return call_service(hass, 'climate.set_swing_mode', {'entity_id': entity_id, 'hvac_mode': mode.lower()}) # 左右摆风 elif mode == 'LEFT_RIGHT_SWING': return call_service(hass, 'climate.set_swing_mode', {'entity_id': entity_id, 'hvac_mode': mode.lower()}) ''' if mode == 'COOL' or mode == 'HEAT' or mode == 'AUTO': service_data.update({'hvac_mode': mode.lower()}) return call_service(hass, 'climate.set_hvac_mode', service_data) # 电视 elif domain == 'media_player': if mode == 'MUTE': service_data.update({'is_volume_muted': True}) return call_service(hass, 'media_player.volume_mute', service_data) elif action == 'UnsetModeRequest': # 空调 if domain == 'climate': service_data.update({'hvac_mode': 'auto'}) return call_service(hass, 'climate.set_hvac_mode', service_data) elif action == 'TimingSetModeRequest': # 空调 if domain == 'climate': mode = xiaodu_data['mode'] if mode == 'COOL' or mode == 'HEAT' or mode == 'AUTO': service_data.update({'hvac_mode': mode.lower()}) return call_service(hass, 'climate.set_hvac_mode', service_data) ################ 可控风速设备 elif action == 'IncrementFanSpeedRequest': # 空调 if domain == 'climate': service_data.update({'hvac_mode': 'high'}) return call_service(hass, 'climate.set_fan_mode', service_data) elif action == 'DecrementFanSpeedRequest': # 空调 if domain == 'climate': service_data.update({'hvac_mode': 'low'}) return call_service(hass, 'climate.set_fan_mode', service_data) elif action == 'SetFanSpeedRequest': print('设置风速') ################ 可控音量设备 elif action == 'IncrementVolumeRequest': # 电视 if domain == 'media_player': return call_service(hass, 'media_player.volume_up', service_data) elif action == 'DecrementVolumeRequest': # 电视 if domain == 'media_player': return call_service(hass, 'media_player.volume_down', service_data) elif action == 'SetVolumeRequest': # 电视 if domain == 'media_player': service_data.update({'volume_level': deltaValue / 100}) return call_service(hass, 'media_player.volume_set', service_data) elif action == 'SetVolumeMuteRequest': # 电视 if domain == 'media_player': service_data.update({'is_volume_muted': deltaValue == 'on'}) return call_service(hass, 'media_player.volume_mute', service_data) ################ 电视频道设置 elif action == 'IncrementTVChannelRequest': print('上一个频道') if domain == 'media_player': return call_service(hass, 'media_player.media_previous_track', service_data) elif action == 'DecrementTVChannelRequest': print('下一个频道') if domain == 'media_player': return call_service(hass, 'media_player.media_next_track', service_data) elif action == 'SetTVChannelRequest': print(f'播放指定频道: {deltaValue}') elif action == 'ReturnTVChannelRequest': print('返回上一个观看频道') ################ 可控高度设备 elif action == 'IncrementHeightRequest': # 晾衣架调高 if domain == 'cover': return call_service(hass, 'cover.close_cover', service_data) elif action == 'DecrementHeightRequest': # 晾衣架调低 if domain == 'cover': return call_service(hass, 'cover.open_cover', service_data) ''' ################ 可控速度设备 elif action == 'IncrementSpeedRequest': elif action == 'DecrementSpeedRequest': elif action == 'SetSpeedRequest': ################ 可锁定设备 elif action == 'SetLockStateRequest': ################ 打印设备 elif action == 'SubmitPrintRequest': ################ 可控吸力设备 elif action == 'SetSuctionRequest': ################ 可控水量设备 elif action == 'SetWaterLevelRequest': ################ 可控电量设备 elif action == 'ChargeRequest': elif action == 'DischargeRequest': ################ 可控方向设备 elif action == 'SetDirectionRequest': elif action == 'SetCleaningLocationRequest': elif action == 'SetComplexActionsRequest': ################ 可控定时设备 elif action == 'SetTimerRequest': # 定时 elif action == 'TimingCancelRequest': # 取消定时 ################ 可复位设备 elif action == 'ResetRequset': ################ 可控楼层设备 elif action == 'SetFloorRequest': elif action == 'IncrementFloorRequest': elif action == 'DecrementFloorRequest': ################ 可控湿度类设备 elif action == 'SetHumidityRequest': ''' # 发送事件 hass.bus.async_fire("xiaodu_event", xiaodu_data) # 调用python_script if hass.services.has_service('python_script', 'xiaodu_event'): hass.async_create_task( hass.services.async_call('python_script', 'xiaodu_event', xiaodu_data)) return {"attributes": []}
async def controlDevice(hass, action, payload): applianceDic = payload['appliance'] additionalApplianceDetails = applianceDic.get('additionalApplianceDetails', {}) # 实体ID entity_id = applianceDic['applianceId'] state = hass.states.get(entity_id) domain = entity_id.split('.')[0] # 小度事件数据 xiaodu_data = {'type': action, 'domain': domain, 'entity_id': entity_id} # 高度 if 'deltValue' in payload: deltValue = payload.get('deltValue') if isinstance(deltValue, dict): deltValue = deltValue['value'] xiaodu_data.update({'deltValue': deltValue}) # 颜色 if 'color' in payload: xiaodu_data.update({'color': payload['color']}) # 色温 if 'colorTemperatureInKelvin' in payload: xiaodu_data.update( {'colorTemperatureInKelvin': payload['colorTemperatureInKelvin']}) # 单位秒 if 'timestamp' in payload: xiaodu_data.update({'timestamp': payload['timestamp']}) # 定时 if 'timeInterval' in payload: xiaodu_data.update({'timeInterval': payload['timeInterval']}) # 亮度 if 'brightness' in payload: xiaodu_data.update({'brightness': payload['brightness']['value']}) # 增量百分比 if 'deltaPercentage' in payload: xiaodu_data.update( {'deltaPercentage': payload['deltaPercentage']['value']}) # 模式 if 'mode' in payload: xiaodu_data.update({'mode': payload['mode']['value']}) # 风速 if 'fanSpeed' in payload: xiaodu_data.update({'fanSpeed': payload['fanSpeed']['value']}) # 温度 if 'targetTemperature' in payload: xiaodu_data.update( {'targetTemperature': payload['targetTemperature']['value']}) # 服务名称 ################ 打开关闭设备 if action == 'TurnOnRequest': return call_service(hass, domain + '.turn_on', {'entity_id': entity_id}) elif action == 'TurnOffRequest': return call_service(hass, domain + '.turn_off', {'entity_id': entity_id}) elif action == 'TimingTurnOnRequest': print('定时打开') elif action == 'TimingTurnOffRequest': print('定时关闭') service_name = 'turn_off' elif action == 'PauseRequest': # 暂停 print('暂停') elif action == 'ContinueRequest': # 继续 print('继续') elif action == 'StartUpRequest': # 启动 print('启动') ################ 可控灯光设备 elif action == 'SetBrightnessPercentageRequest': # 亮度 result = call_service(hass, 'light.turn_on', { 'entity_id': entity_id, 'brightness_pct': xiaodu_data['brightness'] }) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": 100 } }) return result elif action == 'IncrementBrightnessPercentageRequest': # 增加亮度 result = call_service( hass, 'light.turn_on', { 'entity_id': entity_id, 'brightness_step_pct': xiaodu_data['deltaPercentage'] }) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": int(state.attributes.get('brightness', 255) / 255 * 100) } }) return result elif action == 'DecrementBrightnessPercentageRequest': # 减少亮度 result = call_service( hass, 'light.turn_on', { 'entity_id': entity_id, 'brightness_step_pct': -xiaodu_data['deltaPercentage'] }) result.update({ "previousState": { "brightness": { "value": 50 } }, "brightness": { "value": int(state.attributes.get('brightness', 255) / 255 * 100) } }) return result elif action == 'SetColorRequest': # 启动 color = xiaodu_data['color'] return call_service( hass, 'light.turn_on', { 'entity_id': entity_id, 'rgb_color': color_util.color_hsb_to_RGB(color['hue'], color['saturation'], color['brightness']) }) elif action == 'IncrementColorTemperatureRequest': print('增加色温') elif action == 'DecrementColorTemperatureRequest': print('减少色温') elif action == 'SetColorTemperatureRequest': # 设置色温 print('设置色温', xiaodu_data['colorTemperatureInKelvin']) ################ 可控温度设备 elif action == 'IncrementTemperatureRequest': return call_service( hass, 'climate.set_temperature', { 'entity_id': entity_id, 'temperature': state.attributes.get('temperature') - deltValue }) elif action == 'DecrementTemperatureRequest': state = hass.states.get(entity_id) return call_service( hass, 'climate.set_temperature', { 'entity_id': entity_id, 'temperature': state.attributes.get('temperature') - deltValue }) elif action == 'SetTemperatureRequest': return call_service( hass, 'climate.set_temperature', { 'entity_id': entity_id, 'temperature': xiaodu_data['targetTemperature'] }) ''' ################ 可控风速设备 elif action == 'IncrementFanSpeedRequest': elif action == 'DecrementFanSpeedRequest': elif action == 'SetFanSpeedRequest': ################ 可控速度设备 elif action == 'IncrementSpeedRequest': elif action == 'DecrementSpeedRequest': elif action == 'SetSpeedRequest': ################ 设备模式设置 elif action == 'SetModeRequest': elif action == 'UnsetModeRequest': elif action == 'TimingSetModeRequest': ################ 电视频道设置 elif action == 'IncrementTVChannelRequest': elif action == 'DecrementTVChannelRequest': elif action == 'SetTVChannelRequest': elif action == 'ReturnTVChannelRequest': ################ 可控音量设备 elif action == 'IncrementVolumeRequest': elif action == 'DecrementVolumeRequest': elif action == 'SetVolumeRequest': elif action == 'SetVolumeMuteRequest': ################ 可锁定设备 elif action == 'SetLockStateRequest': ################ 打印设备 elif action == 'SubmitPrintRequest': ################ 可控吸力设备 elif action == 'SetSuctionRequest': ################ 可控水量设备 elif action == 'SetWaterLevelRequest': ################ 可控电量设备 elif action == 'ChargeRequest': elif action == 'DischargeRequest': ################ 可控方向设备 elif action == 'SetDirectionRequest': elif action == 'SetCleaningLocationRequest': elif action == 'SetComplexActionsRequest': ################ 可控高度设备 elif action == 'IncrementHeightRequest': # 晾衣架调高 elif action == 'DecrementHeightRequest': # 晾衣架调低 ################ 可控定时设备 elif action == 'SetTimerRequest': # 定时 elif action == 'TimingCancelRequest': # 取消定时 ################ 可复位设备 elif action == 'ResetRequset': ################ 可控楼层设备 elif action == 'SetFloorRequest': elif action == 'IncrementFloorRequest': elif action == 'DecrementFloorRequest': ################ 可控湿度类设备 elif action == 'SetHumidityRequest': ''' # 发送事件 hass.bus.async_fire("xiaodu_event", xiaodu_data) return {"attributes": []}