连接到本地HomeGear服务的分机。
homegear的Python项目详细描述
python3家庭用具
python3 homegear是一个python扩展,用于通过unix域套接字连接到homegear。它支持HomeGear的所有RPC方法和实时事件接收。
先决条件
扩展需要安装libhomegear-ipc
,并且至少需要python版本3。要安装它,请为您的发行版添加HomeGear APT存储库(请参见https://homegear.eu/downloads.html),然后执行
apt install libhomegear-ipc
或者,在非debian类系统上,您可以手动编译libhomegear ipc:
git clone https://github.com/Homegear/libhomegear-ipc
cd libhomegear-ipc
./makeRelease.sh
设置
如果您有PIP,请执行以下操作:
sudo python3 -m pip install homegear
要手动编译和安装扩展,请从gibhut下载并执行
sudo python3 setup.py install
方法
只有一个对象可用:Homegear
。它的构造函数中有两个参数:HomeGear IPC套接字的路径(通常是/var/run/homegear/homegearIPC.sock
)和回调方法。在HomeGear中更新设备变量时执行回调方法。在实例化时,类将等待,直到它成功连接到HomeGear。2秒后,即使没有连接,它也会返回。要检查,如果对象仍然连接,可以调用connected()
。除了此方法之外,还可以调用HomeGear(see ref.homegear.eu)中可用的所有rpc方法。
不连接时的行为
当没有连接到HomeGear时,构造函数在2秒后返回。它无限期地尝试重新连接,直到能够建立连接。同样的情况也发生在连接丢失上。要检查模块是否已连接,请调用connected()
。即使没有连接,也可以毫无例外地调用所有rpc方法。返回值将是None
。
类型转换
python变量到homegar变量
Python | Homegear |
---|---|
None | Void |
Bool | Boolean |
Long | Integer |
Float | Float |
Unicode | String |
Bytes | Binary |
List | Array |
Tuple | Array |
Dict | Struct |
HomeGear变量到Python变量
Homegear | Python |
---|---|
Void | None |
Boolean | Bool |
Integer | Long |
Float | Float |
String | Unicode |
Binary | Bytes |
Array | List |
Struct | Dict |
用法示例
一个简单的例子:
fromhomegearimportHomegear# This callback method is called on Homegear variable changesdefeventHandler(eventSource,peerId,channel,variableName,value):# Note that the event handler is called by a different thread than the main thread. I. e. thread synchronization is# needed when you access non local variables.print("Event handler called with arguments: source: "+eventSource+" peerId: "+str(peerId)+"; channel: "+str(channel)+"; variable name: "+variableName+"; value: "+str(value));hg=Homegear("/var/run/homegear/homegearIPC.sock",eventHandler);
请注意,回调方法是从其他线程调用的。访问共享变量时请使用线程同步。
要执行rpc方法,只需键入hg.<method name>
。例如,将系统变量“test”设置为“6”并再次检索:
hg.setSystemVariable("TEST",6);print(hg.getSystemVariable("TEST"));
一个完整的例子:
importtimefromhomegearimportHomegear# This callback method is called on Homegear variable changesdefeventHandler(eventSource,peerId,channel,variableName,value):# Note that the event handler is called by a different thread than the main thread. I. e. thread synchronization is# needed when you access non local variables.print("Event handler called with arguments: source: "+eventSource+" peerId: "+str(peerId)+"; channel: "+str(channel)+"; variable name: "+variableName+"; value: "+str(value));hg=Homegear("/var/run/homegear/homegearIPC.sock",eventHandler);# hg waits until the connection is established (but for a maximum of 2 seonds).hg.setSystemVariable("TEST",6);print("getSystemVariable(\"TEST\") after setting \"TEST\" to 6: ",hg.getSystemVariable("TEST"));hg.setSystemVariable("TEST",["One",2,3.3]);print("getSystemVariable(\"TEST\") after setting \"TEST\" to an array: ",hg.getSystemVariable("TEST"));hg.setSystemVariable("TEST",{"One":1,2:"Two",3:[3,3,3]});print("getSystemVariable(\"TEST\") after setting \"TEST\" to a struct: ",hg.getSystemVariable("TEST"));counter=0;while(hg.connected()):time.sleep(1);counter+=1;hg.setSystemVariable("TEST",counter);