Get battery level from SwitchBot in Python

Introduction

The problem with using SwitchBot is that the battery is dead. You can check the battery level by opening the official app, but you don't see it on a daily basis, and you're familiar with it because the battery is dead when you want to use it. I use it to unlock the auto lock, so it's a matter of life and death.

There are commands such as Press in Python released by the official SwitchBot, but there is nothing to get the remaining battery level. When I was investigating that it wasn't because I could get it with the app, I found it. https://github.com/RoButton/switchbotpy

I was happy with this once, but I decided to add a command to the official Python that is stable because there are times when I can not get it stably.

Official Python fix

A setting information acquisition command has been added. It is published in my repository that was officially forked. https://github.com/kanon700/python-host/tree/feature/add_get_settings

The correction contents are as follows

switchbot_py3.py


class Driver(object):
    handle = 0x16
    notif_handle = 0x13
    commands = {
        'press'     : '\x57\x01\x00',
        'on'        : '\x57\x01\x01',
        'off'       : '\x57\x01\x02',
        'settings'  : '\x57\x02',
    }

    def run_and_res_command(self, command):
        self.req.write_by_handle(self.handle, self.commands[command])
        return self.req.read_by_handle(self.notif_handle)

    def get_settings_value(self, value):
        value = struct.unpack('B' * len(value[0]), value[0])
        settings = {}
        settings["battery"] = value[1]
        settings["firmware"] = value[2] / 10.0
        settings["n_timers"] = value[8]
        settings["dual_state_mode"] = bool(value[9] & 16)
        settings["inverse_direction"] = bool(value[9] & 1)
        settings["hold_seconds"] = value[10]

        return settings

Battery level acquisition script

You can get the battery level with a script like this.

SwitchBot_Battery_Get.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

from switchbot_py3 import Driver

switchbot = 'xx:xx:xx:xx:xx:xx' #switchbot MAC address

def main():
    bot = Driver(device=switchbot)
    bot.connect()
    value = bot.run_and_res_command('settings')
    settings = bot.get_settings_value(value)
    print("battery:" + str(settings["battery"]))


if __name__ == '__main__':
    main()

that's all

Recommended Posts

Get battery level from SwitchBot in Python
Get data from Quandl in Python
Get exchange rates from open exchange rates in Python
Get Precipitation Probability from XML in Python
Get metric history from MLflow in Python
Get date in Python
Get time series data from k-db.com in Python
Get only articles from web pages in Python
Get YouTube Comments in Python
Get last month in python
OCR from PDF in Python
Get data from GPS module at 10Hz in Python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Get the value while specifying the default value from dict in Python
Hit REST in Python to get data from New Relic
Get macro constants from C (++) header file (.h) in Python
Get message from first offset with kafka consumer in python
Extract text from images in Python
Get, post communication memo in Python
Get upcoming weather from python weather api
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Extract strings from files in Python
How to get a string from a command line argument in python
Get additional data in LDAP with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Get Suica balance in Python (using libpafe)
Get keystrokes from / dev / input (python evdev)
Revived from "no internet access" in Python
Prevent double boot from cron in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Download images from URL list in Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Generate a class from a string in Python
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Convert from Markdown to HTML in Python
Get Cloud Logging available in Python in 10 minutes
[Python] Get the main color from the screenshot
Get rid of DICOM images in Python
Get your own IP address in Python
How to get a value from a parameter store in lambda (using python)
Get options in Python from both JSON files and command line arguments
Python in optimization
CURL in python
Get the contents of git diff from python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Meta-analysis in Python
Unittest in python