def turn_off(self) -> bool: """Turn 15A outlet off - return True if successful.""" body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'off' response, _ = Helpers.call_api( '/15a/v1/device/devicestatus', 'put', headers=Helpers.req_headers(self.manager), json=body, ) if Helpers.code_check(response): self.device_status = 'off' return True logger.warning('Error turning %s off', self.device_name) return False
def toggle(self, status) -> bool: """Toggle power for outdoor outlet.""" body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = status body['switchNo'] = self.sub_device_no response, _ = Helpers.call_api( '/outdoorsocket15a/v1/device/devicestatus', 'put', headers=Helpers.req_headers(self.manager), json=body, ) if Helpers.code_check(response): self.device_status = status return True logger.warning('Error turning %s %s', self.device_name, status) return False
def get_details(self): body = helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api( '/inwallswitch/v1/device/devicedetail', 'post', headers=head, json=body ) if r is not None and helpers.check_response(r, 'walls_detail'): self.device_status = r.get('deviceStatus', self.device_status) self.details['active_time'] = r.get('activeTime', 0) self.connection_status = r.get('connectionStatus', self.connection_status) else: logger.debug('Error getting {} details'.format(self.device_name))
def turn_off(self): body = helpers.req_body(self.manager, 'devicestatus') body['status'] = 'off' body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api( '/inwallswitch/v1/device/devicestatus', 'put', headers=head, json=body ) if r is not None and helpers.check_response(r, 'walls_toggle'): self.device_status = 'off' return True else: logger.warning('Error turning {} off'.format(self.device_name)) return False
def turn_on(self) -> bool: """Turn Air Purifier on.""" if self.device_status != 'on': body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'on' head = Helpers.req_headers(self.manager) r, _ = Helpers.call_api('/131airPurifier/v1/device/deviceStatus', 'put', json=body, headers=head) if r is not None and Helpers.code_check(r): self.device_status = 'on' return True logger.warning('Error turning %s on', self.device_name) return False 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 toggle(self, status) -> bool: """Toggle tunable bulb.""" body = helpers.req_body(self.manager, 'bypass') body['cid'] = self.cid body['configModule'] = self.config_module body['jsonCmd'] = {'light': {'action': status}} r, _ = helpers.call_api( '/cloud/v1/deviceManaged/bypass', 'post', headers=helpers.req_headers(self.manager), json=body, ) if r.get('code') == 0: 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_details(self) -> None: """Get 10A outlet details.""" body = Helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid r, _ = Helpers.call_api( '/10a/v1/device/devicedetail', 'post', headers=Helpers.req_headers(self.manager), json=body, ) if Helpers.code_check(r): self.device_status = r.get('deviceStatus', self.device_status) self.connection_status = r.get('connectionStatus', self.connection_status) self.details = Helpers.build_details_dict(r) else: logger.debug('Unable to get %s details', self.device_name)
def get_details(self): """Get 7A outlet details.""" r, _ = helpers.call_api( '/v1/device/' + self.cid + '/detail', 'get', headers=helpers.req_headers(self.manager) ) if r is not None and all(x in r for x in self.det_keys): self.device_status = r.get('deviceStatus', self.device_status) self.details['active_time'] = r.get('activeTime', 0) self.details['energy'] = r.get('energy', 0) power = r.get('power', '0:0') power = round(float(helpers.calculate_hex(power)), 2) self.details['power'] = power voltage = r.get('voltage', '0:0') voltage = round(float(helpers.calculate_hex(voltage)), 2) self.details['voltage'] = voltage else: logger.debug('Unable to get %s details', self.device_name)
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 get_details(self): r, _ = helpers.call_api( '/v1/device/' + self.cid + '/detail', 'get', headers=helpers.req_headers(self.manager) ) if r is not None and helpers.check_response(r, '7a_detail'): self.device_status = r.get('deviceStatus', self.device_status) self.details['active_time'] = r.get('activeTime', 0) self.details['energy'] = r.get('energy', 0) power = r.get('power', '0:0') power = round(float(helpers.calculate_hex(power)), 2) self.details['power'] = power voltage = r.get('voltage', '0:0') voltage = round(float(helpers.calculate_hex(voltage)), 2) self.details['voltage'] = voltage else: logger.debug('Unable to get {0} details'.format( self.device_name))
def get_details(self): """Get details for outdoor outlet.""" body = helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid r, _ = helpers.call_api('/outdoorsocket15a/v1/device/devicedetail', 'post', headers=helpers.req_headers(self.manager), json=body) if helpers.code_check(r): self.details = helpers.build_details_dict(r) self.connection_status = r.get('connectionStatus') dev_no = self.sub_device_no sub_device_list = r.get('subDevices') if sub_device_list and dev_no <= len(sub_device_list): self.device_status = sub_device_list[( dev_no + -1)].get('subDeviceStatus') else: logger.debug('Unable to get %s details', self.device_name)
def turn_off(self): """Turn Air Purifier Off""" if self.device_status == 'on': body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'off' head = helpers.req_headers(self.manager) r, _ = helpers.call_api( '/131airPurifier/v1/device/deviceStatus', 'put', json=body, headers=head ) if r is not None and helpers.check_response(r, 'airpur_status'): self.device_status = 'off' return True else: logger.warning('Error turning {} off'.format(self.device_name)) return False
def toggle(self, status): """Toggle power for outdoor outlet""" body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = status body['switchNo'] = self.sub_device_no response, _ = helpers.call_api( '/outdoorsocket15a/v1/device/devicestatus', 'put', headers=helpers.req_headers(self.manager), json=body ) if helpers.check_response(response, 'outdoor_toggle'): self.device_status = status return True else: logger.warning( 'Error turning {} {}'.format(self.device_name, status)) return False
def mode_toggle(self, mode: str) -> bool: """Set mode to manual, auto or sleep.""" head = Helpers.req_headers(self.manager) body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid if mode != self.mode and mode in ['sleep', 'auto', 'manual']: body['mode'] = mode if mode == 'manual': body['level'] = 1 r, _ = Helpers.call_api('/131airPurifier/v1/device/updateMode', 'put', json=body, headers=head) if r is not None and Helpers.code_check(r): self.mode = mode return True logger.warning('Error setting %s mode - %s', self.device_name, mode) return False
def get_details(self): """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=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.details = helpers.build_details_dict(r) else: logger.debug('Unable to get %s details', self.device_name)
def set_display(self, mode: bool) -> bool: """Toggle display on/off.""" if not isinstance(mode, bool): logger.debug("Mode must be True or False") return False head, body = self.build_api_dict('setIndicatorLightSwitch') body['payload']['data'] = {'enabled': mode, 'id': 0} 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 toggling 300S display - %s", self.device_name) return False
def set_display(self, mode: bool) -> bool: """Toggle display on/off.""" if not isinstance(mode, bool): logger.debug("Mode must be True or False") return False head, body = self.__build_api_dict('setDisplay') body['payload']['data'] = {'state': mode} 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 300S display - %s", self.device_name) return False
def switch_toggle(self, status: str) -> bool: """Toggle switch status.""" if status not in ['on', 'off']: logger.debug('Invalid status passed to wall switch') return False body = helpers.req_body(self.manager, 'devicestatus') body['status'] = status body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api('/dimmer/v1/device/devicestatus', 'put', headers=head, json=body) if r is not None and helpers.code_check(r): self.device_status = status return True logger.warning('Error turning %s %s', self.device_name, status) return False
def edit_timer(self, timerID, seconds, action="off"): """Updates the timer given by timerID, setting the number of seconds for the timer to delay. Use either "on" or "off" for action (what the outlet will do when the time is done).""" body = helpers.req_body(self.manager, 'devicestatus') body['timerId'] = timerID body['status'] = '0' # timer does not start automatically when added body['uuid'] = self.uuid body['action'] = action body['counterTime'] = seconds response, _ = helpers.call_api('/10a/v1/app/updateTimer', 'put', headers=helpers.req_headers( self.manager), json=body) if helpers.code_check(response): return True logger.warning('Error editing timer %s on device %s', timerID, self.device_name) return False
def get_details(self) -> None: """Build Bypass 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_object=body, ) if not isinstance(r, dict): logger.debug('Error in purifier response') return if not isinstance(r.get('result'), dict): logger.debug('Error in purifier response') return 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 mode_toggle(self, mode: str) -> bool: """Set purifier mode - sleep or manual.""" if mode.lower() not in ['sleep', 'manual']: logger.debug('Invalid purifier mode used (sleep or manual)- %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()} r, _ = Helpers.call_api( '/cloud/v2/deviceManaged/bypassV2', method='post', headers=head, json=body, ) if Helpers.code_check(r): return True logger.debug('Error setting purifier mode') return False
def set_brightness(self, brightness: int) -> bool: """Set brightness of dimmer - 1 - 100.""" if isinstance(brightness, int) and (brightness > 0 or brightness <= 100): body = helpers.req_body(self.manager, 'devicestatus') body['brightness'] = brightness body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api('/dimmer/v1/device/updatebrightness', 'put', headers=head, json=body) if r is not None and helpers.code_check(r): self._brightness = brightness return True logger.warning('Error setting %s brightness', self.device_name) else: logger.warning('Invalid brightness') return False
def get_details(self) -> None: """Get dimmer switch details.""" body = helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api('/dimmer/v1/device/devicedetail', 'post', headers=head, json=body) if r is not None and helpers.code_check(r): self.device_status = r.get('deviceStatus', self.device_status) self.details['active_time'] = r.get('activeTime', 0) self.connection_status = r.get('connectionStatus', self.connection_status) self._brightness = r.get('brightness') self._rgb_status = r.get('rgbStatus') self._rgb_value = r.get('rgbValue') self._indicator_light = r.get('indicatorlightStatus') else: logger.debug('Error getting %s details', self.device_name)
def get_details(self): """Build details dictionary""" body = helpers.req_body(self.manager, 'devicedetail') body['uuid'] = self.uuid head = helpers.req_headers(self.manager) r, _ = helpers.call_api('/131airPurifier/v1/device/deviceDetail', method='post', headers=head, json=body) if r is not None and helpers.check_response(r, 'airpur_detail'): self.device_status = r.get('deviceStatus', 'unknown') self.connection_status = r.get('connectionStatus', 'unknown') self.details['active_time'] = r.get('activeTime', 0) self.details['filter_life'] = r.get('filterLife', {}) self.details['screen_status'] = r.get('screenStatus', 'unknown') self.mode = r.get('mode', self.mode) self.details['level'] = r.get('level', 0) self.details['air_quality'] = r.get('airQuality', 'unknown') else: logger.debug('Error getting {} details'.format(self.device_name))
def set_color_temp(self, color_temp: int) -> bool: """Set Color Temperature of Bulb in pct (1 - 100).""" if color_temp < 0 or color_temp > 100: logger.debug('Invalid color temperature - only between 0 and 100') return False body = helpers.req_body(self.manager, 'bypass') body['cid'] = self.cid body['jsonCmd'] = {'light': {}} if self.device_status == 'off': light_dict = {'action': 'on', 'colorTempe': color_temp} else: light_dict = {'colorTempe': color_temp} body['jsonCmd']['light'] = light_dict r, _ = helpers.call_api( '/cloud/v1/deviceManaged/bypass', 'post', headers=helpers.req_headers(self.manager), json_object=body, ) if not helpers.code_check(r): return False if r.get('code') == -11300027: logger.debug('%s device offline', self.device_name) self.connection_status = 'offline' self.device_status = 'off' return False if r.get('code') == 0: self.device_status = 'on' self._color_temp = color_temp return True logger.debug( '%s - Unknown return code - %d with message %s', self.device_name, r.get('code'), r.get('msg'), ) return False
def mode_toggle(self, mode: str) -> bool: """Set mode to manual, auto or sleep""" head = helpers.req_headers(self.manager) body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid if mode != self.mode and mode in ['sleep', 'auto', 'manual']: body["mode"] = mode if mode == 'manual': body['level'] = 1 r, _ = helpers.call_api( '/131airPurifier/v1/device/updateMode', 'put', json=body, headers=head ) if r is not None and helpers.check_response(r, 'airpur_status'): self.mode = mode return True 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 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))) 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_object=body, ) if helpers.code_check(r): self._brightness = brightness return True self.device_status = 'off' self.connection_status = 'offline' logger.debug('%s offline', self.device_name) return False
def set_night_light(self, mode: str) -> bool: """Set night list - on, off or dim.""" if mode.lower() not in ['on', 'off', 'dim']: logger.debug('Invalid nightlight mode used (on, off or dim)- %s', mode) return False head, body = self.__build_api_dict('setNightLight') if not head and not body: return False body['payload']['data'] = {'night_light': mode.lower()} 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['night_light'] = mode.lower() return True logger.debug('Error setting nightlight mode') return False
def set_mist_level(self, level: int) -> bool: """Set humidifier mist level with int between 0 - 9.""" if level < 1 or level > 9: 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=body, ) if Helpers.code_check(r): return True logger.debug('Error setting mist level') return False