Asyncio module for Broadlink devices control
Project description
pybroadlink
Control Broadlink devices with Python 3 using asyncio (single threaded with event loop). Currently supports the RM3 Smart Remote.
Usage
import sys
import logging
import traceback
from pybroadlink.broadlink_udp import BroadlinkRM3
import asyncio
import binascii
from base64 import b64decode
from pybroadlink import _LOGGER
async def testFake(n):
for i in range(n):
_LOGGER.debug("Counter is %d",i)
await asyncio.sleep(1)
async def discovery_test(*args):
rv = await BroadlinkUDP.discovery(local_ip_address=args[2],timeout=int(args[3]))
if rv:
_LOGGER.info("Discovery OK %s",rv)
else:
_LOGGER.warning("Discovery failed")
async def emit_test(*args):
import re
mo = re.search('^[a-fA-F0-9]+$', args[4])
if mo:
payload = binascii.unhexlify(args[4])
else:
payload = b64decode(args[4])
a = BroadlinkRM3((args[2],PORT),args[3])
rv = await a.emit_ir(payload,retry=1)
if rv:
_LOGGER.info("Emit OK %s",binascii.hexlify(rv[0]).decode('utf-8'))
else:
_LOGGER.warning("Emit failed")
a.destroy_remote()
async def learn_test(*args):
a = BroadlinkRM3((args[2],PORT),args[3])
rv = await a.enter_learning_mode()
if rv:
_LOGGER.info("Entered learning mode (%s): please press key",rv)
rv = await a.learn_ir_get(30)
if rv:
_LOGGER.info("Obtained %s",binascii.hexlify(rv[0]).decode('utf-8'))
else:
_LOGGER.warning("No key pressed")
else:
_LOGGER.warning("Enter learning failed")
a.destroy_remote()
_LOGGER.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
_LOGGER.addHandler(handler)
loop = asyncio.get_event_loop()
try:
asyncio.ensure_future(testFake(150))
if sys.argv[1]=="learn":
loop.run_until_complete(learn_test(*sys.argv))
elif sys.argv[1]=="discovery":
loop.run_until_complete(discovery_test(*sys.argv))
else:
loop.run_until_complete(emit_test(*sys.argv))
except BaseException as ex:
_LOGGER.error("Test error %s",str(ex))
traceback.print_exc()
except:
_LOGGER.error("Test error")
traceback.print_exc()
finally:
loop.close()
Contributions
Pull requests are welcome. Possible areas for improvement:
- Additional Broadlink devices (switches).
Disclaimer
Not affiliated with Broadlink.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pybroadlink-1.5.tar.gz
(8.7 kB
view details)
Built Distribution
pybroadlink-1.5-py3-none-any.whl
(10.3 kB
view details)
File details
Details for the file pybroadlink-1.5.tar.gz
.
File metadata
- Download URL: pybroadlink-1.5.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea3712597be0ae14daa446c3dee4e2099bef4c492866019f176d51478364e356 |
|
MD5 | 6507c4b82b1f040d57d52bce2877e272 |
|
BLAKE2b-256 | c8740b77d383e87c540df09b136c4025258a0645759b9db050a49c74de35d934 |
File details
Details for the file pybroadlink-1.5-py3-none-any.whl
.
File metadata
- Download URL: pybroadlink-1.5-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f406a977dfda6985819802b7163f5ca2b26d377e6e27899edad1f540d86f39f |
|
MD5 | 61551302a24c1415a99a0073d2e4acfc |
|
BLAKE2b-256 | 4555cd9a96218e76f9f04c67ca7e415204762ffa25d8050606d0d0aeab134833 |