Skip to content

Commit

Permalink
tuya: (best effort) relay provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Jan 11, 2023
1 parent 9b28d64 commit ef086ac
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 73 deletions.
56 changes: 28 additions & 28 deletions code/espurna/config/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -1344,60 +1344,60 @@
// Tuya
// -----------------------------------------------------------------------------

#ifndef TUYA_CH_STATE_DPID
#define TUYA_CH_STATE_DPID 0
#ifndef TUYA_LIGHT_STATE_DPID
#define TUYA_LIGHT_STATE_DPID 0
#endif

#ifndef TUYA_CH1_DPID
#define TUYA_CH1_DPID 0
#ifndef TUYA_LIGHT_CH1_DPID
#define TUYA_LIGHT_CH1_DPID 0
#endif

#ifndef TUYA_CH2_DPID
#define TUYA_CH2_DPID 0
#ifndef TUYA_LIGHT_CH2_DPID
#define TUYA_LIGHT_CH2_DPID 0
#endif

#ifndef TUYA_CH3_DPID
#define TUYA_CH3_DPID 0
#ifndef TUYA_LIGHT_CH3_DPID
#define TUYA_LIGHT_CH3_DPID 0
#endif

#ifndef TUYA_CH4_DPID
#define TUYA_CH4_DPID 0
#ifndef TUYA_LIGHT_CH4_DPID
#define TUYA_LIGHT_CH4_DPID 0
#endif

#ifndef TUYA_CH5_DPID
#define TUYA_CH5_DPID 0
#ifndef TUYA_LIGHT_CH5_DPID
#define TUYA_LIGHT_CH5_DPID 0
#endif

#ifndef TUYA_SW1_DPID
#define TUYA_SW1_DPID 0
#ifndef TUYA_RELAY1_DPID
#define TUYA_RELAY1_DPID 0
#endif

#ifndef TUYA_SW2_DPID
#define TUYA_SW2_DPID 0
#ifndef TUYA_RELAY2_DPID
#define TUYA_RELAY2_DPID 0
#endif

#ifndef TUYA_SW3_DPID
#define TUYA_SW3_DPID 0
#ifndef TUYA_RELAY3_DPID
#define TUYA_RELAY3_DPID 0
#endif

#ifndef TUYA_SW4_DPID
#define TUYA_SW4_DPID 0
#ifndef TUYA_RELAY4_DPID
#define TUYA_RELAY4_DPID 0
#endif

#ifndef TUYA_SW5_DPID
#define TUYA_SW5_DPID 0
#ifndef TUYA_RELAY5_DPID
#define TUYA_RELAY5_DPID 0
#endif

#ifndef TUYA_SW6_DPID
#define TUYA_SW6_DPID 0
#ifndef TUYA_RELAY6_DPID
#define TUYA_RELAY6_DPID 0
#endif

#ifndef TUYA_SW7_DPID
#define TUYA_SW7_DPID 0
#ifndef TUYA_RELAY7_DPID
#define TUYA_RELAY7_DPID 0
#endif

#ifndef TUYA_SW8_DPID
#define TUYA_SW8_DPID 0
#ifndef TUYA_RELAY8_DPID
#define TUYA_RELAY8_DPID 0
#endif

// -----------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -4677,11 +4677,11 @@
#define TUYA_SUPPORT 1
#define TUYA_PORT 1

#define TUYA_CH_STATE_DPID 1
#define TUYA_CH1_DPID 2
#define LIGHT_PROVIDER LIGHT_PROVIDER_CUSTOM
#define TUYA_LIGHT_CH1_DPID 2
#define TUYA_LIGHT_STATE_DPID 1

#define LIGHT_PROVIDER LIGHT_PROVIDER_CUSTOM
#define RELAY1_PROVIDER RELAY_PROVIDER_NONE
#define RELAY1_PROVIDER RELAY_PROVIDER_LIGHT_STATE

#define LED1_GPIO 14

Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#define RELAY_PROVIDER_LIGHT_STATE RelayProvider::LightState
#define RELAY_PROVIDER_FAN RelayProvider::Fan
#define RELAY_PROVIDER_LIGHTFOX RelayProvider::Lightfox
#define RELAY_PROVIDER_TUYA RelayProvider::Tuya

#define RFB_PROVIDER_RCSWITCH 0
#define RFB_PROVIDER_EFM8BB1 1
Expand Down
8 changes: 8 additions & 0 deletions code/espurna/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include "api.h"
#include "mqtt.h"
#include "relay.h"
#include "tuya.h"
#include "rpc.h"
#include "rtcmem.h"
#include "light.h"
Expand Down Expand Up @@ -68,6 +69,7 @@ enum class RelayProvider {
LightState,
Fan,
Lightfox,
Tuya,
};

