GithubHelp home page GithubHelp logo

simensma / tuyamqtt Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 16.0 44 KB

Listens on MQTT topic and routes requests to Tuya devices

Dockerfile 0.83% Makefile 1.75% Python 89.80% JavaScript 3.40% Vue 4.23%

tuyamqtt's Introduction

TuyaMQTT

Listens on MQTT topic and routes requests to Tuya devices, based on a one to one topic translation.

Quick Example

tuya/3.3/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/1/state
tuya/3.3/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/1/command
tuya/3.3/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/1/attributes
tuya/3.3/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/availability
tuya/3.3/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/attributes

Installation

git clone https://github.com/TradeFace/tuyamqtt.git
cd tuyamqtt
make
make install

Installation Docker

git clone https://github.com/TradeFace/tuyamqtt.git
cd tuyamqtt
make docker

Running Docker

docker run -it --rm --name my-app tuyamqtt

Running Docker Compose

version: '3'
services:
  homeassistant:
    ports: 
      - "8123:8123"
    ...
    restart: always
    network_mode: host
  mosquitto:
    image: eclipse-mosquitto
    ...
    restart: always
    network_mode: host
  tuyamqtt:
    image: "tuyamqtt:latest"
    hostname: tuyamqtt 
    container_name: tuyamqtt   
    working_dir: /usr/src/app    
    volumes:
      - ./config:/usr/src/app/config    
    command: "python -u main.py"
    restart: always
    network_mode: host

MQTT Topics

tuya/<protocol>/<device-id>/<device-localkey>/<device-ip>/<dps>/state
tuya/<protocol>/<device-id>/<device-localkey>/<device-ip>/<dps>/command
tuya/<protocol>/<device-id>/<device-localkey>/<device-ip>/<dps>/attributes
tuya/<protocol>/<device-id>/<device-localkey>/<device-ip>/attributes
tuya/<protocol>/<device-id>/<device-localkey>/<device-ip>/availability

MQTT state

State payload values can be set in tuyamqtt.conf. Updates on command, or on state change on device (5 second polling)

  • Default values: ON, OFF

MQTT command

Valid values are true, ON, 1, false, OFF, 0

MQTT dps attributes

Returns dps state, via state and timestamp

MQTT attributes

Returns all device attributes, via state and timestamp. Updates on command, or on state change on device (5 second polling)

MQTT availability

Availability payload values can be set in tuyamqtt.conf. Updates every 15 seconds.

  • Default values: online, offline.

Via state

The via state can be used in your home automation system. i.e. a state change with via tuya could be a strong indication of presence Returns mqtt when a state change comes in via a mqtt-request Returns tuya when a state change is detected in status polling

tuyamqtt.conf

[General]
entity_file: ./config/entities.json
topic: tuya
payload_on: ON
payload_off: OFF
availability_online: online
availability_offline: offline

[MQTT]
user: myusername
pass: mypassword
host: 192.168.1.14
port: 1883

Home Assistant Example

switch:
  - platform: mqtt
    name: "Living Socket Left 1"
    command_topic: "tuya/3.3/34280100500194d17c95/e7e9339dd82abe61/192.168.1.50/1/command"  
    state_topic: "tuya/3.3/34280100500194d17c95/e7e9339dd82abe61/192.168.1.50/1/state"
    json_attributes_topic: "tuya/3.3/34280100500194d17c95/e7e9339dd82abe61/192.168.1.50/1/attributes"
    availability_topic: "tuya/3.3/34280100500194d17c95/e7e9339dd82abe61/192.168.1.50/availability"
  - platform: mqtt
    name: "Living Socket Left 2"
    command_topic: "tuya/3.3/347550642cf432a2cbaf/314c6b7f3f44f979/192.168.1.51/1/command"
    state_topic: "tuya/3.3/347550642cf432a2cbaf/314c6b7f3f44f979/192.168.1.51/1/state"
    json_attributes_topic: "tuya/3.3/347550642cf432a2cbaf/314c6b7f3f44f979/192.168.1.51/attributes"
    availability_topic: "tuya/3.3/347550642cf432a2cbaf/314c6b7f3f44f979/192.168.1.51/availability"
  - platform: mqtt
    name: "Living Socket Right 1"
    command_topic: "tuya/3.3/347550712cf432a2b04a/f9b80165db5e06bd/192.168.1.54/1/command" 
    state_topic: "tuya/3.3/347550712cf432a2b04a/f9b80165db5e06bd/192.168.1.54/1/state"
    json_attributes_topic: "tuya/3.3/347550712cf432a2b04a/f9b80165db5e06bd/192.168.1.54/attributes"
    availability_topic: "tuya/3.3/347550712cf432a2b04a/f9b80165db5e06bd/192.168.1.54/availability"
  • Note: availability only works for devices known by TuyaMQTT.

Testing with Mosquitto

Use mosquitto_pub to make TuyaMQTT aware of the device. TuyaMQTT needs a topic ending with command to discover a device

mosquitto_pub -t tuya/3.1/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/1/command -m ON

And run mosquitto_sub to listen to updates (in another terminal).

mosquitto_sub -t tuya/3.1/34280100600194d17c96/e7e9339aa82abe61/192.168.1.50/#

It might take a few seconds to get a state update.

Note: that TuyaMQTT only pushes status message when there was a change. The availability topic should be published every 15 sec.

Debugging

python3 -u main.py -v
python3 -u main.py -vv
python3 -u main.py -vvv

TODO

Changelog

Acknowledgements

License

Free to use, copy, modify, whatever you like.

tuyamqtt's People

Stargazers

Alex M avatar

Watchers

James Cloos avatar

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.