FHEM家庭自动化服务器的PythonAPI
fhem的Python项目详细描述
python fhem
python fhem(家庭自动化服务器)api
通过套接字或http(s)连接到fhem家庭自动化服务器的简单api,使用fhem上的telnet或web端口,支持可选的ssl(tls)和密码或basicauth。
注意:python 2.x不推荐警告。python-fhem
版本0.6.x将是支持python 2.x的最后一个版本。
安装
PIP安装(PYPI)
有关包的其他信息,请参见PyPI page。
pip install [-U] fhem
来源
在python-fhem/fhem
:
获取安装的自述文件副本(setup.py需要):
cp ../README.md .
然后:
pip install [-U] .
或者,作为开发人员安装,允许就地编辑:
pip install [-U] -e .
历史记录
- 0.6.2(2019-06-06):错误修复,get_device_reading()可能返回其他不相关的读数。#14。telnet的默认阻塞模式已设置为非阻塞。这可以用参数
blocking=True
(仅限telnet)更改。建议使用http(s)(高级 性能和速度) - [构建环境](2019-07-22):对Travisci自动自检的初始支持。
- 0.6.1(2018-12-26):新的api在get上使用telnet非阻塞,这导致了问题(d1nd141,#12),已修复 通过使用阻塞telnet i/o。
- 0.6.0(2018-12-16):增强和扩展的get api(andre0512#10)。有关新功能的详细信息,请参见online documentation,特别是new get()方法。标记为已弃用的专有日志记录功能。
- 0.5.5(2018-08-26):文档清理,斯芬克斯自动文档。
- 0.5.3(2018-08-26):修复异常处理程序中的语法
- 0.5.2(2018-06-09):修复无效csrf返回时的崩溃
- 0.5.1(2018-01-29):删除了对logging.basicconfig()的调用,因为它是不必要的,如果其他模块也使用它,则会导致中断。(海勒里希#8)
- 0.5:API清理(中断更改!)。删除了不推荐使用的函数:sendcmd、sendrcvcmd、getdevstate、getdevreading(替换为pep8 conform names,s.b.)。重命名参数ssl=->;使用ssl=
- 0.4.4:合并的python记录器支持(chuckmoe,#6)
- 0.4.3:用于获取上次读取更改时间的合并API扩展(logi85,#5)
- 0.4.2:已修复弃用错误消息(ivermue,#4)
- 0.4.0:CSRF令牌支持(FHEM 5.8要求)
用法
设置并获取事务
无密码和无加密的默认telnet连接:
importloggingimportfhemlogging.basicConfig(level=logging.DEBUG)## Connect via HTTP, port 8083:fh=fhem.Fhem("myserver.home.org",protocol="http",port=8083)# Send a command to FHEM (this automatically connects() in case of telnet)fh.send_cmd("set lamp on")# Get temperatur of LivingThermometertemp=fh.get_device_reading("LivingThermometer","temperature")# Get a dict of kitchen lights with light onlights=fh.get_states(group="Kitchen",state="on",device_type="light",value_only=True)# Get all data of specific tvstvs=fh.get(device_type=["LGTV","STV"])# Get indoor thermometers with low batterylow=fh.get_readings(name=".*Thermometer",not_room="outdoor",filter={"battery!":"ok"})# Get temperature readings from all devices that have a temperature reading:all_temps=fh.get_readings('temperature')
https连接:
fh=fhem.Fhem('myserver.home.org',port=8085,protocol='https')
接受自签名证书(因为没有cafile
选项)。
通过https与ssl和basicauth连接:
fh=fhem.Fhem('myserver.home.org',port=8086,protocol='https',cafile=mycertfile,username="myuser",password="secretsauce")
如果没有给出公共证书cafile
,则接受自签名证书。
通过默认协议telnet连接,默认端口7072:(不推荐)
note:通过telnet的连接对于大型请求是不可靠的,这些请求 包括使用通配符函数的所有内容。
fh=fhem.Fhem("myserver.home.org")
使用SSL和密码通过Telnet连接:
fh=fhem.Fhem("myserver.home.org",port=7073,use_ssl=True,password='mysecret')fh.connect()iffh.connected():# Do things
建议改用http(s)连接到fhem。
事件队列(当前仅限于telnet)
库可以创建使用后台线程接收 并发送FHEM事件:
try:# Python 3.ximportqueueexcept:# Python 2.ximportQueueasqueueimportfhemque=queue.Queue()fhemev=fhem.FhemEventQueue("myserver.home.org",que)whileTrue:ev=que.get()# FHEM events are parsed into a Python dictionary:print(ev)que.task_done()