Skip to content

A repository for using the switchbot via bluetooth

Notifications You must be signed in to change notification settings

TForest-UwU/Switchy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

Switchy - Python API to control switchbots

This project makes it easier to connect to the switchbot services by combining 2 API's that for me only seemed to work together, yet not alone
This project is focused on the raspberry pi, syscmds therefore only work on that unless you change them in the config file

DISCLAIMER

I did not fully make this myself, i heavily relied on these 2 existing programs to write this

Python-Host
Made by: OpenWonderLabs
https://github.com/OpenWonderLabs/python-host

Switchbotpy
Made by: nicolas-kuechler
https://github.com/RoButton/switchbotpy

USAGE

Firstly import Bot from SwitchyBot
from SwitchyBot import Bot

Then you have to create a Bot instance to control, you can create one by running this command
[Name of instance] = Bot(bot_id = [id you want], mac = [mac adress], name = [name of the bot])
For example
Bot1 = Bot(bot_id = 1, mac = "A1:B2:C3:D4:E5:F6", name = "LightBot1")

When we have the Bot instance we can call press() to press the bot, if the bot is in dual state mode it will switch between ON/OFF
[Name of instance].press() or Bot1.press()

We can also call the switch() function if we have a bot in dual state mode, state is 0 for OFF and 1 for ON
[Name of instance].switch(state) or Bot1.switch(0)

SYSCMD

This is a class for the system commands, to use them import SysCmd
from SwitchyBot import SysCmd

To start bluetooth on demand run startblue()
SysCmd.startblue()

If you cant connect to bluetooth run restartblue() to restart bluetooth
SysCmd.restartblue()

To stop the bluetooth services run stopblue()
SysCmd.stopblue()

EXAMPLES

This is an example where you input your variables and it activates the bot

from SwitchyBot import Bot

Name = input("Instance name: ")                   # Pick a name
ID = input("ID: ")                                # An ID
Mac = input("Mac adress: ")                       # And the adress

Bot0 = Bot(bot_id = ID, mac = Mac, name = Name)   # Create the bot
Bot0.press()                                      # And press

KNOWN ISSUES

After running a succesfull script you will get a warning that states
Failed to send b'W\x01' to [Instance name] at [Mac adress]

It did not fail, it just thinks it did

When trying to run the script you get a ModuleNotFoundError
ModuleNotFoundError: No module named 'SwitchyBot'

To fix this import sys and use sys.path.append or put the script in the same directory as the repository

import sys
sys.path.append([Your path to SwitchyBot.py])
from SwitchyBot import Bot

If you encounter any other errors or problems feel free to make an issue, however do keep in mind that im not a professional and can only do so much