使用asyncio在局域网中与LIFX设备进行本地通信的API。
项目描述
aiolifx
aiolifx 是一个用于控制您局域网中的LIFX LED灯泡的Python 3/asyncio库。
大部分内容来自Meghan Clarkk的lifxlan包(https://github.com/mclarkk),并调整为Python 3(以及显然的asyncio)
安装
我们在PyPi上,所以
pip3 install aiolifx
或者python3 -m pip install aiolifx
安装后,该实用程序
aiolifx
可以用于测试/控制设备。
注意:使用Python 3.4安装时,安装会产生错误信息(语法错误)。这可以安全忽略。
如何使用
基本上,您创建一个具有至少2个方法的对象
- register
- unregister
然后您在asyncio中启动LifxDiscovery任务。它将注册它找到的任何新灯。所有与灯通信的方法都可以传递一个回调函数来响应灯的响应。该回调应接受2个参数
- a light object
- the response message
最简单的方法是查看examples目录中的文件。 "Wifi" 和 "Uptime" 使用回调在返回信息时打印信息。
本质上,测试程序是这样的
class bulbs():
""" A simple class with a register and unregister methods
"""
def __init__(self):
self.bulbs=[]
def register(self,bulb):
self.bulbs.append(bulb)
def unregister(self,bulb):
idx=0
for x in list([ y.mac_addr for y in self.bulbs]):
if x == bulb.mac_addr:
del(self.bulbs[idx])
break
idx+=1
def readin():
"""Reading from stdin and displaying menu"""
selection = sys.stdin.readline().strip("\n")
DoSomething()
MyBulbs = bulbs()
loop = aio.get_event_loop()
discovery = alix.LifxDiscovery(loop, MyBulbs)
try:
loop.add_reader(sys.stdin, readin)
discovery.start()
loop.run_forever()
except:
pass
finally:
discovery.cleanup()
loop.remove_reader(sys.stdin)
loop.close()
其他值得注意的事项
- Whilst LifxDiscovery uses UDP broadcast, the bulbs are
connected with Unicast UDP
- The socket connecting to a bulb is not closed unless the bulb is deemed to have
gone the way of the Dodo. I've been using that for days with no problem
- You can select to used IPv6 connection to the bulbs by passing an
IPv6 prefix to LifxDiscovery. It's only been tried with /64 prefix.
If you want to use a /48 prefix, add ":" (colon) at the end of the
prefix and pray. (This means 2 colons at the end!)
- I only have Original 1000, so I could not test with other types
of bulbs
- Unlike in lifxlan, set_waveform takes a dictionary with the right
keys instead of all those parameters
感谢
感谢Anders Melchiorsen的重要贡献
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装软件包的更多信息。
源代码分发
haaiolifx-0.8.2.tar.gz (28.5 kB 查看哈希值)
构建分发
haaiolifx-0.8.2-py3-none-any.whl (30.1 kB 查看哈希值)