与mystrom设备交互的python api
python-mystrom的Python项目详细描述
用于与myStrom设备交互的python api。
此模块不是mystrom ag正式开发、支持或认可的。 如有任何疑问或其他查询,请使用本回购协议中的问题追踪工具。
如果没有mystrom ag的支持,创建 这个模块是集成到 Home Assistant。Mystrom AG已提供 仍然提供硬件、有价值的反馈和建议。他们连续不断 支持使本模块的进一步开发成为可能。
插头/开关
目前涵盖以下端点:
- /report:用于获取当前状态和功耗
- /relay:用于设置中继状态
您仍然可以将设备与智能手机应用程序一起使用, curl或其他工具。下面的示例演示如何将开关与 httpie和curl以及python-mystrom。
$ http http://IP_ADDRESS_PLUG/report HTTP/1.1 200 OK Content-Length: 39 Content-Type: application/json Date: Mon, 15 Feb 201617:52:21 GMT {"power": 51.630947, "relay": true}
$ curl -X GET http://IP_ADDRESS_PLUG/relay?state=1
灯泡
如果灯泡是亮的,那么您应该能够检索 灯泡。
浏览到http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB或使用 命令行工具。
$ curl -d "color=0;0;100" -d "action=on" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB {"5DFF7FAHZ987": {"on": true, "color": "0;0;100", "mode": "hsv", "ramp": 100, "notifyurl": ""}}
灯泡无法处理格式化为json的有效负载。必须 使用application/x-www-form-urlencoded。如果有什么事 不起作用,尤其是在用hsv设置颜色时。
如果你打算用你的灯泡和Home Assistant 使用应用程序或使用下面的命令将灯泡设置为从colors开始的状态。
$ curl -d "color=0;0;100" IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
设置状态
您可以使用post请求和有效负载设置状态。
- on:curl -d "action=on" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
- 关:curl -d "action=off" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
- 切换:$ curl -d "action=toggle" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
设置颜色rgb
设置颜色的支持模式之一是rbg。
- 白色:$ curl -d "color=FF000000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
- red:$ curl -d "color=00FF0000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
- 绿色:$ curl -d "color=0000FF00" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
- 蓝色:$ curl -d "color=000000FF" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
设置颜色hsv(色调、饱和度、值)
也可以使用hsv。
$ curl -d "color=0;0;100" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
而“color=”由色调、饱和度和值组成。
设置单声道(白色)
如果只想设置灯泡的“白色”,请使用mono。
$ curl -d "color=10;100" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
“color=”包含色温值(从1到18)和 亮度(从0到100)。
调光(斜坡)
添加ramp和一个间隔以设置更改时的转换时间 颜色。
$ curl -d "action=on&ramp=1000&color=00FF0000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
斜坡的测量单位为毫秒(ms)。
^{tt(TT)16}$helper工具
命令行工具mystrom可以帮助设置按钮并获取 灯泡和插头的细节。
$ mystrom Usage: mystrom [OPTIONS] COMMAND [ARGS]... Simple command-line tool to get and set the values of a myStrom devices. This tool can set the targets of a myStrom button for the different available actions single, double, long and touch. Options: --version Show the version and exit. --help Show this message and exit. Commands: bulb Get and set details of a myStrom bulb. button Get and set details of a myStrom button. config Get and set the configuration of a myStrom...
这些例子展示了如何获得给定灯泡的细节。
$ mystrom config read IP address of the myStrom device: IP_ADDRESS_BULB MAC address of the device: MAC_ADDRESS_BULB Read configuration from IP_ADDRESS_BULB {'MAC_ADDRESS_BULB':{'type':'rgblamp', 'battery':False, 'reachable':True, 'meshroot':False, 'on':True, 'color':'191;90;14', 'mode':'hsv', 'ramp':100, 'power':0.953, 'fw_version':'2.25'}}
模块的示例用法
下面的示例演示如何使用这个python模块。
importpymystromplug=pymystrom.MyStromPlug('IP_ADDRESS_PLUG')# Preserve stateSTATE_ON=plug.get_relay_state()# Switch relay on if the plug is currently offifnotSTATE_ON:print("Relay will be switched on")plug.set_relay_on()# Wait a few seconds to get a reading of the power consumptionprint("Waiting for a couple of seconds...")time.sleep(10)# Get the new state of the switchprint("Relay state:",plug.get_relay_state())print("Power consumption:",plug.get_consumption())# Switch relay off if it was off.ifnotSTATE_ON:plug.set_relay_off()
灯泡的示例可以在目录examples中找到。
许可证
python-mystrom在mit下获得许可,有关更多详细信息,请检查许可证。