natekspencer / pylitterbot

Python package for controlling a Whisker connected self-cleaning litter boxes and feeders
MIT License
87 stars 12 forks source link

Accessing Commands / Robots #102

Closed lenoth closed 1 year ago

lenoth commented 1 year ago

Hi, I'm running into a problem where I can authenticate my user but nothing comes back when listing robots or running commands. Am I doing something wrong? Thank you in advance!

import asyncio

from flask import Flask, request, jsonify
from flask_cors import CORS
from pylitterbot import Account

app = Flask(__name__)
CORS(app)

async def get_robots():
    account = Account()
    return account.robots

@app.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    username = data['username']
    password = data['password']

    async def login():
      account = Account()
      await account.connect(username=username, password=password, load_robots=True)

    asyncio.run(login())
    return jsonify({'status': 'success'})

@app.route('/logout', methods=['POST'])
def logout():
    async def logout():
        account = Account()
        await account.disconnect()

    asyncio.run(logout())
    return jsonify({'status': 'success'})

@app.route('/robots', methods=['GET'])
def list_robots():
    robots = asyncio.run(get_robots())
    return jsonify([str(robot) for robot in robots])

@app.route('/clean', methods=['POST'])
def clean():
    async def clean_all():
        robots = await get_robots()
        for robot in robots:
            await robot.start_cleaning()

    asyncio.run(clean_all())
    return jsonify({'status': 'success'})

@app.route('/nightlight', methods=['POST'])
def night_light():
    async def set_night_light_all():
        robots = await get_robots()
        for robot in robots:
            await robot.set_night_light()

    asyncio.run(set_night_light_all())
    return jsonify({'status': 'success'})

if __name__ == '__main__':
    app.run()
Screen Shot 2023-01-10 at 2 05 42 AM Screen Shot 2023-01-10 at 2 05 54 AM
natekspencer commented 1 year ago

It looks like you are creating a new account object when getting the robots which means that account is not authenticated and won't have any information.

lenoth commented 1 year ago

Ah, good catch. Do you know if robots only load if the user has a Litter Robot Connect account?

Using this snippet below I can't load them either. I'm able to authenticate with the correct account, just can't run commands following that.

import asyncio

from pylitterbot import Account

# Set email and password for initial authentication.
username = AUTHENTICATION_EMAIL
password = AUTHENTICATION_PASSWORD

async def main():
    # Create an account.
    account = Account()

    try:
        # Connect to the API and load robots.
        await account.connect(username=username, password=password, load_robots=True)

        # Print robots associated with account.
        print("Robots:")
        for robot in account.robots:
            print(robot)
    finally:
        # Disconnect from the API.
        await account.disconnect()

if __name__ == "__main__":
    asyncio.run(main())
Screen Shot 2023-01-10 at 9 41 02 AM
natekspencer commented 1 year ago

Do you know if robots only load if the user has a Litter Robot Connect account?

Yes, you can only see robots associated with your account. If you don't have any robots, there won't be anything to display. Your question makes it seem like you don't have a Litter-Robot or Feeder-Robot, or am I misinterpreting?

lenoth commented 1 year ago

I have the litter robot 3. There is an extra option to buy the Connect which enables your account to use their mobile app.

Wasn't sure if that was a requirement to load my robot or not.

image

natekspencer commented 1 year ago

Ahh yes, this only supports reading and controlling connected robots. I'm curious what your use case is with this module and trying to access your LR3. At most you'd only be able to see a serial number or something, but I don't know the endpoints that are called to just get robots that are registered but have no cloud accessibility.

lenoth commented 1 year ago

Ah! I'll have to get the connection then. Working on bit of a hobby project to control most of my home from my own personal built assistant on a React Native app and assign automations. (Already have it setup through SmartThings & Alexa, but wanted my own that I could experiment with).

Awesome library though, you deserve a beer! Thanks for the help.