예제 #1
0
    def get_details(self):
        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.check_response(r, '15a_detail')
                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 {0} details'.format(self.device_name)
            )
예제 #2
0
    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 and pass_check:
            response, _ = helpers.call_api('/cloud/v1/user/login',
                                           'post',
                                           json=helpers.req_body(
                                               self, 'login'))

            if response and helpers.check_response(response, 'login'):
                self.token = response['result']['token']
                self.account_id = response['result']['accountID']
                self.enabled = True

                return True
            else:
                logger.error('Error logging in with username and password')
                return False

        else:
            if user_check is False:
                logger.error('Username invalid')
            if pass_check is False:
                logger.error('Password invalid')

        return False
예제 #3
0
    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.check_response(response, 'get_devices'):
            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)
예제 #4
0
    def set_brightness(self, brightness: int):
        """Set brightness of vesync bulb"""
        if self.dimmable_feature:
            if brightness > 0 and brightness <= 100:
                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.check_response(r, 'bulb_toggle'):
                    self._brightness = brightness
                    return True
                else:
                    logger.debug('Error setting brightness for {}'.format(
                        self.device_name))
                    return False
            else:
                logger.warning('Invalid brightness')
                return False
        else:
            logger.debug('{} is not dimmable - ', self.device_name)
예제 #5
0
    def get_yearly_energy(self):
        r, _ = helpers.call_api(
            '/v1/device/' + self.cid + '/energy/year',
            'get',
            headers=helpers.req_headers(self.manager)
        )

        if r is not None and helpers.check_response(r, '7a_energy'):
            self.energy['year'] = helpers.build_energy_dict(r)
        else:
            logger.debug(
                'Unable to get {0} yearly data'.format(self.device_name))
예제 #6
0
    def get_config(self):
        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.check_response(r, 'config'):
            self.config = helpers.build_config_dict(r)
예제 #7
0
 def get_details(self):
     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.check_response(r, 'bulb_detail'):
         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 {} details'.format(self.device_name))
예제 #8
0
 def toggle(self, status):
     """Toggle vesync 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.check_response(r, 'bulb_toggle'):
         self.device_status = status
         return True
     else:
         return False
예제 #9
0
    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
        )

        return helpers.check_response(response, '15a_ntlight')
예제 #10
0
    def change_fan_speed(self, speed: int = None) -> bool:
        """Adjust Fan Speed by Specifying 1,2,3 as argument or 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.check_response(r, 'airpur_status'):
            self.details['level'] = body['level']
            return True
        else:
            logger.warning(
                'Error changing {} speed'.format(self.device_name))
            return False
예제 #11
0
    def get_monthly_energy(self):
        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=body
            )

        if helpers.check_response(response, 'outdoor_energy'):
            self.energy['month'] = helpers.build_energy_dict(response)
        else:
            logger.debug(
                'Unable to get {} monthly data'.format(self.device_name)
            )
예제 #12
0
    def turn_on(self):
        body = helpers.req_body(self.manager, 'devicestatus')
        body['status'] = 'on'
        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 = 'on'
            return True
        else:
            logger.warning('Error turning {} on'.format(self.device_name))
            return False
예제 #13
0
    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))
예제 #14
0
    def get_yearly_energy(self):
        body = helpers.req_body(self.manager, 'energy_year')
        body['uuid'] = self.uuid

        response, _ = helpers.call_api(
            '/15a/v1/device/energyyear',
            'post',
            headers=helpers.req_headers(self.manager),
            json=body
        )

        if helpers.check_response(response, '15a_energy'):
            self.energy['year'] = helpers.build_energy_dict(response)
        else:
            logger.debug(
                'Unable to get {0} yearly data'.format(self.device_name)
            )
예제 #15
0
    def get_details(self):
        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.check_response(r, '10a_detail'):
            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 {0} details'.format(self.device_name))
예제 #16
0
    def turn_off(self):
        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.check_response(response, '15a_toggle'):
            self.device_status = 'off'
            return True
        else:
            logger.warning('Error turning {} off'.format(self.device_name))
            return False
예제 #17
0
    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
예제 #18
0
    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']:
            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
예제 #19
0
    def get_details(self):
        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.check_response(r, 'outdoor_detail'):
            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 {} details'.format(self.device_name))
예제 #20
0
    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))
예제 #21
0
    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
예제 #22
0
    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))