def get_devices(self) -> tuple: """Return tuple listing outlets, switches, and fans of devices.""" outlets = [] switches = [] fans = [] bulbs = [] if not self.enabled: return None self.in_process = True 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'] outlets, switches, fans, bulbs = 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 (outlets, switches, fans, bulbs)
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_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 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 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_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_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_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_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 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_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 get_weekly_energy(self): """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=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): """Get configuration info for air purifier.""" body = helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api('/131airpurifier/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 config info for %s", self.device_name)
def turn_off(self): """Turn off switch device.""" 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.code_check(r): self.device_status = 'off' return True logger.warning('Error turning %s off', self.device_name) return False
def change_fan_speed(self, speed: int = None) -> bool: """Adjust Fan Speed for air purifier. Specifying 1,2,3 as argument or call without argument to cycle through speeds increasing by one. """ if self.mode != 'manual': logger.debug('{} not in manual mode, cannot change speed'.format( self.device_name)) return False try: level = self.details['level'] except KeyError: logger.debug('Cannot change fan speed, no level set for {}'.format( 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 elif speed in [1, 2, 3]: body['level'] = speed else: logger.debug('Invalid fan speed for {}'.format( self.device_name)) return False else: if (level + 1) > 3: body['level'] = 1 else: body['level'] = int(level + 1) r, _ = helpers.call_api('/131airPurifier/v1/device/updateSpeed', 'put', json=body, headers=head) if r is not None and helpers.code_check(r): self.details['level'] = body['level'] return True logger.warning('Error changing %s speed', self.device_name) return False
def get_details(self): """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 turn_on(self): """Turn 15A outlet on - return True if successful.""" body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'on' 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 = 'on' return True _LOGGER.warning('Error turning %s on', self.device_name) return False
def get_details(self): """Get switch device details.""" 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.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) else: logger.debug('Error getting %s details', self.device_name)
def turn_on(self): """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
def display_toggle(self, mode: str) -> bool: """Toggle the display on/off.""" if mode not in ['on', 'off']: _LOGGER.warning("Invalid display power option for %s - %s", self.device_name, mode) return False head = helpers.req_headers(self.manager) body = helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = mode r, _ = helpers.call_api('/131airPurifier/v1/device/updateScreen', 'put', json=body, headers=head) if r is not None and helpers.code_check(r): self.details['screen_status'] = mode return True
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.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): """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 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 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 get_details(self): """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.code_check(r): 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 %s details', self.device_name)
def set_brightness(self, brightness: int): """Set brightness of dimmer - 1 - 100.""" if not isinstance(brightness, int) and \ (brightness <= 0 or brightness > 100): _LOGGER.warning('Invalid brightness') return False 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) return False