Giter Club home page Giter Club logo

broadlink-bridge's Introduction

broadlink-bridge

A HTTP/MQTT/LIRC bridge to Broadlink IR/RF devices, written in Python and powered by python-broadlink.

Note: that there is a similar project for Node.js with the same name.

Features

  • supports HTTP protocol
  • supports MQTT protocol
  • supports LIRC protocol
  • supports multiple Broadlink devices
  • supports Broadlink IR/RF codes
  • supports Pronto hex codes
  • supports defining named commands
  • supports specifying repeats
  • can act as sending hardware for IrScrutinizer via the LIRC output

Installing/running

To install: pip install [--user] git+https://github.com/lbschenkel/broadlink-bridge.git

To run: broadlink-bridge [config-file]

Docker

(to be written)

Usage in Home Assistant

This bridge is available as an unnoficial add-on.

Codes can be transmitted via RESTful commands:

rest_command:
  my_command:
    url: http://BRIDGE_HOST:PORT/device/DEVICE
    method: post
    payload: CODE

Or by publishing via MQTT, for example in an entity button:

type: entity-button
name: My command
entity: ...
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: PREFIX/device/DEVICE/transmit
    payload: CODE

An on/off switch can be implemented directly via a MQTT switch:

switch:
- platform: mqtt
  command_topic: PREFIX/device/DEVICE/transmit
  payload_on: CODE
  payload_off: CODE

Or by combining RESTful commands with a boolean input and an automation:

rest_command:
  my_device_on:
    url: http://BRIDGE_HOST:PORT/device/DEVICE
    method: post
    payload: CODE
  my_device_off:
    url: http://BRIDGE_HOST:PORT/device/DEVICE
    method: post
    payload: CODE
input_boolean:
  my_device:
    name: My Device
automation:
  trigger:
    platform: state
    entity_id: input_boolean.my_device
  action:
    service_template: rest_command.my_device_{{ trigger.to_state.state }}

Note that the RESTful switch is not useful, because it relies on the REST endpoint providing the state (not possible here) and it does not implement an optimistic mode with an assumed state (like the MQTT switch does).

In the above:

  • BRIDGE_HOST is the hostname/IP address of this bridge
  • PORT is the HTTP port (default 8780)
  • PREFIX is the MQTT prefix (default broadlink)
  • DEVICE identifies the target device
  • CODE is the code (or command) to transmit, prefixed by any repeats

Configuration

A configuration file can be optionally specified as a command-line argument. An example file is provided.

MQTT client

To enable the MQTT client, it is necessary to specify the URL of the MQTT broker as the broker_url value inside the [mqtt] section.

The prefix for the MQTT topics is configurable via topic_prefix.

Manually declared devices

Devices can be manually declared in the [devices] section. When a device is declared in this way it is given an alias which can act as an additional identifier for the device in the bridge. Auto-discovered devices do not have aliases.

Commands

Commands can be defined in the [commands] section. Commands associate a name with a code. The name can then be used anywhere where a code can appear.

The code for the command can be in any supported format and contain repeats, with the exception that it cannot be another command.

Protocols

Definitions

Device

A device represents a Broadlink device which can be addressed on any of the protocols. It can be identified by any of the following:

  • by its alias (as specified in the configuration file)
  • by its host (as specified in the configuration file or found via discovery)
  • by its MAC address
  • by one of its IP addresses

When the device is not found via one of the mechanisms above, a final attempt is made by interpreting device as a host: in case a device is discovered at that address, then it is added to the list of known devices (this can be considered a lazy discovery mechanism).

Default device

The default device is the device with an alias of default. If there is no device with such an alias, it is the first declared or discovered device.

Code

