From a9ae2128a261256d4c70065a40fad24251eef302 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 12 Apr 2021 01:20:25 +0200 Subject: [PATCH] Tasmota: Add support for the Sonoff Dimmer --- tasmota/integrationplugintasmota.cpp | 36 ++++++++++++++-- tasmota/integrationplugintasmota.h | 2 + tasmota/integrationplugintasmota.json | 59 +++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/tasmota/integrationplugintasmota.cpp b/tasmota/integrationplugintasmota.cpp index 5a477e2a..1eb499b4 100644 --- a/tasmota/integrationplugintasmota.cpp +++ b/tasmota/integrationplugintasmota.cpp @@ -44,6 +44,9 @@ static QHash sonoff_basicPowerStateTypeIds = { {"POWER1", sonoff_basicPowerStateTypeId}, }; +static QHash sonoff_dimmerPowerStateTypeIds = { + {"POWER1", sonoff_dimmerPowerStateTypeId}, +}; static QHash sonoff_dualPowerStateTypeIds = { {"POWER1", sonoff_dualPowerCH1StateTypeId}, {"POWER2", sonoff_dualPowerCH2StateTypeId}, @@ -64,6 +67,7 @@ static QHash> stateMaps = { {sonoff_dualThingClassId, sonoff_dualPowerStateTypeIds}, {sonoff_triThingClassId, sonoff_triPowerStateTypeIds}, {sonoff_quadThingClassId, sonoff_quadPowerStateTypeIds}, + {sonoff_dimmerThingClassId, sonoff_dimmerPowerStateTypeIds} }; IntegrationPluginTasmota::IntegrationPluginTasmota() @@ -73,6 +77,7 @@ IntegrationPluginTasmota::IntegrationPluginTasmota() m_ipAddressParamTypeMap[sonoff_dualThingClassId] = sonoff_dualThingIpAddressParamTypeId; m_ipAddressParamTypeMap[sonoff_triThingClassId] = sonoff_triThingIpAddressParamTypeId; m_ipAddressParamTypeMap[sonoff_quadThingClassId] = sonoff_quadThingIpAddressParamTypeId; + m_ipAddressParamTypeMap[sonoff_dimmerThingClassId] = sonoff_dimmerThingIpAddressParamTypeId; m_attachedDeviceParamTypeIdMap[sonoff_basicThingClassId] << sonoff_basicThingAttachedDeviceCH1ParamTypeId; m_attachedDeviceParamTypeIdMap[sonoff_dualThingClassId] << sonoff_dualThingAttachedDeviceCH1ParamTypeId << sonoff_dualThingAttachedDeviceCH2ParamTypeId; @@ -103,6 +108,7 @@ IntegrationPluginTasmota::IntegrationPluginTasmota() m_connectedStateTypeMap[sonoff_dualThingClassId] = sonoff_dualConnectedStateTypeId; m_connectedStateTypeMap[sonoff_triThingClassId] = sonoff_triConnectedStateTypeId; m_connectedStateTypeMap[sonoff_quadThingClassId] = sonoff_quadConnectedStateTypeId; + m_connectedStateTypeMap[sonoff_dimmerThingClassId] = sonoff_dimmerConnectedStateTypeId; m_connectedStateTypeMap[tasmotaSwitchThingClassId] = tasmotaSwitchConnectedStateTypeId; m_connectedStateTypeMap[tasmotaLightThingClassId] = tasmotaLightConnectedStateTypeId; m_connectedStateTypeMap[tasmotaShutterThingClassId] = tasmotaShutterConnectedStateTypeId; @@ -112,10 +118,13 @@ IntegrationPluginTasmota::IntegrationPluginTasmota() m_signalStrengthStateTypeMap[sonoff_dualThingClassId] = sonoff_dualSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[sonoff_triThingClassId] = sonoff_triSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[sonoff_quadThingClassId] = sonoff_quadSignalStrengthStateTypeId; + m_signalStrengthStateTypeMap[sonoff_dimmerThingClassId] = sonoff_dimmerSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[tasmotaSwitchThingClassId] = tasmotaSwitchSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[tasmotaLightThingClassId] = tasmotaLightSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[tasmotaShutterThingClassId] = tasmotaShutterSignalStrengthStateTypeId; m_signalStrengthStateTypeMap[tasmotaBlindsThingClassId] = tasmotaBlindsSignalStrengthStateTypeId; + + m_brightnessStateTypeMap[sonoff_dimmerThingClassId] = sonoff_dimmerBrightnessStateTypeId; } IntegrationPluginTasmota::~IntegrationPluginTasmota() @@ -285,7 +294,8 @@ void IntegrationPluginTasmota::executeAction(ThingActionInfo *info) if (thing->thingClassId() == sonoff_basicThingClassId || thing->thingClassId() == sonoff_dualThingClassId || thing->thingClassId() == sonoff_triThingClassId - || thing->thingClassId() == sonoff_quadThingClassId) { + || thing->thingClassId() == sonoff_quadThingClassId + || action.actionTypeId() == sonoff_dimmerPowerActionTypeId) { MqttChannel *channel = m_mqttChannels.value(thing); if (!channel) { qCWarning(dcTasmota()) << "No MQTT channel for this thing."; @@ -293,8 +303,24 @@ void IntegrationPluginTasmota::executeAction(ThingActionInfo *info) return; } QString channelName = stateMaps.value(thing->thingClassId()).key(action.actionTypeId()); - qCDebug(dcTasmota) << "Publishing:" << channel->topicPrefixList().first() + "/sonoff/cmnd/" + channelName << (action.paramValue(action.actionTypeId()).toBool() ? "ON" : "OFF"); - channel->publish(channel->topicPrefixList().first() + "/sonoff/cmnd/" + channelName, action.paramValue(action.actionTypeId()).toBool() ? "ON" : "OFF"); + QByteArray payload = action.paramValue(action.actionTypeId()).toBool() ? "ON" : "OFF"; + qCDebug(dcTasmota) << "Publishing:" << channel->topicPrefixList().first() + "/sonoff/cmnd/" + channelName << payload; + channel->publish(channel->topicPrefixList().first() + "/sonoff/cmnd/" + channelName, payload); + thing->setStateValue(action.actionTypeId(), action.paramValue(action.actionTypeId())); + info->finish(Thing::ThingErrorNoError); + return; + } + if (action.actionTypeId() == sonoff_dimmerBrightnessActionTypeId) { + MqttChannel *channel = m_mqttChannels.value(thing); + if (!channel) { + qCWarning(dcTasmota()) << "No MQTT channel for this thing:" << thing->name(); + info->finish(Thing::ThingErrorHardwareNotAvailable); + return; + } + QString channelName = stateMaps.value(thing->thingClassId()).key(action.actionTypeId()); + QByteArray payload = QByteArray::number(action.paramValue(action.actionTypeId()).toInt()); + qCDebug(dcTasmota) << "Publishing:" << channel->topicPrefixList().first() + "/sonoff/cmnd/DIMMER1" + channelName << payload; + channel->publish(channel->topicPrefixList().first() + "/sonoff/cmnd/DIMMER1" + channelName, payload); thing->setStateValue(action.actionTypeId(), action.paramValue(action.actionTypeId())); info->finish(Thing::ThingErrorNoError); return; @@ -404,6 +430,10 @@ void IntegrationPluginTasmota::onPublishReceived(MqttChannel *channel, const QSt QVariantMap dataMap = jsonDoc.toVariant().toMap(); thing->setStateValue(m_signalStrengthStateTypeMap.value(thing->thingClassId()), dataMap.value("Wifi").toMap().value("RSSI").toInt()); + if (m_brightnessStateTypeMap.contains(thing->thingClassId())) { + thing->setStateValue(m_brightnessStateTypeMap.value(thing->thingClassId()), dataMap.value("Dimmer").toInt()); + } + // Legacy (deprecated) connected things by params foreach (Thing *child, myThings().filterByParentId(thing->id())) { if (m_powerStateTypeMap.contains(child->thingClassId())) { diff --git a/tasmota/integrationplugintasmota.h b/tasmota/integrationplugintasmota.h index 2bc72616..a499234d 100644 --- a/tasmota/integrationplugintasmota.h +++ b/tasmota/integrationplugintasmota.h @@ -77,6 +77,8 @@ private: // Helpers for both devices QHash m_connectedStateTypeMap; QHash m_signalStrengthStateTypeMap; + + QHash m_brightnessStateTypeMap; }; #endif // INTEGRATIONPLUGINTASMOTA_H diff --git a/tasmota/integrationplugintasmota.json b/tasmota/integrationplugintasmota.json index 3899c872..59b0833e 100644 --- a/tasmota/integrationplugintasmota.json +++ b/tasmota/integrationplugintasmota.json @@ -346,6 +346,65 @@ } ] }, + { + "id": "4fb65caa-7f84-4496-91f6-1cd35574cc2e", + "name": "sonoff_dimmer", + "displayName": "Dimmer (Sonoff D1...)", + "createMethods": ["user"], + "interfaces": [ "dimmablelight", "wirelessconnectable" ], + "paramTypes": [ + { + "id": "935a5a79-900a-4943-a9e2-5de228d66758", + "name":"ipAddress", + "displayName": "IP address", + "type": "QString" + } + ], + "stateTypes": [ + { + "id": "1078dd25-b5bd-4005-8c1f-005ddca7ea21", + "name": "connected", + "displayName": "Connected", + "displayNameEvent": "Connected changed", + "type": "bool", + "defaultValue": false, + "cached": false + }, + { + "id": "662d99de-bfce-4cf0-8029-108e9911866c", + "name": "signalStrength", + "displayName": "Signal strength", + "displayNameEvent": "Signal strength changed", + "type": "uint", + "unit": "Percentage", + "minValue": 0, + "maxValue": 100, + "defaultValue": -1 + }, + { + "id": "c8ec62d4-024b-4770-b893-6288b64f0dfe", + "name": "power", + "displayName": "Power", + "displayNameEvent": "Power changed", + "displayNameAction": "Set power", + "type": "bool", + "writable": true, + "defaultValue": false + }, + { + "id": "5cd8d0df-1188-4da1-9a87-bff991d3b2af", + "name": "brightness", + "displayName": "Brightness", + "displayNameEvent": "Brightness changed", + "displayNameAction": "Set brightness", + "type": "int", + "minValue": 0, + "maxValue": 100, + "defaultValue": 50, + "writable": true + } + ] + }, { "id": "8a5e69c0-14ad-4ae8-9ff9-10055de6ffdf", "name": "tasmotaSwitch",