def startPairing(info): # Start OAuth2 session apiKey = apiKeyStorage().requestKey("neato") oauthSession = OAuthSession(client_id=apiKey.data("clientId"), client_secret=apiKey.data("clientSecret"), redirect_uri="https://127.0.0.1:8888", vendor=Neato()) oauthSessions[info.transactionId] = oauthSession authorizationUrl = oauthSession.get_authorization_url() info.oAuthUrl = authorizationUrl info.finish(nymea.ThingErrorNoError)
def setupThing(info): # Setup for the account if info.thing.thingClassId == accountThingClassId: logger.log("SetupThing for account:", info.thing.name) pluginStorage().beginGroup(info.thing.id) token = json.loads(pluginStorage().value("token")) logger.log("setup", token) pluginStorage().endGroup() try: oAuthSession = OAuthSession(token=token) # Login went well, finish the setup info.finish(nymea.ThingErrorNoError) except Exception as e: # Login error logger.warn("Error setting up neato account:", str(e)) info.finish(nymea.ThingErrorAuthenticationFailure, str(e)) return # Mark the account as logged-in and connected info.thing.setStateValue(accountLoggedInStateTypeId, True) info.thing.setStateValue(accountConnectedStateTypeId, True) # Create an account session on the session to get info about the login account = Account(oAuthSession) accountsMap[info.thing] = account # List all robots associated with account logger.log("account created. Robots:", account.robots) thingDescriptors = [] for robot in account.robots: logger.log("Found new robot:", robot.serial) # Check if this robot is already added in nymea found = False for thing in myThings(): logger.log("Comparing to existing robot:", thing.name) if thing.thingClassId == robotThingClassId and thing.paramValue( robotThingSerialParamTypeId) == robot.serial: logger.log("Already have this robot in the system") # Yep, already here... skip it found = True break if found: continue logger.log("Adding new robot to the system with parent", info.thing.id) thingDescriptor = nymea.ThingDescriptor(robotThingClassId, robot.name, parentId=info.thing.id) thingDescriptor.params = [ nymea.Param(robotThingSerialParamTypeId, robot.serial), nymea.Param(robotThingSecretParamTypeId, robot.secret) ] thingDescriptors.append(thingDescriptor) # And let nymea know about all the users robots autoThingsAppeared(thingDescriptors) # If no poll timer is set up yet, start it now logger.log("Creating polltimer") global pollTimer if pollTimer is None: pollTimer = threading.Timer(5, pollService) pollTimer.start() return # Setup for the robots if info.thing.thingClassId == robotThingClassId: logger.log("SetupThing for robot:", info.thing.name) serial = info.thing.paramValue(robotThingSerialParamTypeId) secret = info.thing.paramValue(robotThingSecretParamTypeId) try: robot = Robot(serial, secret, info.thing.name) thingsAndRobots[info.thing] = robot refreshRobot(info.thing) except Exception as e: logger.warn("Error setting up robot:", e) info.finish(nymea.ThingErrorHardwareFailure, str(e)) return info.thing.setStateValue(robotConnectedStateTypeId, True) # set up polling for robot status info.finish(nymea.ThingErrorNoError) return
def setupThing(info): # Setup for the account if info.thing.thingClassId == accountThingClassId: pluginStorage().beginGroup(info.thing.id) token = json.loads(pluginStorage().value("token")) logger.log("setup", token) pluginStorage().endGroup() try: oAuthSession = OAuthSession(token=token) # Login went well, finish the setup info.finish(nymea.ThingErrorNoError) except: # Login error info.finish(nymea.ThingErrorAuthenticationFailure, "Login error") return # Mark the account as logged-in and connected info.thing.setStateValue(accountLoggedInStateTypeId, True) info.thing.setStateValue(accountConnectedStateTypeId, True) # Create an account session on the session to get info about the login account = Account(oAuthSession) # List all robots associated with account logger.log("account created. Robots:", account.robots) logger.log("Persistent maps: ", account.persistent_maps) mapDict = account.persistent_maps mapKeys = mapDict.keys() logger.log("Keys: ", mapKeys) mapValues = mapDict.values() logger.log("Values: ", mapValues) thingDescriptors = [] for robot in account.robots: logger.log(robot) # Check if this robot is already added in nymea found = False for thing in myThings(): if thing.paramValue( robotThingSerialParamTypeId) == robot.serial: # Yep, already here... skip it found = True continue if found: continue thingDescriptor = nymea.ThingDescriptor(robotThingClassId, robot.name) logger.log("MapID for Serial: ", robot.serial, mapDict[robot.serial]) mapInfo = mapDict[robot.serial] logger.log("Type mapInfo: ", type(mapInfo)) logger.log("Contents mapInfo: ", mapInfo) mapInfo2 = mapInfo[0] logger.log("MapInfo2 type: ", type(mapInfo2), " MapInfo2 contents: ", mapInfo2) mapId = mapInfo2['id'] logger.log("MapId type: ", type(mapId), " MapId contents: ", mapId) mapName = mapInfo2['name'] logger.log("MapName type: ", type(mapName), " MapName contents: ", mapName) mapIDshort = mapId thingDescriptor.params = [ nymea.Param(robotThingSerialParamTypeId, robot.serial), nymea.Param(robotThingSecretParamTypeId, robot.secret), nymea.Param(robotThingMapIdParamTypeId, mapIDshort) ] thingDescriptors.append(thingDescriptor) # And let nymea know about all the users robots autoThingsAppeared(thingDescriptors) # return # If no poll timer is set up yet, start it now logger.log("Creating polltimer") global pollTimer pollTimer = threading.Timer(5, pollService) pollTimer.start() return # Setup for the robots if info.thing.thingClassId == robotThingClassId: serial = info.thing.paramValue(robotThingSerialParamTypeId) secret = info.thing.paramValue(robotThingSecretParamTypeId) robot = Robot(serial, secret, info.thing.name) thingsAndRobots[info.thing] = robot logger.log(robot.get_robot_state()) # set up polling for robot status info.finish(nymea.ThingErrorNoError) return
# Set your vendor vendor = Neato() ########################## # Authenticate via Email and Password ########################## # session = PasswordSession(email=email, password=password, vendor=vendor) # account = Account(session) ########################## # Authenticate via OAuth2 ########################## session = OAuthSession( client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, vendor=vendor, ) authorization_url = session.get_authorization_url() print("Visit: " + authorization_url) authorization_response = input("Enter the full callback URL: ") token = session.fetch_token(authorization_response) account = Account(session) ########################## # Authenticate via One Time Password ########################## # session = PasswordlessSession(client_id=client_id, vendor=vendor) # session.send_email_otp(email) # code = input("Enter the code: ") # session.fetch_token_passwordless(email, code)