The code is the data to transmit. It can have one of the following forms:

  • IR/RF data in Broadlink format (encoded as base64): JgAcAB0dHB44HhweGx4cHR06HB0cHhwdHB8bHhwADQUAAAAAAAAAAAAAAAA=

  • IR data in Pronto hex format:

    0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0041 0016 0016 0016
    0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041
    0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0041 0016
    0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016
    0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 05F7 015B 0057 0016
    0E6C
    
  • a command defined via the configuration file: in that case the code for the command is used (which can have any of the above forms)

Note that codes can contain repeats (see next section).

Repeats

Repeats are the number of times a code will be retransmitted. Repeats can be specified in multiple ways:

  • as part of the code — if the code starts with N* (number plus asterisk) then the code will be sent N times (N-1 repeats), for example:
    • 3 * JgAcAB... (Broadlink format, sent 3 times)
    • 2 * 0000 006C ... (Pronto hex format, sent 2 times)
    • 5 * tv/on (command defined via configuration file, sent 5 times)
  • inside Broadlink data packet (second data byte is the number of repeats)
  • via the protocol command (in case of LIRC)

When repeats are specified in more than one way, their effects multiply. For example, LIRC command SEND_ONCE default 3*tv/on 1 (1 being the repeat and tv/on being a command defined in the configuration file as 2*JgAcAB...) will result in the code being sent 2x3x2 = 12 times (11 repeats).

HTTP

verb path description
POST /device/device transmits the submitted code via device

Status codes:

  • 404 when the device is unknown
  • 400 when the code is invalid or not recognized

MQTT

The topic prefix defaults to broadlink and can be changed via the configuration file.

topic description
prefix/device/device/transmit transmits the submitted code via device

LIRC

A subset of the LIRC command interface and the unofficial CCF extension is supported.

command description
SEND_ONCE device code [repeat] transmits the given code (no spaces) via device, optionally repeating it repeat times
SEND_CCF_ONCE repeat code transmits the given code (spaces allowed) via the default device, repeating it repeat times
LIST replies with all known devices
LIST device replies with all defined commands (commands are not device-specific)
VERSION replies with bridge version

broadlink-bridge's People

Contributors

kathampy avatar lbschenkel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

broadlink-bridge's Issues

Usage on Raspberry pi HassOS

I Sucefully installed the addon on raspberry pi, but it seems its configured to start on docker as I receive the follwing error:

404 Client Error for http+docker://localhost/v1.40/containers/create?name=addon_c523f6d3_broadlink_bridge: Not Found ("No such image: c523f6d3/armv7-addon-broadlink_bridge:0.1.6")

Can I manage to work around on raspberry pi?

Link to Node.js Project?

Hi @lbschenkel,

I've realized that we have similar projects that share the same name.

Last year, I created a HTTP to Broadlink service written in Node.js.
You can find it on GitHub & NPM.

To avoid confusion, would you be interested in listing each others project in the README?

For instance, I would add something like "If you are looking for a similar project written in Python, check out broadlink-bridge by lbschenkel." and will include a link to this repo.

Let me know your thoughts,
-- Stephen

Repeat Commands

Been using this for a while. It's great!
I'd like to use the repeats option mentioned in the readme and get away from using scripts to do it.

I've got the code learned and tested. But when I add the 2* to the beginning of the string, I get the following error:

Client error http://127.0.0.1:8780/device/192.168.2.100.
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/rest_command/__init__.py", line 120, in async_service_handler
    timeout=timeout,
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1012, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 504, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 847, in start
    message, payload = await self._protocol.read()  # type: ignore  # noqa
  File "/usr/local/lib/python3.7/site-packages/aiohttp/streams.py", line 591, in read
    await self._waiter
aiohttp.client_exceptions.ServerDisconnectedError: None

Here's the relevant REST command:

new_proj_off:
    url: http://127.0.0.1:8780/device/192.168.2.100
    method: post
    payload: 2*JgBQAAABKZQTEhM3ExIUEhM3EzUVEhQSEzcTEhM3FDcTEhMSEzYVNxMSEzcTOBM1FRITNxQSExITNxMSFBEUEhM3ExITNxQ3EwAFCAABKUoTAA0FAAAAAAAAAAA=

