Giter VIP home page Giter VIP logo

Comments (3)

strunker avatar strunker commented on July 30, 2024 1

you need to do use json= as the body in the post.

See my implementation below. You can ignore all the other nonsense in here, just look at my global variables and the api.post area. I have an alexa skill I develop for my home use as well as a watch app for my samsung watch so a lot of the hacky code in that function exists in support of those two considerations.

The unique token you obviously have already, and the device guids you have to query for with /devices get request.

import requests as api

botToken = 'INSERT YOUR PERSONAL TOKEN HERE'
botHeader = {'Authorization':botToken,'Content-Type':'application/json; charset=utf8'}
bedroomCurtain = 'INSERT UNIQUE DEVICE ID HERE'
livingroomCurtain = 'INSERT UNIQUE DEVICE ID HERE'
openCurtains = {'command':'turnOn'}
closeCurtains = {'command':'turnOff'}

def SwitchBotCommand(command):
    command = command.lower()
    if "living" in command:
        localRoom = livingroomCurtain
        localroomText = "living room"
    elif "bed" in command:
        localRoom = bedroomCurtain
        localroomText = "bedroom"
    else:
        print("SwitchBot Problem: {} Is Not A Valid Room.".format(command))
        return("Bad-You stated an invalid room, please try again.") 
    try:
        if "open" in command:
            data = api.post('https://api.switch-bot.com/v1.0/devices/{}/commands'.format(localRoom),headers=botHeader,json=openCurtains)
            return("Good-Opening {} curtain.".format(localroomText))
        elif "close" in command:
            data = api.post('https://api.switch-bot.com/v1.0/devices/{}/commands'.format(localRoom),headers=botHeader,json=closeCurtains)
            return("Good-Closing {} curtain.".format(localroomText))
        else:
            print("SwitchBot Problem: {} Is Not A Valid Command.".format(command))
            return("Bad-You stated an invalid command, please try again.")
    except Exception as E:
        print("SwitchBot Error: {}".format(E))
        return("Bad-There was an error talking to the SwitchBot.")

from switchbotapi.

nigels0 avatar nigels0 commented on July 30, 2024

That’s exactly it! Thanks.

my command changed to:

response = requests.post(url, headers=HEADERS, json=params)

from switchbotapi.

strunker avatar strunker commented on July 30, 2024

That’s exactly it! Thanks.

my command changed to:

response = requests.post(url, headers=HEADERS, json=params)

Very good :) you may want to mark your issue resolved and close at this point. TY

from switchbotapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.