Skip to content

Commit

Permalink
Merge PR #757: Meross: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins committed Jan 13, 2025
2 parents 61e2fa6 + 9e9e508 commit 1b51da8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
55 changes: 42 additions & 13 deletions meross/integrationpluginmeross.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Copyright 2013 - 2024, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
Expand Down Expand Up @@ -59,16 +59,44 @@ void IntegrationPluginMeross::discoverThings(ThingDiscoveryInfo *info)
connect(reply, &NetworkDeviceDiscoveryReply::finished, info, [info, reply, this](){
foreach (const NetworkDeviceInfo &deviceInfo, reply->networkDeviceInfos()) {
qCDebug(dcMeross) << "Discovery result" << deviceInfo;
if (deviceInfo.hostName().toLower().startsWith("meross_smart_plug") || deviceInfo.macAddressManufacturer().toLower().contains("meross")) {
ThingDescriptor descriptor(plugThingClassId, "Meross Smart Plug", deviceInfo.macAddress());
descriptor.setParams({Param(plugThingMacAddressParamTypeId, deviceInfo.macAddress())});

bool macVendorMatches = false;
foreach (const MacAddressInfo &macInfo, deviceInfo.macAddressInfos()) {
if (macInfo.vendorName().toLower().contains("meross")) {
macVendorMatches = true;
break;
}
}

if (deviceInfo.hostName().toLower().startsWith("meross_smart_plug") || macVendorMatches) {

QString description;
switch (deviceInfo.monitorMode()) {
case NetworkDeviceInfo::MonitorModeMac:
description = deviceInfo.macAddressInfos().constFirst().macAddress().toString();
break;
case NetworkDeviceInfo::MonitorModeHostName:
description = deviceInfo.hostName();
break;
case NetworkDeviceInfo::MonitorModeIp:
description = deviceInfo.address().toString();
break;
}

ThingDescriptor descriptor(plugThingClassId, "Meross Smart Plug", description);

ParamList params;
params.append(Param(plugThingMacAddressParamTypeId, deviceInfo.thingParamValueMacAddress()));
params.append(Param(plugThingHostNameParamTypeId, deviceInfo.thingParamValueHostName()));
params.append(Param(plugThingAddressParamTypeId, deviceInfo.thingParamValueAddress()));
descriptor.setParams(params);

Thing *existingThing = myThings().findByParams(descriptor.params());
if (existingThing) {
qCInfo(dcMeross) << "Existing smart plug discovered";
qCInfo(dcMeross) << "Existing smart plug discovered" << existingThing;
descriptor.setThingId(existingThing->id());
} else {
qCInfo(dcMeross) << "New smart plug discovered";
qCInfo(dcMeross) << "New smart plug discovered" << deviceInfo;
}

info->addThingDescriptor(descriptor);
Expand Down Expand Up @@ -154,13 +182,13 @@ void IntegrationPluginMeross::setupThing(ThingSetupInfo *info)
m_keys.insert(thing, pluginStorage()->value("key").toByteArray());
pluginStorage()->endGroup();

NetworkDeviceMonitor *monitor = m_deviceMonitors.take(thing);
NetworkDeviceMonitor *monitor = m_monitors.take(thing);
if (monitor) {
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(monitor);
}

monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(MacAddress(thing->paramValue(plugThingMacAddressParamTypeId).toString()));
m_deviceMonitors.insert(thing, monitor);
monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
m_monitors.insert(thing, monitor);

pollDevice5s(thing);
pollDevice60s(thing);
Expand All @@ -174,7 +202,7 @@ void IntegrationPluginMeross::postSetupThing(Thing */*thing*/)
m_timer5s = hardwareManager()->pluginTimerManager()->registerTimer(5);
connect(m_timer5s, &PluginTimer::timeout, this, [=](){
foreach (Thing *thing, myThings()) {
if (m_deviceMonitors.value(thing)->reachable()) {
if (m_monitors.value(thing)->reachable()) {
pollDevice5s(thing);
}
}
Expand All @@ -184,7 +212,7 @@ void IntegrationPluginMeross::postSetupThing(Thing */*thing*/)
m_timer5s = hardwareManager()->pluginTimerManager()->registerTimer(60);
connect(m_timer5s, &PluginTimer::timeout, this, [=](){
foreach (Thing *thing, myThings()) {
if (m_deviceMonitors.value(thing)->reachable()) {
if (m_monitors.value(thing)->reachable()) {
pollDevice60s(thing);
}
}
Expand All @@ -194,7 +222,8 @@ void IntegrationPluginMeross::postSetupThing(Thing */*thing*/)

void IntegrationPluginMeross::thingRemoved(Thing *thing)
{
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_deviceMonitors.take(thing));
if (m_monitors.contains(thing))
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));

if (myThings().isEmpty()) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_timer5s);
Expand Down Expand Up @@ -366,7 +395,7 @@ QNetworkReply* IntegrationPluginMeross::request(Thing *thing, const QString &nam

QUrl url;
url.setScheme("http");
url.setHost(m_deviceMonitors.value(thing)->networkDeviceInfo().address().toString());
url.setHost(m_monitors.value(thing)->networkDeviceInfo().address().toString());
url.setPath("/config");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
Expand Down
10 changes: 5 additions & 5 deletions meross/integrationpluginmeross.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Copyright 2013 - 2024, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
Expand Down Expand Up @@ -74,10 +74,10 @@ private slots:

QNetworkReply *request(Thing *thing, const QString &nameSpace, Method method = GET, const QVariantMap &payload = QVariantMap());

QHash<Thing*, QByteArray> m_keys;
QHash<Thing*, NetworkDeviceMonitor*> m_deviceMonitors;
PluginTimer* m_timer5s = nullptr;
PluginTimer* m_timer60s = nullptr;
QHash<Thing *, QByteArray> m_keys;
QHash<Thing *, NetworkDeviceMonitor *> m_monitors;
PluginTimer *m_timer5s = nullptr;
PluginTimer *m_timer60s = nullptr;
};

#endif // INTEGRATIONPLUGINMEROSS_H
22 changes: 20 additions & 2 deletions meross/integrationpluginmeross.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,31 @@
"displayName": "Smart plug",
"createMethods": ["discovery"],
"setupMethod": "userandpassword",
"interfaces": [ "powersocket", "smartmeterconsumer", "wirelessconnectable" ],
"interfaces": [ "powersocket", "smartmeterconsumer", "wirelessconnectable", "networkdevice"],
"paramTypes": [
{
"id": "1e273e10-3ea0-4337-a221-3b8e26c6e7dc",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString"
"type": "QString",
"inputType": "MacAddress",
"defaultValue": ""
},
{
"id": "e9a8097d-409b-4d2c-a4d5-86c5f4016b73",
"name":"hostName",
"displayName": "Host name",
"type": "QString",
"inputType": "TextLine",
"defaultValue": ""
},
{
"id": "7d2f1a1a-f26b-4ae4-820a-cbc381675f8b",
"name":"address",
"displayName": "IP address",
"type": "QString",
"inputType": "IPv4Address",
"defaultValue": ""
}
],
"stateTypes": [
Expand Down

0 comments on commit 1b51da8

Please sign in to comment.