Have also tried it with spaces and get the same result.

Any help greatly appreciated. :-)

status - mqtt

it would be great to get updates of the broadlink devices via mqtt

Command repeats doesnt work

Hi,

looks like command repeats doesnt work for IR, at least i can't debug to provide more info.

Non of these work

❯ curl -X POST -d "5*JgBQAAABJpITNhQ1FBEUNhMSEzYTEhQ1FDYTEhM2FDUUERQSEzYTNhQ1FBEUEhMSExITNhMSEzYUERQ2EzYTNxM2ExITNhMSEwAEzQABJkkTAA0FAAAAAAAA" 192.168.1.XXX:8780/device/rm4
❯ curl -X POST -d "fireplace/power" 192.168.1.XXX:8780/device/rm4

where:
[commands]
fireplace/power = 2*JgBQAAABJpITNhQ1FBEUNhMSEzYTEhQ1FDYTEhM2FDUUERQSEzYTNhQ1FBEUEhMSExITNhMSEzYUERQ2EzYTNxM2ExITNhMSEwAEzQABJkkTAA0FAAAAAAAA

Can you please help with this, at least can tell how to debug

Hotkeys to terminate process.

Ctrl + C, Ctrl + Z, or Ctrl + Pause / Break don't work in this application and I have to terminate the process manually. Can this be supported?

install

hello,
the addon does not install for me
odroid xu4 card

rm4 pro

Hello very interesting project!
One question is the rm4 pro supported?
I received this error:
TypeError: str, bytes or bytearray expected, not rm4

Missing package in Docker build: py3-pip

Thanks for your effort. It looks a lot like the thing that I was about to build myself. Glad to see somebody got the same itch before me ;-) I'm going to try out the code on my system.

I tried building the Docker image, but it tripped over pip3 not being available in the image.
The easy fix was to add the package for it to the build:

RUN apk add py3-pip py3-cryptography py3-paho-mqtt

mqtts with certificate & keys

Hi,
Is there (or planned to be) a way to add a private key for mqtts connection?
For example connecting the broadlink-mqtt to AWS IOT as a thing that requires certificate

Thanks

Docker build on raspberry pi required some extra work

Because the python3 cryptography module wasn't available in a ready-to-go package, it needed to be compiled while building the docker image. That compile missed some requirements. I was able to build the container image, by using the following updated line in the Dockerfile:

RUN apk add py3-cryptography py3-paho-mqtt py3-pip gcc rust cargo python3-dev libffi-dev libc-dev linux-headers openssl-dev

Alternatively, I also tried adding a "pip install --upgrade pip" operation in the build (normally the way to get access to the required binary package), but that didn't help on my Raspberry.

Error in addning codes

Hello,

i have this config