enum class RelaySync {
Expand Down Expand Up @@ -2948,6 +2950,12 @@ RelayProviderBasePtr _relaySetupProvider(size_t index) {
#endif
break;

case RelayProvider::Tuya:
#if TUYA_SUPPORT
result = tuya::makeRelayProvider(index);
#endif
break;

case RelayProvider::None:
break;
}
Expand Down
70 changes: 29 additions & 41 deletions code/espurna/tuya.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ namespace tuya {
_dp(dp)
{}

const char* id() const {
return "tuya";
espurna::StringView id() const {
return STRING_VIEW("tuya");
}

void change(bool status) {
send(_dp, status);
}

private:
unsigned char _dp;
};
Expand Down Expand Up @@ -363,7 +364,6 @@ namespace tuya {
#if LIGHT_PROVIDER == LIGHT_PROVIDER_CUSTOM
setupChannels();
#endif
setupSwitches();
}

DEBUG_MSG_P(PSTR("[TUYA] Starting discovery\n"));
Expand Down Expand Up @@ -518,26 +518,26 @@ namespace tuya {
namespace build {

constexpr unsigned char channelDpId(size_t index) {
return (index == 0) ? TUYA_CH1_DPID :
(index == 1) ? TUYA_CH2_DPID :
(index == 2) ? TUYA_CH3_DPID :
(index == 3) ? TUYA_CH4_DPID :
(index == 4) ? TUYA_CH5_DPID : 0u;
return (index == 0) ? TUYA_LIGHT_CH1_DPID :
(index == 1) ? TUYA_LIGHT_CH2_DPID :
(index == 2) ? TUYA_LIGHT_CH3_DPID :
(index == 3) ? TUYA_LIGHT_CH4_DPID :
(index == 4) ? TUYA_LIGHT_CH5_DPID : 0u;
}

constexpr unsigned char switchDpId(size_t index) {
return (index == 0) ? TUYA_SW1_DPID :
(index == 1) ? TUYA_SW2_DPID :
(index == 2) ? TUYA_SW3_DPID :
(index == 3) ? TUYA_SW4_DPID :
(index == 4) ? TUYA_SW5_DPID :
(index == 5) ? TUYA_SW6_DPID :
(index == 6) ? TUYA_SW7_DPID :
(index == 7) ? TUYA_SW8_DPID : 0u;
return (index == 0) ? TUYA_RELAY1_DPID :
(index == 1) ? TUYA_RELAY2_DPID :
(index == 2) ? TUYA_RELAY3_DPID :
(index == 3) ? TUYA_RELAY4_DPID :
(index == 4) ? TUYA_RELAY5_DPID :
(index == 5) ? TUYA_RELAY6_DPID :
(index == 6) ? TUYA_RELAY7_DPID :
(index == 7) ? TUYA_RELAY8_DPID : 0u;
}

constexpr unsigned char channelStateDpId() {
return TUYA_CH_STATE_DPID;
return TUYA_LIGHT_STATE_DPID;
}

} // namespace build
Expand All @@ -546,30 +546,6 @@ namespace tuya {
// Respective provider setup should be called before state restore,
// so we can use dummy values

void setupSwitches() {
bool done { false };
for (size_t id = 0; id < RelaysMax; ++id) {
auto dp = getSetting({"tuyaSwitch", id}, build::switchDpId(id));
if (!dp) {
break;
}

if (!switchIds.add(relayCount(), dp)) {
break;
}

if (!relayAdd(std::make_unique<TuyaRelayProvider>(dp))) {
break;
}

done = true;
}

if (done) {
configDone.set();
}
}

#if LIGHT_PROVIDER == LIGHT_PROVIDER_CUSTOM

void setupChannels() {
Expand Down Expand Up @@ -654,6 +630,18 @@ namespace tuya {
}
#endif

RelayProviderBasePtr makeRelayProvider(size_t index) {
RelayProviderBasePtr out;

const auto dpId = build::switchDpId(index);
if (!switchIds.add(index, dpId)) {
out = std::make_unique<TuyaRelayProvider>(dpId);
configDone.set();
}

return out;
}

void setup() {
const auto port = uartPort(TUYA_PORT - 1);

Expand Down
7 changes: 7 additions & 0 deletions code/espurna/tuya.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#pragma once

#include <cstddef>
#include <memory>

class RelayProviderBase;

namespace tuya {

void setupChannels();
Expand All @@ -14,4 +19,6 @@ void sendSwitch(unsigned char, bool);

void setup();

std::unique_ptr<RelayProviderBase> makeRelayProvider(size_t);

} // namespace tuya

0 comments on commit ef086ac

Please sign in to comment.