Skip to content

Commit

Permalink
Minor rewrite
Browse files Browse the repository at this point in the history
A few changes before merging into main branch:
- Create UnsupportedDevice exception class, to simplify catching exception
  in user code.
- Create read-only properties for available_services and
  service_version.
- Check that service version is supported once (in __init__), to avoid
  code duplication.
- Renamed a few variables for PEP 8 compliance.
- Bumped version
  • Loading branch information
stianaske committed Aug 6, 2017
1 parent dedf9c7 commit 43d9f41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
50 changes: 25 additions & 25 deletions pybotvac/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
# Disable warning due to SubjectAltNameWarning in certificate
requests.packages.urllib3.disable_warnings()

SUPPORTED_SERVICES = ['basic-1', 'minimal-2', 'basic-2']


class UnsupportedDevice(Exception):
pass


class Robot:
"""Data and methods for interacting with a Neato Botvac Connected vacuum robot"""
Expand All @@ -27,8 +33,9 @@ def __init__(self, serial, secret, traits, name=''):

self._url = 'https://nucleo.neatocloud.com/vendors/neato/robots/{0}/messages'.format(self.serial)
self._headers = {'Accept': 'application/vnd.neato.nucleo.v1'}

self.availableServices = self.state['availableServices']

if self.service_version not in SUPPORTED_SERVICES:
raise UnsupportedDevice("Version " + self.service_version + " of service houseCleaning is not known")

def __str__(self):
return "Name: %s, Serial: %s, Secret: %s Traits: %s" % (self.name, self.serial, self.secret, self.traits)
Expand All @@ -49,63 +56,48 @@ def _message(self, json):
response.raise_for_status()
return response

def start_cleaning(self, mode=2, navigationMode=2):
# mode & naivigationMode used if applicable to service version
def start_cleaning(self, mode=2, navigation_mode=2):
# mode & naivigation_mode used if applicable to service version
# mode: 1 eco, 2 turbo
# navigationMode: 1 normal, 2 extra care
serviceVersion = self.availableServices['houseCleaning']
# navigation_mode: 1 normal, 2 extra care

if serviceVersion == 'basic-1':
if self.service_version == 'basic-1':
json = {'reqId': "1",
'cmd': "startCleaning",
'params': {
'category': 2,
'mode': mode,
'modifier': 1}
}
elif serviceVersion == 'minimal-2':
elif self.service_version == 'minimal-2':
json = {'reqId': "1",
'cmd': "startCleaning",
'params': {
'category': 2,
"navigationMode": navigationMode}
"navigationMode": navigation_mode}
}
elif serviceVersion == 'basic-2':
else: # self.service_version == 'basic-2'
json = {'reqId': "1",
'cmd': "startCleaning",
'params': {
'category': 2,
'mode': mode,
'modifier': 1,
"navigationMode": navigationMode}
"navigationMode": navigation_mode}
}
else:
raise Exception("Version " + serviceVersion + " of service houseCleaning is not known")

return self._message(json)

def pause_cleaning(self):
serviceVersion = self.availableServices['houseCleaning']
if serviceVersion not in ['basic-1', 'minimal-2', 'basic-2']:
raise Exception("Version " + serviceVersion + " of service houseCleaning is not known")
return self._message({'reqId': "1", 'cmd': "pauseCleaning"})

def resume_cleaning(self):
serviceVersion = self.availableServices['houseCleaning']
if serviceVersion not in ['basic-1', 'minimal-2', 'basic-2']:
raise Exception("Version " + serviceVersion + " of service houseCleaning is not known")
return self._message({'reqId': "1", 'cmd': "resumeCleaning"})

def stop_cleaning(self):
serviceVersion = self.availableServices['houseCleaning']
if serviceVersion not in ['basic-1', 'minimal-2', 'basic-2']:
raise Exception("Version " + serviceVersion + " of service houseCleaning is not known")
return self._message({'reqId': "1", 'cmd': "stopCleaning"})

def send_to_base(self):
serviceVersion = self.availableServices['houseCleaning']
if serviceVersion not in ['basic-1', 'minimal-2', 'basic-2']:
raise Exception("Version " + serviceVersion + " of service houseCleaning is not known")
return self._message({'reqId': "1", 'cmd': "sendToBase"})

def get_robot_state(self):
Expand Down Expand Up @@ -135,6 +127,14 @@ def schedule_enabled(self, enable):
def state(self):
return self.get_robot_state().json()

@property
def available_services(self):
return self.state['availableServices']

@property
def service_version(self):
return self.available_services['houseCleaning']


class Auth(requests.auth.AuthBase):
"""Create headers for request authentication"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='pybotvac',
version='0.0.3',
version='0.0.4',
description='Python package for controlling Neato pybotvac Connected vacuum robot',
author='Stian Askeland',
author_email='[email protected]',
Expand Down

0 comments on commit 43d9f41

Please sign in to comment.