From dd8d40b4280c45b1f998678ad85d035171a8f371 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 9 Aug 2021 00:15:46 +0200 Subject: [PATCH] Shelly: Add generic IO states for shelly 2.5 --- shelly/integrationpluginshelly.cpp | 23 ++++++++++++++++++++++- shelly/integrationpluginshelly.json | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/shelly/integrationpluginshelly.cpp b/shelly/integrationpluginshelly.cpp index 6700db37..085c91a7 100644 --- a/shelly/integrationpluginshelly.cpp +++ b/shelly/integrationpluginshelly.cpp @@ -320,6 +320,8 @@ static QHash powerActionTypesMap = { {shellyLightPMPowerActionTypeId, shellyLightPMThingClassId}, {shellySocketPMPowerActionTypeId, shellySocketPMThingClassId}, {shellyEm3PowerActionTypeId, shellyEm3ThingClassId}, + {shelly25Channel1ActionTypeId, shelly25ThingClassId}, + {shelly25Channel2ActionTypeId, shelly25ThingClassId} }; static QHash powerActionParamTypesMap = { @@ -334,6 +336,8 @@ static QHash powerActionParamTypesMap = { {shellyLightPMPowerActionTypeId, shellyLightPMPowerActionPowerParamTypeId}, {shellySocketPMPowerActionTypeId, shellySocketPMPowerActionPowerParamTypeId}, {shellyEm3PowerActionTypeId, shellyEm3PowerActionPowerParamTypeId}, + {shelly25Channel1ActionTypeId, shelly25Channel1ActionChannel1ParamTypeId}, + {shelly25Channel2ActionTypeId, shelly25Channel2ActionChannel2ParamTypeId} }; static QHash colorPowerActionTypesMap = { @@ -548,8 +552,14 @@ void IntegrationPluginShelly::executeAction(ThingActionInfo *info) MqttChannel *channel = m_mqttChannels.value(parentDevice); QString shellyId = parentDevice->paramValue(idParamTypeMap.value(parentDevice->thingClassId())).toString(); int relay = 1; + QHash actionChannelMap = { + {shelly25Channel1ActionTypeId, 1}, + {shelly25Channel2ActionTypeId, 2} + }; if (channelParamTypeMap.contains(thing->thingClassId())) { relay = thing->paramValue(channelParamTypeMap.value(thing->thingClassId())).toInt(); + } else if (actionChannelMap.contains(action.actionTypeId())) { + relay = actionChannelMap.value(action.actionTypeId()); } ParamTypeId powerParamTypeId = powerActionParamTypesMap.value(action.actionTypeId()); bool on = action.param(powerParamTypeId).value().toBool(); @@ -822,6 +832,15 @@ void IntegrationPluginShelly::onPublishReceived(MqttChannel *channel, const QStr if (powerStateTypeMap.contains(thing->thingClassId())) { thing->setStateValue(powerStateTypeMap.value(thing->thingClassId()), on); } + // If the shelly main thing has multiple channels (e.g. Shelly 2.5) + if (thing->thingClassId() == shelly25ThingClassId) { + QHash powerChannelStateTypesMap = { + {0, shelly25Channel1StateTypeId}, + {1, shelly25Channel2StateTypeId} + }; + thing->setStateValue(powerChannelStateTypesMap.value(channel), on); + } + // And switch all childs of this shelly too foreach (Thing *child, myThings().filterByParentId(thing->id())) { if (powerStateTypeMap.contains(child->thingClassId())) { @@ -831,6 +850,7 @@ void IntegrationPluginShelly::onPublishReceived(MqttChannel *channel, const QStr } } } + } topicMatcher = QRegExp("shellies/" + shellyId + "/(relay|roller)/[0-1]/power"); @@ -1156,12 +1176,13 @@ void IntegrationPluginShelly::setupShellyGateway(ThingSetupInfo *info) } MqttChannel *channel = hardwareManager()->mqttProvider()->createChannel(shellyId, QHostAddress(address), {"shellies"}); - m_mqttChannels.insert(info->thing(), channel); if (!channel) { qCWarning(dcShelly()) << "Failed to create MQTT channel."; return info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error creating MQTT channel. Please check MQTT server settings.")); } + + m_mqttChannels.insert(info->thing(), channel); connect(channel, &MqttChannel::clientConnected, this, &IntegrationPluginShelly::onClientConnected); connect(channel, &MqttChannel::clientDisconnected, this, &IntegrationPluginShelly::onClientDisconnected); connect(channel, &MqttChannel::publishReceived, this, &IntegrationPluginShelly::onPublishReceived); diff --git a/shelly/integrationpluginshelly.json b/shelly/integrationpluginshelly.json index a9366396..eb3aef57 100644 --- a/shelly/integrationpluginshelly.json +++ b/shelly/integrationpluginshelly.json @@ -531,6 +531,28 @@ "displayNameEvent": "Available firmware version changed", "type": "QString", "defaultValue": "" + }, + { + "id": "118d572c-cc12-4037-82d8-7d8f6fb4a364", + "name": "channel1", + "displayName": "Power channel 1", + "displayNameEvent": "Channel 1 turned on or off", + "displayNameAction": "Turn channel 1 on or off", + "type": "bool", + "defaultValue": false, + "writable": true, + "ioType": "digitalOutput" + }, + { + "id": "7952aec0-cd27-4ef9-87a6-c499564bc1d4", + "name": "channel2", + "displayName": "Power channel 2", + "displayNameEvent": "Channel 2 turned on or off", + "displayNameAction": "Turn channel 2 on or off", + "type": "bool", + "defaultValue": false, + "writable": true, + "ioType": "digitalOutput" } ], "actionTypes": [