Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuya support #1997

Merged
merged 116 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
67774a9
Tuya dimmer draft
mcspr Jun 7, 2019
3ace7bf
quickfix for max pwm define
mcspr Jun 7, 2019
e17224c
send wifi status (always?)
mcspr Jun 7, 2019
d52b5d2
streamstring
mcspr Jun 7, 2019
8f1a6f2
TUYA_SERIAL stream
mcspr Jun 13, 2019
3827802
fix setup code
mcspr Jun 13, 2019
063163e
fix dataframe writer
mcspr Jun 13, 2019
aab614b
reset read_until
mcspr Jun 13, 2019
ab63d5c
Queue output data
mcspr Jun 13, 2019
427fb99
typo
mcspr Jun 13, 2019
ad320f0
fix ns
mcspr Jun 13, 2019
729bd37
fix type mismatch
mcspr Jun 13, 2019
19a5641
correct emplace args
mcspr Jun 13, 2019
45799f6
default dimmer dp define
mcspr Jun 13, 2019
16017f9
fix ns prototypes, switch dp define
mcspr Jun 13, 2019
add182a
fix command cast
mcspr Jun 13, 2019
db3a104
add in&out debug
mcspr Jun 13, 2019
38bdd1a
use HEX in streamstring output
mcspr Jun 13, 2019
44430c2
dont disable serial debug, it can be serial1
mcspr Jun 13, 2019
1518687
trying template arg instead
mcspr Jun 13, 2019
231ece9
send wifi status, properly step through steps
mcspr Jun 14, 2019
4a330ad
env:tuya-generic-dimmer
mcspr Jun 14, 2019
c507df4
fix warnings
mcspr Jun 14, 2019
5676df6
dataPtr should be offset
mcspr Jun 15, 2019
fe20117
timeout for read
mcspr Jun 15, 2019
7ece78b
fix 0 byte, checksum starts from 0
mcspr Jun 15, 2019
9f0f9b5
offset read_until with current index and checksum byte
mcspr Jun 15, 2019
9aba595
set version from serialbuffer
mcspr Jun 15, 2019
17bc791
drop arduino HEX, no zero padding ...
mcspr Jun 15, 2019
10ba047
proper timing
mcspr Jun 15, 2019
3e68aef
rename data to buffer. avoid mixup with frame "data" part
mcspr Jun 15, 2019
23da54a
fix product debug
mcspr Jun 16, 2019
4755f24
remove update_wifi state. just send it
mcspr Jun 16, 2019
711e24c
reuse dataframe debugsend
mcspr Jun 16, 2019
4da40d0
remove all update_wifi switches
mcspr Jun 16, 2019
0e948c8
handle and ack wifi config / reset requests
mcspr Jun 16, 2019
954073f
fix use before definition for wifistatus func
mcspr Jun 16, 2019
bca19f4
fix build_flags, update relay_provider
mcspr Jun 17, 2019
321b60b
fix light define
mcspr Jun 27, 2019
b8f2b0b
allow to create dummy relays and channels at runtime
mcspr Jun 27, 2019
1ff8e5b
wip: refactor dataframe/transport relation, work on discovery
mcspr Jul 1, 2019
e545c53
Finish draft, fixup missing files
mcspr Jul 6, 2019
a2936ba
style fixes
mcspr Jul 6, 2019
5ee15ab
guard terminal support, boot-time dp assoc, comments
mcspr Jul 6, 2019
8d3eba7
fixup! typo
mcspr Jul 6, 2019
c9cc323
fixup! typo2
mcspr Jul 6, 2019
7c09e0a
fix frame data accessor, fix accidental command matching
mcspr Jul 7, 2019
1178b9b
move helper class outside of ino
mcspr Jul 7, 2019
5b42a13
properly construct dataframe objects when copying / moving
mcspr Jul 7, 2019
419fe28
Support switch without lights
mcspr Jul 7, 2019
8bb2455
Update namespace
mcspr Jul 7, 2019
9600eb3
force const for write types
mcspr Jul 7, 2019
9c2678f
tuya_support define
mcspr Jul 7, 2019
f4fd5c9
bounds checks for lightChannel
mcspr Jul 7, 2019
7b0f6d6
use broker to dispatch events
mcspr Jul 7, 2019
651a5dc
report wifi status only when requested
mcspr Jul 7, 2019
aea6173
fixup frame data accessors
mcspr Jul 7, 2019
e55348e
Merge remote-tracking branch 'origin/dev' into experimental/tuya
mcspr Jul 7, 2019
fdd8c7f
fix missing header with core 2.3.0
mcspr Jul 7, 2019
abf84e0
Fix slow heartbeat
mcspr Jul 7, 2019
31b9fde
verbose
mcspr Jul 7, 2019
09b3d56
apply channel value when turning relay on
mcspr Jul 7, 2019
912bb88
fix tuya.info, implement tuya.save (current config)
mcspr Jul 7, 2019
f232341
return to discovery after processing frame
mcspr Jul 7, 2019
36383c3
extend to heartbeat
mcspr Jul 7, 2019
92dbd7b
process heartbeat only initially
mcspr Jul 7, 2019
d45b13b
specify that config is complete, like with settings setup
mcspr Jul 7, 2019
9bfa547
typo
mcspr Jul 7, 2019
526f421
naming
mcspr Jul 8, 2019
b13d3dc
naming
mcspr Jul 8, 2019
60c146d
Fix duplicating of states
mcspr Jul 15, 2019
88b5296
DataFrame default constructors
mcspr Jul 15, 2019
8743fd0
Hack! Ignore dimmer states until they are properly filtered
mcspr Jul 30, 2019
b47b141
tuya/states: implement vector proxy, mark as const access
mcspr Jul 30, 2019
2a2b134
:oops: esp8266 toolchain does not support c++17...
mcspr Jul 30, 2019
8795251
Merge remote-tracking branch 'origin/dev' into experimental/tuya
mcspr Jul 30, 2019
32e885f
Add safeguards when there are no channels configured yet
mcspr Aug 2, 2019
b588a05
domoticz: fix tuya integration autodiscovery
mcspr Aug 5, 2019
c653c11
tuya: restore vector proxy for tests
mcspr Aug 5, 2019
18a7e95
tuya: remove filtering hack, proper setting
mcspr Aug 5, 2019
0c2f285
fix filter consts
mcspr Aug 5, 2019
1e87b7f
Light: send target value instead of raw one
mcspr Aug 6, 2019
6d1906a
tuya: fix partial filter match disabling all external controls
mcspr Aug 6, 2019
57d3b73
Move consts to the proper class
mcspr Aug 7, 2019
7905a82
fully parse 32bit int
mcspr Aug 7, 2019
9d17ab2
use single dataframe class, store data ptr
mcspr Aug 7, 2019
0cac46f
naming, properly move ctor arg
mcspr Aug 7, 2019
4513498
Allow building without light support
mcspr Aug 9, 2019
6257f43
Merge remote-tracking branch 'origin/dev' into experimental/tuya
mcspr Nov 17, 2019
02988d5
fix merge
mcspr Nov 17, 2019
824fd67
fix merge again
mcspr Nov 17, 2019
ce3b005
upd header
mcspr Nov 17, 2019
f040cc3
...this is not python...
mcspr Nov 17, 2019
5e4452e
move dataframe contents knowledge to the appropriate file
mcspr Nov 18, 2019
a092414
Merge remote-tracking branch 'origin/dev' into experimental/tuya
mcspr Nov 18, 2019
b668030
check how "pio test" works
mcspr Nov 18, 2019
a7d3205
use ctor for transport conversion, shorter emplace
mcspr Nov 18, 2019
2f9993e
use proper constants
mcspr Nov 18, 2019
adbeaf8
separate broker callbacks per message type
mcspr Nov 19, 2019
da06d5b
add missing broker header
mcspr Nov 19, 2019
163c679
explicit cast to string, fix influx
mcspr Nov 19, 2019
5bc7ca5
shorter class
mcspr Nov 19, 2019
13b7815
fixup! explicit cast to string, fix influx
mcspr Nov 19, 2019
a00773b
use proper type for tbroker (unused atm)
mcspr Nov 19, 2019
5f4f6e7
tspk and domoticz only see switch status
mcspr Nov 19, 2019
e130491
regen webui again
mcspr Nov 19, 2019
34a307e
upd test with first arg as expected value
mcspr Nov 20, 2019
ef78abe
upd test with const_iterator parser
mcspr Nov 20, 2019
8fdcac0
use external lib for arduino emulation
mcspr Nov 20, 2019
17ce954
port StreamString, since this is an esp extension
mcspr Nov 20, 2019
867aeb8
fix _end pointing to the wrong place
mcspr Nov 20, 2019
082ebd1
replace lock relay type values to match pod struct defaults
mcspr Nov 20, 2019
58f4ce1
ensure broker callback gets called on relay setup
mcspr Nov 20, 2019
72156aa
fixup pio test deps
mcspr Nov 20, 2019
586a95b
...or just use lib_deps
mcspr Nov 20, 2019
b93488e
changelog
mcspr Nov 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Restore last schedule after reboot ([#1948](https://github.com/xoseperez/espurna/issues/1948), thanks to **[@foxman69](https://github.com/foxman69)**)
- Add `relayDelayOnN`, `relayDelayOffN`, `relayFloodTime`, `relayFloodCount` as runtime settings ([#1594](https://github.com/xoseperez/espurna/issues/1594), [#1962](https://github.com/xoseperez/espurna/pull/1962))
- Add experimental `relayDelayInterlock` to add a pause after turning relay off in SYNC\_ONE or SYNC\_NONE\_OR\_ONE modes ([#1510](https://github.com/xoseperez/espurna/issues/1510), [#1962](https://github.com/xoseperez/espurna/pull/1962))
- Add experimental `TUYA_SUPPORT` and `LIGHT_PROVIDER_TUYA` ([#1729](https://github.com/xoseperez/espurna/issues/1729), [#1997](https://github.com/xoseperez/espurna/issues/1997))
#### MQTT
- Add option to disable relay reporting ([#1645](https://github.com/xoseperez/espurna/issues/1645), thanks to **[@Niek](https://github.com/Niek)**)
- Safer settings reload and change detection ([#1701](https://github.com/xoseperez/espurna/issues/1701))
Expand Down Expand Up @@ -118,6 +119,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- ESP8266 FS-UAP1 Control board ([#1925](https://github.com/xoseperez/espurna/issues/1925), thanks to **[@NemoN](https://github.com/NemoN)**)
- Support for Teckin SP21 (Thanks to **[@xoseperez](https://github.com/xoseperez)**)
- Support for TFlag NX-SM100 and NX-SM200 power monitoring switches (Thanks to **[@xoseperez](https://github.com/xoseperez)**)
- Add tuya-generic-dimmer ([#1729](https://github.com/xoseperez/espurna/issues/1729), [#1997](https://github.com/xoseperez/espurna/issues/1997))
#### WebUI
- Configure WEB_REMOTE_DOMAIN at runtime ([#1789](https://github.com/xoseperez/espurna/issues/1789))
#### Modules
Expand Down
21 changes: 12 additions & 9 deletions code/espurna/alexa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

#if ALEXA_SUPPORT

#include "broker.h"

#include <fauxmoESP.h>
fauxmoESP alexa;

Expand Down Expand Up @@ -48,18 +50,19 @@ void _alexaConfigure() {
#endif

#if BROKER_SUPPORT
void _alexaBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
void _alexaBrokerCallback(const String& topic, unsigned char id, unsigned int value) {

// Only process status messages
if (BROKER_MSG_TYPE_STATUS != type) return;

unsigned char value = atoi(payload);
// Only process status messages for switches and channels
if (!topic.equals(MQTT_TOPIC_CHANNEL)
&& !topic.equals(MQTT_TOPIC_RELAY)) {
return;
}

if (strcmp(MQTT_TOPIC_CHANNEL, topic) == 0) {
alexa.setState(id+1, value > 0, value);
if (topic.equals(MQTT_TOPIC_CHANNEL)) {
alexa.setState(id + 1, value > 0, value);
}

if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
if (topic.equals(MQTT_TOPIC_RELAY)) {
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
if (id > 0) return;
#endif
Expand Down Expand Up @@ -146,7 +149,7 @@ void alexaSetup() {

// Register main callbacks
#if BROKER_SUPPORT
brokerRegister(_alexaBrokerCallback);
StatusBroker::Register(_alexaBrokerCallback);
#endif
espurnaRegisterReload(_alexaConfigure);
espurnaRegisterLoop(alexaLoop);
Expand Down
56 changes: 56 additions & 0 deletions code/espurna/broker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*

BROKER MODULE

Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>

*/

#if BROKER_SUPPORT

#pragma once

#include <functional>
#include <vector>
#include <utility>

enum class TBrokerType {
SYSTEM,
STATUS,
SENSOR,
DATETIME,
CONFIG
};

template <typename... TArgs>
using TBrokerCallback = std::function<void(TArgs...)>;

template <typename... TArgs>
using TBrokerCallbacks = std::vector<TBrokerCallback<TArgs...>>;

template <TBrokerType type, typename... TArgs>
struct TBroker {
static TBrokerCallbacks<TArgs...> callbacks;

static void Register(TBrokerCallback<TArgs...> callback) {
callbacks.push_back(callback);
}
static void Publish(TArgs... args) {
for (auto& callback : callbacks) {
callback(args...);
}
}
};

template <TBrokerType type, typename... TArgs>
TBrokerCallbacks<TArgs...> TBroker<type, TArgs...>::callbacks;


// --- Some known types. Bind them here to avoid .ino screwing with order ---

using StatusBroker = TBroker<TBrokerType::STATUS, const String&, unsigned char, unsigned int>;
using SensorBroker = TBroker<TBrokerType::SENSOR, const String&, unsigned char, double, const char*>;
using TimeBroker = TBroker<TBrokerType::DATETIME, const String&, time_t, const String&>;
using ConfigBroker = TBroker<TBrokerType::CONFIG, const String&, const String&>;

#endif // BROKER_SUPPORT
32 changes: 0 additions & 32 deletions code/espurna/broker.ino

This file was deleted.

13 changes: 13 additions & 0 deletions code/espurna/config/dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@
#undef SSDP_SUPPORT
#define SSDP_SUPPORT 0
#endif

#if LIGHT_PROVIDER == LIGHT_PROVIDER_TUYA
#undef TUYA_SUPPORT
#define TUYA_SUPPORT 1 // Need base Tuya module for this to work
#undef LIGHT_USE_TRANSITIONS
#define LIGHT_USE_TRANSITIONS 0 // TODO: temporary, maybe slower step instead?
#endif

#if TUYA_SUPPORT
#undef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // Broker is required to process relay & lights events
#endif

11 changes: 11 additions & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -1808,3 +1808,14 @@
#ifndef RFM69_IS_RFM69HW
#define RFM69_IS_RFM69HW 0
#endif

//--------------------------------------------------------------------------------
// TUYA switch & dimmer support
//--------------------------------------------------------------------------------
#ifndef TUYA_SUPPORT
#define TUYA_SUPPORT 0
#endif

#ifndef TUYA_SERIAL
#define TUYA_SERIAL Serial
#endif
11 changes: 10 additions & 1 deletion code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -3900,6 +3900,16 @@
#define LED2_RELAY 1
#define LED2_MODE LED_MODE_FINDME_WIFI

#elif defined(TUYA_GENERIC_DIMMER)

#define MANUFACTURER "TUYA"
#define DEVICE "GENERIC_DIMMER"

#define LIGHT_PROVIDER LIGHT_PROVIDER_TUYA
#define LIGHT_CHANNELS 0
#define RELAY_PROVIDER RELAY_PROVIDER_LIGHT
#define DUMMY_RELAY_COUNT 0

// -----------------------------------------------------------------------------
// Etekcity ESW01-USA
// https://www.amazon.com/Etekcity-Voltson-Outlet-Monitoring-Required/dp/B01M3MYIFS
Expand Down Expand Up @@ -4019,7 +4029,6 @@
#define HLW8012_POWER_RATIO 3711185
#define HLW8012_INTERRUPT_ON FALLING


// -----------------------------------------------------------------------------
// MUVIT_IO_MIOBULB001
// -----------------------------------------------------------------------------
Expand Down
12 changes: 5 additions & 7 deletions code/espurna/config/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ using api_put_callback_f = std::function<void(const char *)> ;
void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn = NULL);
#endif

// -----------------------------------------------------------------------------
// Broker
// -----------------------------------------------------------------------------
#if BROKER_SUPPORT
void brokerRegister(void (*)(const unsigned char, const char *, unsigned char, const char *));
#endif

// -----------------------------------------------------------------------------
// Debug
// -----------------------------------------------------------------------------
Expand All @@ -106,6 +99,9 @@ extern "C" {
void custom_crash_callback(struct rst_info*, uint32_t, uint32_t);
}

class PrintRaw;
class PrintHex;

// Core version 2.4.2 and higher changed the cont_t structure to a pointer:
// https://github.com/esp8266/Arduino/commit/5d5ea92a4d004ab009d5f642629946a0cb8893dd#diff-3fa12668b289ccb95b7ab334833a4ba8L35
// Core version 2.5.0 introduced EspClass helper method:
Expand Down Expand Up @@ -282,6 +278,8 @@ const String& relayPayloadOff();
const String& relayPayloadToggle();
const char* relayPayload(RelayStatus status);

void relaySetupDummy(unsigned char size, bool reconfigure = false);

// -----------------------------------------------------------------------------
// Settings
// -----------------------------------------------------------------------------
Expand Down
16 changes: 4 additions & 12 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
// Do not touch this definitions
//------------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// BROKER
// -----------------------------------------------------------------------------

#define BROKER_MSG_TYPE_SYSTEM 0
#define BROKER_MSG_TYPE_DATETIME 1
#define BROKER_MSG_TYPE_STATUS 2
#define BROKER_MSG_TYPE_SENSOR 3

// -----------------------------------------------------------------------------
// WIFI
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -103,9 +94,9 @@
#define RELAY_GROUP_SYNC_INVERSE 1
#define RELAY_GROUP_SYNC_RECEIVEONLY 2

#define RELAY_LOCK_OFF 0
#define RELAY_LOCK_ON 1
#define RELAY_LOCK_DISABLED 2
#define RELAY_LOCK_DISABLED 0
#define RELAY_LOCK_OFF 1
#define RELAY_LOCK_ON 2

//------------------------------------------------------------------------------
// UDP SYSLOG
Expand Down Expand Up @@ -192,6 +183,7 @@
#define LIGHT_PROVIDER_NONE 0
#define LIGHT_PROVIDER_MY92XX 1 // works with MY9291 and MY9231
#define LIGHT_PROVIDER_DIMMER 2
#define LIGHT_PROVIDER_TUYA 3

// -----------------------------------------------------------------------------
// SCHEDULER
Expand Down
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.lightfox.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfbridge.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfm69.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.small.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.thermostat.html.gz
Binary file not shown.
43 changes: 29 additions & 14 deletions code/espurna/domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

#if DOMOTICZ_SUPPORT

#include "broker.h"
#include <ArduinoJson.h>

bool _dcz_enabled = false;
Expand Down Expand Up @@ -38,10 +39,12 @@ void _domoticzMqttSubscribe(bool value) {
}

bool _domoticzStatus(unsigned char id) {
if (id >= _dcz_relay_state.size()) return false;
return _dcz_relay_state[id];
}

void _domoticzStatus(unsigned char id, bool status) {
if (id >= _dcz_relay_state.size()) return;
_dcz_relay_state[id] = status;
relayStatus(id, status);
}
Expand Down Expand Up @@ -162,19 +165,27 @@ void _domoticzMqtt(unsigned int type, const char * topic, char * payload) {
};

#if BROKER_SUPPORT
void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {

// Only process status messages
if (BROKER_MSG_TYPE_STATUS != type) return;
void _domoticzConfigCallback(const String& key, const String& value) {
if (key.equals("relayDummy")) {
_domoticzRelayConfigure(value.toInt());
return;
}
}

void _domoticzBrokerCallback(const String& topic, unsigned char id, unsigned int value) {

if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
bool status = atoi(payload) == 1;
if (_domoticzStatus(id) == status) return;
_dcz_relay_state[id] = status;
domoticzSendRelay(id, status);
// Only process status messages for switches
if (!topic.equals(MQTT_TOPIC_RELAY)) {
return;
}

if (_domoticzStatus(id) == value) return;
_dcz_relay_state[id] = value;
domoticzSendRelay(id, value);

}

#endif // BROKER_SUPPORT

#if WEB_SUPPORT
Expand Down Expand Up @@ -206,15 +217,18 @@ void _domoticzWebSocketOnConnected(JsonObject& root) {

#endif // WEB_SUPPORT

void _domoticzRelayConfigure(size_t size) {
_dcz_relay_state.reserve(size);
for (size_t n = 0; n < size; ++n) {
_dcz_relay_state[n] = relayStatus(n);
}
}

void _domoticzConfigure() {
bool enabled = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1;
if (enabled != _dcz_enabled) _domoticzMqttSubscribe(enabled);

_dcz_relay_state.reserve(relayCount());
for (size_t n = 0; n < relayCount(); ++n) {
_dcz_relay_state[n] = relayStatus(n);
}

_domoticzRelayConfigure(relayCount());
_dcz_enabled = enabled;
}

Expand Down Expand Up @@ -267,7 +281,8 @@ void domoticzSetup() {
#endif

#if BROKER_SUPPORT
brokerRegister(_domoticzBrokerCallback);
StatusBroker::Register(_domoticzBrokerCallback);
ConfigBroker::Register(_domoticzConfigCallback);
#endif

// Callbacks
Expand Down
Loading