def get_devices(self) -> bool: """Return tuple listing outlets, switches, and fans of devices.""" if not self.enabled: return False self.in_process = True proc_return = False response, _ = Helpers.call_api( '/cloud/v1/deviceManaged/devices', 'post', headers=Helpers.req_header_bypass(), json_object=Helpers.req_body(self, 'devicelist'), ) if response and Helpers.code_check(response): if 'result' in response and 'list' in response['result']: device_list = response['result']['list'] proc_return = self.process_devices(device_list) else: logger.error('Device list in response not found') else: logger.warning('Error retrieving device list') self.in_process = False return proc_return
def toggle(self, status) -> bool: """Toggle multicolor bulb.""" body = helpers.req_body(self.manager, 'bypassV2') if status == 'off': status_bool = False else: status_bool = True body['cid'] = self.cid body['configModule'] = self.config_module body['payload'] = { 'method': 'setSwitch', 'source': 'APP', 'data': { 'id': 0, 'enabled': status_bool, } } r, _ = helpers.call_api('/cloud/v2/deviceManaged/bypassV2', 'post', headers=helpers.req_header_bypass(), json_object=body) if helpers.code_check(r): self.device_status = status return True logger.debug('%s offline', self.device_name) self.device_status = 'off' self.connection_status = 'offline' return False
def get_config(self) -> None: """Get configuration and firmware info of multicolor bulb.""" body = helpers.req_body(self.manager, 'bypass') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api( '/cloud/v1/deviceManaged/configurations', 'post', headers=helpers.req_header_bypass(), json_object=body, ) if helpers.code_check(r): if r.get('result') is not None: result = r.get('result') self.__build_config_dict(result) else: logger.debug('Error getting %s config info', self.device_name) logger.debug(' return code - %d with message %s', r.get('code'), r.get('msg'))
def get_details(self) -> None: """Get details of multicolor bulb.""" body = helpers.req_body(self.manager, 'bypassV2') body['cid'] = self.cid body['configModule'] = self.config_module body['payload'] = { 'method': 'getLightStatusV2', 'source': 'APP', 'data': {}, } r, _ = helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', 'post', headers=helpers.req_header_bypass(), json_object=body, ) if not isinstance(r, dict) or not helpers.code_check(r): logger.debug('Error calling %s', self.device_name) return self._interpret_apicall_result(r)
def set_status( self, brightness: int = None, color_temp: int = None, color_saturation: int = None, color_hue: float = None, color_mode: str = None, color_value: int = None, ) -> bool: """Turn on Multicolor bulbs and adjust parameters.""" # initiate variables color_mode_api = '' should_force = 1 if (color_temp is None and color_saturation is None and color_hue is None and color_mode is None and color_value is None): if brightness is not None: should_force = 0 else: # Turn on without any extra parameters if self.toggle('on'): self.device_status = 'on' return True self.device_status = 'off' self.connection_status = 'offline' logger.debug('%s offline', self.device_name) return False # """Set brightness of multicolor bulb.""" brightness_api: Union[str, int] = '' if brightness is not None: if not self.dimmable_feature: logger.debug('%s is not dimmable', self.device_name) return False if not isinstance(brightness, int): logger.error('Error: brightness value should be a integer ' 'number between 1 and 100 OR None') return False if brightness < 1 or brightness > 100: logger.warning('Warning: brightness value should be ' 'between 1 and 100') # ensure brightness is between 0 and 100 brightness = max(1, (min(100, brightness))) brightness_api = int(brightness) # """Set White Temperature of Bulb in pct (1 - 100).""" color_temp_api: Union[str, int] = '' if color_temp is not None: if not self.color_temp_feature: logger.debug('%s is not white temperature tunable', self.device_name) return False if not isinstance(color_temp, int): logger.error('Error: color_temp value should be a integer ' 'number between 0 and 100 OR None') return False if color_temp < 0 or color_temp > 100: logger.debug( 'Warning: color_temp value should be between 0 and 100') # ensure color_temp is between 0 and 100 color_temp = max(0, (min(100, color_temp))) color_temp_api = color_temp color_mode_api = 'white' # """Set Color Saturation of Bulb in pct (1 - 100).""" color_saturation_api: Union[str, int] = '' if color_saturation is not None: if not self.color_temp_feature: logger.debug('%s is not color capable', self.device_name) return False if not isinstance(color_saturation, int): logger.error( 'Error: color_saturation value should be a integer ' 'number between 0 and 100 OR None') return False if color_saturation < 0 or color_saturation > 100: logger.debug('Warning: color_saturation value should be ' 'between 0 and 100') # ensure color_temp is between 0 and 100 color_saturation = max(0, (min(100, color_saturation))) # convert value to api expected range (0-10000) color_saturation_api = round(color_saturation * 100, None) color_mode_api = 'hsv' # """Set Color Hue of Bulb in pct (1 - 100).""" color_hue_api: Union[str, int] = '' if color_hue is not None: if not self.rgb_shift_feature: logger.debug('%s is not color capable', self.device_name) return False if not isinstance(color_hue, float): logger.error('Error: color_hue value should be a float number ' 'between 0 and 360 OR None') return False if color_hue <= 0 or color_hue > 360: logger.warning( 'Warning: color_hue value should be between 0 and 360') # ensure color_hue is between 0 and 360 color_hue = max(0, (min(360, color_hue))) # convert value to api expected range (0-10000) color_hue_api = round(color_hue * 27.777777, None) color_mode_api = 'hsv' # """Set Color Mode of Bulb (white / hsv).""" if color_mode is not None: if not self.rgb_shift_feature: logger.debug('%s is not color capable', self.device_name) return False if not isinstance(color_mode, str): logger.error('Error: color_mode should be a string value') return False color_mode = color_mode.lower() possible_modes = {'white': 'white', 'color': 'hsv', 'hsv': 'hsv'} if color_mode not in possible_modes: logger.error('Color mode specified is not acceptable ' '(Try: "white"/"color"/"hsv")') return False color_mode_api = str(possible_modes[color_mode]) # """Set color value of multicolor bulb.""" if color_value is not None: if not self.rgb_shift_feature: logger.debug('%s is not color capable', self.device_name) return False if not isinstance(color_value, int): logger.error( 'Error: color_value value should be a integer number ' 'between 1 and 100 OR None') return False if color_value < 1 or color_value > 100: logger.warning( 'Warning: color_value value should be between 1 and 100') # ensure color_value is between 0 and 100 color_value = max(1, (min(100, color_value))) # color value is actually set by the brightness # parameter when color_mode = hsv should_force = 1 brightness_api = color_value color_mode_api = 'hsv' # Prepare JSON for API call body = helpers.req_body(self.manager, 'bypassV2') body['cid'] = self.cid body['configModule'] = self.config_module body['payload'] = { 'method': 'setLightStatusV2', 'source': 'APP', 'data': { 'force': should_force, 'brightness': brightness_api, 'colorTemp': color_temp_api, 'colorMode': color_mode_api, 'hue': color_hue_api, 'saturation': color_saturation_api, 'value': '' } } # Make API call r, _ = helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', 'post', headers=helpers.req_header_bypass(), json_object=body, ) # Check result if helpers.code_check(r): self._interpret_apicall_result(r) self.device_status = 'on' return True self.device_status = 'off' self.connection_status = 'offline' logger.debug('%s offline', self.device_name) return False