LoginSignup
0
0

More than 1 year has passed since last update.

Pythonで始めるテストツール製作(4)電源(一次側)のオンオフ(Windows編)

Posted at

1. はじめに

電源をオンオフするテスト手順を自動化する」で作成したswitchbotplugmini.pyをモジュールとして利用しメニュー操作で電源をオンオフできるようにします。

1.1 これまでの記事

2. 環境

  • Windows 10 Home 22H2
  • Python 3.10.0

3. ソースコード

メニュー操作は目視で行うことからレスポンスメッセージ(0100、0180など)をOFF、ON、UNKNOWNに置き換えて表示するようにします。

mytools_switchbotplugmini.py
# mytools_switchbotplugmini.py by ka's@pbjpkas 2023
# for Windows
# MIT License
from switchbotplugmini import *

address="00:00:5E:00:53:00" #replace your SwitchBot Plug Mini MAC Address.

def response(result, resp):
    if binascii.hexlify(resp) == b"0100":
        print(result, "OFF")
    elif binascii.hexlify(resp) == b"0180":
        print(result, "ON")
    else:
        print(result, resp, "UNKNOWN")

def main():
    while True:
        print("== myTools SwitchBot Plug Mini(Windows) ==")
        print("a: Turn ON")
        print("b: Turn OFF")
        print("c: Toggle")
        print("d: Read State")
        print("x: exit")

        s = input(">")

        if s == "a":
            result, resp = switchbotplugmini(address, "turnon")
            response(result, resp)
        if s == "b":
            result, resp = switchbotplugmini(address, "turnoff")
            response(result, resp)
        if s == "c":
            result, resp = switchbotplugmini(address, "toggle")
            response(result, resp)
        if s == "d":
            result, resp = switchbotplugmini(address, "readstate")
            response(result, resp)
        if s == "x":
            if __name__ == "__main__":
                print("Bye.")
            return


if __name__ == "__main__":
    main()

4. 使い方

python mytools_switchbotplugmini.py

以下のようにメニューとプロンプト(>)が表示されればOKです。

== myTools SwitchBot Plug Mini(Windows) ==
a: Turn ON
b: Turn OFF
c: Toggle
d: Read State
x: exit
>

5. mytools.pyへの組み込み

5.1 mytools.pyの改修

mytools.pyを3か所編集します。

mytools.py
# myTools.py by ka's@pbjpkas 2023
# python 3.x
# MIT License
import sys
import cv2
import mytools_visa
import mytools_pyserial
import mytools_switchbotplugmini                      #1

#(略)

def main():    while True:
        print("== myTools ==")
        print("a: comparison of old and new value")
        print("b: crop image")
        print("c: VISA menu")
        print("d: Serial menu")
        print("e: SwitchBot Plug Mini(Windows) menu") #2
        print("x: exit")

        s = input(">")

        if s == "a":
            comparison_value()
        if s == "b":
            crop_image_menu()
        if s == "c":
            mytools_visa2.main()
        if s == "d":
            mytools_pyserial.main()
        if s == "e":
            mytools_switchbotplugmini.main()          #3
        if s == "x":
            print("Bye.")
            sys.exit()


main()

5.2 実行例

メニューにSwitchBot Plug Mini(Windows)メニューが追加されています。

== myTools ==
a: comparison of old and new value
b: crop image
c: VISA menu
d: Serial menu
e: SwitchBot Plug Mini(Windows) menu
x: exit
>

6. おわりに

SwitchBot Plug Miniのオンオフ操作をメニュー形式でできるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0