`[commands]

Commands are codes that can be referred to by name.

If an incoming code matches a command name, the command code is

sent to the device instead.

For example, the code below will be sent 5 times (4 repeats):

lamp/power-on = gGEAwgKCCsIKwgJCSoICggrCAoICggrCCsICQkqCQkJCQgrCAoIKwgKCCoJCggqCAoIKwgKCCoJCggqCAoIKwgKCCsICggrCAoIKggrCAoICggrCAoIKgkJCCsIKwgKCAABXQhUCCsICggJCSoJKggKCAoIKwgrBwoICggrCAoIKwgKCCoJCQkqCAoIKwgrCAoIKggKCSoICggKCCsIKwgKCCoICgkKBysICggrCAoIKwgKCCsICggqCAoIKwgKCCsICggqCQkIKwgKCCsICggqCSoICggKCCsICggrCAoIKgkqCAoIAAFeCFMJKggKCQkIKwgrCAoICggrCCsICggKCCsICgcrCQkIKwgKCCsICggrCCsICggqCQoIKggKCQoHKwkqCAoIKwgKCAoIKwgKCCsICggrCAoIKwgJCSoJCQgrCAoIKwgKCCsICggrCAoIKwgKCCsIKgkJCQkJKwcKCCsJCQgrCCsICggAAV4IVAgqCQoICggrCCsHCgkJCSoIKwgKCAoIKwgKCCsICggrCAoIKwgKCCsIKwgKCCsICggrCAoICggrCCsICggqCQkJCgcsCAkJKgkJCCsICggrCAoIKwgKCCsICggrCAoIKwgJCSoJCgcrCAsIKggrCAoICggrCAoIKwgKCCsIKwgKCAABXQhUCSoICggKCCsIKwgKCAoIKwgrCAoICggrCAoIKwgJCSoJCggqCQkILAgqCAoIKwgKCCsICggKCCsIKwgKCCsICggKCCsICggrCAoIKwgKCCoJCggrCAkILAgKCCsICQgrCAoIKwgKCCsICggrCCsICggKCCsICggrCAoIKwgrCAoIAAFdCVQIKgkKCAkILAgqCAoJCQkrCCoJCggKCCsICggrCAoIKwgKCCsICggrCCsICggrCAoIKwgKCAoIKwgrCAoIKwgKCAoIKwgKCCsICggrCAoIKwgKCCsICggrCAoIKwgKCCsICggrCAoIKwgKCCsIKwgKCAoIKwgKCCsICggrCCsICggAAV4IVAgqCQkJCQkrCCsICQkJCSoJKwgKCAkJKgkJCSsHCwgrCAoHLAgKCCsIKgkKCCsICQkrCAoICggrCCsICggrCAoICggrCAoIKwgKCCoJCggqCQoIKwgKCCsICggrCAoIKwgJCSsICggrCAoIKwgrCAoICggrCAoIKwgKCCsIKwgKCAAF3AAAAA=

[devices]

Known devices that will be exposed via the bridge.

The key is the alias that the device can be addressed as.

The value is the host (name or IP) of the Broadlink device.

myalias = 192.168.178.21

[discovery]

The bridge can also discover nearby devices (same subnet).

Number of seconds in which to wait for devices to reply during discovery.

A value of 0 disables auto-discovery.

timeout = 5

[http]

The port that the REST service will use.

8780 is the default port. Use 0 to disable.

port = 8780`

and get error by initialisation

[s6-init] making user provided files available at /var/run/s6/etc...exited 0. [s6-init] ensuring user provided files have correct perms...exited 0. [fix-attrs.d] applying ownership & permissions fixes... [fix-attrs.d] done. [cont-init.d] executing container initialization scripts... [cont-init.d] done. [services.d] starting services [services.d] done. 2020-12-17 16:24:30,119 INFO Starting broadlink-bridge/0.1.6... 2020-12-17 16:24:30,121 INFO Reading config file: /config/broadlink-bridge.ini 2020-12-17 16:24:30,122 INFO Registering command: lamp/power-on Traceback (most recent call last): File "/usr/local/bin/broadlink-bridge", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.9/site-packages/broadlink_bridge/cli.py", line 59, in main REGISTRY.set_command(command, payload) File "/usr/local/lib/python3.9/site-packages/broadlink_bridge/__init__.py", line 66, in set_command self._commands[command] = ir_decode(data)[0] File "/usr/local/lib/python3.9/site-packages/broadlink_bridge/util.py", line 63, in ir_decode code = base64.b64decode(code) File "/usr/local/lib/python3.9/base64.py", line 87, in b64decode return binascii.a2b_base64(s) binascii.Error: Invalid base64-encoded string: number of data characters (1209) cannot be 1 more than a multiple of 4 [cmd] /run.sh exited 1 [cont-finish.d] executing container finish scripts... [cont-finish.d] done. [s6-finish] waiting for services. [s6-finish] sending all processes the TERM signal.

It is the 100% working code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.