def get_devices(self): """Return tuple listing outlets, switches, and fans of devices.""" outlets = [] switches = [] fans = [] bulbs = [] if not self.enabled: return self.in_process = True proc_return = False response, _ = helpers.call_api('/cloud/v1/deviceManaged/devices', 'post', headers=helpers.req_headers(self), json=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 get_details(self): """Build details dictionary.""" body = helpers.req_body(self.manager, 'devicedetail') body['pageSize'] = 100 body['page'] = 1 body['debugMode'] = False body['allData'] = True body['method'] = 'getWeighingDataV2' body['configModule'] = self.config_module head = helpers.req_headers(self.manager) r, _ = helpers.call_api( '/cloud/v2/deviceManaged/getWeighingDataV2', method='post', headers=head, json=body ) subuser_dict = {} if r is not None and helpers.code_check(r): if not isinstance(r.get('result', {}).get('weightDatas'), list): logger.warning("Error in getting %s weight data", self.device_name) return subuser_dict = self.bt_data_builder(r.get('result').get('weightDatas')) self.details = subuser_dict
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 login(self) -> bool: """Return True if log in request succeeds.""" user_check = isinstance(self.username, str) and len(self.username) > 0 pass_check = isinstance(self.password, str) and len(self.password) > 0 if user_check is False: logger.error('Username invalid') return False if pass_check is False: logger.error('Password invalid') return False response, _ = Helpers.call_api('/cloud/v1/user/login', 'post', json_object=Helpers.req_body( self, 'login')) if Helpers.code_check(response) and 'result' in response: self.token = response.get('result').get('token') self.account_id = response.get('result').get('accountID') self.enabled = True logger.debug('Login successful') logger.debug('token %s', self.token) logger.debug('account_id %s', self.account_id) return True logger.error('Error logging in with username and password') return False
def get_details(self) -> None: """Get details of tunable bulb.""" body = helpers.req_body(self.manager, 'bypass') body['cid'] = self.cid body['jsonCmd'] = {'getLightStatus': 'get'} body['configModule'] = self.config_module r, _ = helpers.call_api( '/cloud/v1/deviceManaged/bypass', 'post', headers=helpers.req_headers(self.manager), json_object=body, ) if not isinstance(r, dict) or not helpers.code_check(r): logger.debug('Error calling %s', self.device_name) return response = r if response.get('result', {}).get('light') is not None: light = response.get('result', {}).get('light') self.connection_status = 'online' self.device_status = light.get('action', 'off') if self.dimmable_feature: self._brightness = light.get('brightness') if self.color_temp_feature: self._color_temp = light.get('colorTempe') elif response.get('code') == -11300027: logger.debug('%s device offline', self.device_name) self.connection_status = 'offline' self.device_status = 'off' else: logger.debug( '%s - Unknown return code - %d with message %s', self.device_name, response.get('code'), response.get('msg'), )
def set_brightness(self, brightness: int) -> bool: """Set brightness of dimmable bulb.""" 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') 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))) body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'on' body['brightNess'] = str(brightness) r, _ = helpers.call_api( '/SmartBulb/v1/device/updateBrightness', 'put', headers=helpers.req_headers(self.manager), json_object=body, ) if helpers.code_check(r): self._brightness = brightness return True logger.debug('Error setting brightness for %s', self.device_name) return False
def mode_toggle(self, mode: str) -> bool: """Set purifier mode - sleep or manual.""" if mode.lower() not in self.modes: logger.debug('Invalid purifier mode used - %s', mode) return False head, body = self.build_api_dict('setPurifierMode') if not head and not body: return False body['payload']['data'] = {'mode': mode.lower()} if mode == 'manual': body['payload'] = { 'data': { 'id': 0, 'level': 1, 'type': 'wind' }, 'method': 'setLevel', 'type': 'APP' } r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json_object=body, ) if Helpers.code_check(r): return True logger.debug('Error setting purifier mode') return False
def get_details(self) -> None: """Get 15A outlet details.""" body = Helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid r, _ = Helpers.call_api( '/15a/v1/device/devicedetail', 'post', headers=Helpers.req_headers(self.manager), json_object=body, ) attr_list = ( 'deviceStatus', 'activeTime', 'energy', 'power', 'voltage', 'nightLightStatus', 'nightLightAutomode', 'nightLightBrightness', ) if Helpers.code_check(r) and all(k in r for k in attr_list): self.device_status = r.get('deviceStatus') self.connection_status = r.get('connectionStatus') self.nightlight_status = r.get('nightLightStatus') self.nightlight_brightness = r.get('nightLightBrightness') self.details = Helpers.build_details_dict(r) else: logger.debug('Unable to get %s details', self.device_name)
def get_details(self) -> None: """Build Core200S/300S Purifier details dictionary.""" head = Helpers.bypass_header() body = Helpers.bypass_body_v2(self.manager) body['cid'] = self.cid body['configModule'] = self.config_module body['payload'] = { 'method': 'getPurifierStatus', 'source': 'APP', 'data': {} } r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) outer_result = r.get('result', {}) inner_result = None if outer_result: inner_result = r.get('result', {}).get('result') if inner_result is not None and Helpers.code_check(r): if outer_result.get('code') == 0: self.build_purifier_dict(inner_result) else: logger.debug('error in inner result dict from purifier') if inner_result.get('configuration', {}): self.build_config_dict(inner_result.get('configuration', {})) else: logger.debug('No configuration found in purifier status') else: logger.debug('Error in purifier response')
def set_night_light_brightness(self, brightness: int) -> bool: """Set target 200S/300S Humidifier night light brightness.""" if not self.night_light: logger.debug('%s is a %s does not have a nightlight', self.device_name, self.device_type) return False if brightness < 0 or brightness > 100: logger.debug("Brightness value must be set between 0 and 100") return False head, body = self.build_api_dict('setNightLightBrightness') if not head and not body: return False body['payload']['data'] = {'night_light_brightness': brightness} r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json_object=body, ) if r is not None and Helpers.code_check(r): return True logger.debug('Error setting humidity') return False
def set_brightness(self, brightness: int): """Set brightness of dimmable bulb.""" if not self.dimmable_feature: logger.debug('%s is not dimmable', self.device_name) return False if isinstance(brightness, int) and (brightness <= 0 or brightness > 100): logger.warning('Invalid brightness') return False body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'on' body['brightNess'] = str(brightness) r, _ = helpers.call_api('/SmartBulb/v1/device/updateBrightness', 'put', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self._brightness = brightness return True logger.debug('Error setting brightness for %s', self.device_name) return False
def set_child_lock(self, mode: bool) -> bool: """Set Core400S child lock.""" if mode not in (True, False): logger.debug('Invalid mode passed to set_child_lock - %s', mode) return False head, body = self.__build_api_dict('setChildLock') if not head and not body: return False body['payload']['data'] = {'child_lock': mode} r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) if r is not None and Helpers.code_check(r): self.details['child_lock'] = mode return True if isinstance(r, dict): logger.debug('Error toggling child lock') else: logger.debug('Error in api return json for %s', self.device_name) return False
def set_mist_level(self, level) -> bool: """Set humidifier mist level with int between 0 - 9.""" try: level = int(level) except ValueError: level = str(level) if level not in self.mist_levels: logger.debug('Humidifier mist level must be between 0 and 9') return False head, body = self.build_api_dict('setVirtualLevel') if not head and not body: return False body['payload']['data'] = {'id': 0, 'level': level, 'type': 'mist'} r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json_object=body, ) if r is not None and Helpers.code_check(r): return True logger.debug('Error setting mist level') return False
def rgb_color_status(self, status: str, red: int = None, blue: int = None, green: int = None) -> bool: """Set faceplate RGB color.""" body = helpers.req_body(self.manager, 'devicestatus') body['status'] = status body['uuid'] = self.uuid head = helpers.req_headers(self.manager) if red is not None and blue is not None and green is not None: body['rgbValue'] = {'red': red, 'blue': blue, 'green': green} r, _ = helpers.call_api('/dimmer/v1/device/devicergbstatus', 'put', headers=head, json=body) if r is not None and helpers.code_check(r): self._rgb_status = status if body.get('rgbValue') is not None: self._rgb_value = {'red': red, 'blue': blue, 'green': green} return True logger.warning('Error turning %s off', self.device_name) return False
def set_automatic_stop(self, mode: bool) -> bool: """Set 300S Humidifier to automatic stop.""" if mode not in (True, False): logger.debug('Invalid mode passed to set_automatic_stop - %s', mode) return False head, body = self.__build_api_dict('setAutomaticStop') if not head and not body: return False body['payload']['data'] = {'enabled': mode} r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) if Helpers.code_check(r): return True if isinstance(r, dict): logger.debug('Error toggling automatic stop') else: logger.debug('Error in api return json for %s', self.device_name) return False
def set_brightness(self, brightness: int) -> bool: """Set brightness of tunable bulb.""" if not self.dimmable_feature: logger.debug('%s is not dimmable', self.device_name) return False if brightness <= 0 or brightness > 100: logger.debug('Invalid brightness') return False body = helpers.req_body(self.manager, 'bypass') body['cid'] = self.cid body['configModule'] = self.config_module if self.device_status == 'off': light_dict = {'action': 'on', 'brightness': brightness} else: light_dict = {'brightness': brightness} body['jsonCmd'] = {'light': light_dict} r, _ = helpers.call_api('/cloud/v1/deviceManaged/bypass', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self._brightness = brightness return True else: self.device_status = 'off' self.connection_status = 'offline' logger.debug('%s offline', self.device_name) logger.debug('Error setting brightness for %s', self.device_name) return False
def toggle_switch(self, toggle: bool) -> bool: """Toggle purifier on/off.""" if not isinstance(toggle, bool): logger.debug('Invalid toggle value for purifier switch') return False head = Helpers.bypass_header() body = Helpers.bypass_body_v2(self.manager) body['cid'] = self.cid body['configModule'] = self.config_module body['payload'] = { 'data': { 'enabled': toggle, 'id': 0 }, 'method': 'setSwitch', 'source': 'APP' } r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) if Helpers.code_check(r): return True logger.debug("Error toggling Core200S/300S purifier - %s", self.device_name) return False
def change_fan_speed(self, speed: int = None) -> bool: """1,2,3,4 or call without argument to increment by 1.""" if self.mode != 'manual': logger.debug('%s not in manual mode, cannot change speed', self.device_name) return False try: level = int(self.speed) except KeyError: logger.debug( 'Cannot change fan speed, no level set for %s', self.device_name, ) return False body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid head = Helpers.req_headers(self.manager) if speed is not None: if speed == level: return True if speed in [1, 2, 3, 4]: level = speed else: logger.debug('Invalid fan speed for %s', self.device_name) return False else: if (level + 1) > 4: level = 1 else: level = int(level + 1) head, body = self.__build_api_dict('setLevel') if not head and not body: return False body['payload']['data'] = { 'id': 0, 'level': level, 'type': 'wind', } r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) if r is not None and Helpers.code_check(r): self.speed = level return True logger.warning('Error changing %s speed', self.device_name) return False
def toggle(self, status): """Toggle dimmable bulb.""" body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = status r, _ = helpers.call_api('/SmartBulb/v1/device/devicestatus', 'put', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.device_status = status return True return False
def get_config(self): """Get configuration and firmware info of tunable bulb.""" body = helpers.req_body(self.manager, 'bypass_config') body['uuid'] = self.uuid r, _ = helpers.call_api('/cloud/v1/deviceManaged/configurations', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.config = helpers.build_config_dict(r) else: logger.debug("Error getting %s config info", self.device_name)
def get_monthly_energy(self) -> None: """Get outdoor outlet monthly energy info and populate energy dict.""" body = Helpers.req_body(self.manager, 'energy_month') body['uuid'] = self.uuid response, _ = Helpers.call_api( '/outdoorsocket15a/v1/device/energymonth', 'post', headers=Helpers.req_headers(self.manager), json_object=body, ) if Helpers.code_check(response): self.energy['month'] = Helpers.build_energy_dict(response) logger.debug('Unable to get %s monthly data', self.device_name)
def get_monthly_energy(self): """Get 10A outlet monthly energy info and populate energy dict.""" body = helpers.req_body(self.manager, 'energy_month') body['uuid'] = self.uuid response, _ = helpers.call_api('/10a/v1/device/energymonth', 'post', headers=helpers.req_headers( self.manager), json=body) if helpers.code_check(response): self.energy['month'] = helpers.build_energy_dict(response) else: logger.debug('Unable to get %s monthly data', self.device_name)
def get_config(self): """Get switch device configuration info.""" body = helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api('/inwallswitch/v1/device/configurations', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.config = helpers.build_config_dict(r) else: logger.warning("Unable to get %s config info", self.device_name)
def get_yearly_energy(self): """Get outdoor outlet yearly energy info and populate energy dict.""" body = helpers.req_body(self.manager, 'energy_year') body['uuid'] = self.uuid response, _ = helpers.call_api( '/outdoorsocket15a/v1/device/energyyear', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(response): self.energy['year'] = helpers.build_energy_dict(response) else: logger.debug('Unable to get %s yearly data', self.device_name)
def get_config(self): """Get configuration info for outdoor outlet.""" body = helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api('/outdoorsocket15a/v1/device/configurations', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.config = helpers.build_config_dict(r) else: logger.debug("Error getting %s config info", self.device_name)
def turn_off_nightlight(self): """Turn Off Nightlight.""" body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['mode'] = 'manual' response, _ = helpers.call_api('/15a/v1/device/nightlightstatus', 'put', headers=helpers.req_headers( self.manager), json=body) if helpers.code_check(response): return True logger.debug("Error turning off %s nightlight", self.device_name)
def get_details(self): """Get details of dimmable bulb.""" body = helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid r, _ = helpers.call_api('/SmartBulb/v1/device/devicedetail', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.connection_status = r.get('connectionStatus') self.device_status = r.get('deviceStatus') if self.dimmable_feature: self._brightness = int(r.get('brightNess')) else: logger.debug('Error getting %s details', self.device_name)
def get_config(self): """Get configuration of dimmable bulb.""" body = helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api('/SmartBulb/v1/device/configurations', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.config = helpers.build_config_dict(r) else: logger.warning("Error getting %s config info", self.device_name)
def get_weekly_energy(self) -> None: """Get 15A outlet weekly energy info and populate energy dict.""" body = Helpers.req_body(self.manager, 'energy_week') body['uuid'] = self.uuid response, _ = Helpers.call_api( '/15a/v1/device/energyweek', 'post', headers=Helpers.req_headers(self.manager), json_object=body, ) if Helpers.code_check(response): self.energy['week'] = Helpers.build_energy_dict(response) else: logger.debug('Unable to get %s weekly data', self.device_name)
def get_config(self) -> None: """Get configuration info for outdoor outlet.""" body = Helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = Helpers.call_api( '/outdoorsocket15a/v1/device/configurations', 'post', headers=Helpers.req_headers(self.manager), json_object=body, ) if Helpers.code_check(r): self.config = Helpers.build_config_dict(r) logger.debug('Error getting %s config info', self.device_name)