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_yearly_energy(self): """Get 15A outlet yearly energy info and populate energy dict.""" 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.code_check(response): self.energy['year'] = helpers.build_energy_dict(response) else: logger.debug('Unable to get %s yearly data', self.device_name)
def turn_off(self): """Turn 7A outlet off - return True if successful.""" _, status_code = helpers.call_api( '/v1/wifi-switch-1.3/' + self.cid + '/status/off', 'put', headers=helpers.req_headers(self.manager) ) if status_code is not None and status_code == 200: self.device_status = 'off' return True else: logger.warning('Error turning %s off', self.device_name) return False
def test_7a_onoff(self, caplog, api_mock): """Test 7A outlet on/off methods.""" self.mock_api.return_value = ("response", 200) vswitch7a = VeSyncOutlet7A(DEV_LIST_DETAIL, self.vesync_obj) on = vswitch7a.turn_on() head = helpers.req_headers(self.vesync_obj) self.mock_api.assert_called_with( '/v1/wifi-switch-1.3/' + vswitch7a.cid + '/status/on', 'put', headers=head) assert on off = vswitch7a.turn_off() self.mock_api.assert_called_with( '/v1/wifi-switch-1.3/' + vswitch7a.cid + '/status/off', 'put', headers=head) assert off
def toggle(self, status) -> bool: """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_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_config(self): """Get 15A outlet configuration info.""" body = helpers.req_body(self.manager, 'devicedetail') body['method'] = 'configurations' body['uuid'] = self.uuid r, _ = helpers.call_api('/15a/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("Unable to get %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 test_7a_yearly(self, api_mock): """Test 7A outlet yearly energy API call and energy dict.""" self.mock_api.return_value = ENERGY_HISTORY vswitch7a = VeSyncOutlet7A(DEV_LIST_DETAIL, self.vesync_obj) vswitch7a.get_yearly_energy() self.mock_api.assert_called_with( '/v1/device/' + vswitch7a.cid + '/energy/year', 'get', headers=helpers.req_headers(self.vesync_obj)) energy_dict = vswitch7a.energy['year'] assert energy_dict['energy_consumption_of_today'] == 1 assert energy_dict['cost_per_kwh'] == 1 assert energy_dict['max_energy'] == 1 assert energy_dict['total_energy'] == 1 assert energy_dict['data'] == [1, 1]
def get_config(self) -> None: """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_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)
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 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) )
def toggle(self, status): """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): 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_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
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) )
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 stop_timer(self, timerID): """Stops the timer with ID timerID - return True if successful.""" body = helpers.req_body(self.manager, 'devicestatus') body['timerId'] = timerID body['timerStatus'] = '0' response, _ = helpers.call_api('/10a/v1/app/updateTimerStatus', 'put', headers=helpers.req_headers( self.manager), json=body) if helpers.code_check(response): return True logger.warning('Error stopping timer %s on device %s', timerID, self.device_name) return False
def turn_off_nightlight(self) -> bool: """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_object=body, ) if Helpers.code_check(response): return True logger.debug('Error turning off %s nightlight', self.device_name) return False
def get_config(self) -> None: """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 turn_on(self) -> bool: """Turn on switch device.""" 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.code_check(r): self.device_status = 'on' return True logger.warning('Error turning %s on', self.device_name) return False
def get_config(self) -> None: """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_object=body, ) if r is not None and Helpers.code_check(r): self.config = Helpers.build_config_dict(r) else: logger.debug('Unable to get config info for %s', 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) -> None: """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 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 turn_off(self) -> bool: """Turn 10A outlet off - return True if successful.""" body = Helpers.req_body(self.manager, 'devicestatus') body['uuid'] = self.uuid body['status'] = 'off' response, _ = Helpers.call_api( '/10a/v1/device/devicestatus', 'put', headers=Helpers.req_headers(self.manager), json_object=body, ) if Helpers.code_check(response): self.device_status = 'off' return True logger.warning('Error turning %s off', self.device_name) return False
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)