From d85b3d30c8d622e02324ef5481159e2edad92898 Mon Sep 17 00:00:00 2001 From: cjkas Date: Mon, 12 Oct 2026 20:55:21 +0200 Subject: [PATCH] fix: turn a group's sun flag on, not off, for MQTT sunFlag 1 groups/+/sunFlag/set sent Flag for 1 and SunFlag for 0, the opposite of the shade topic. SunFlag turns the flag on. Both topics now take the command from sunFlagCommand() so they can't drift apart again. Co-Authored-By: Claude Opus 5.5 --- src/MQTT.cpp | 8 ++------ src/SomfyFrame.cpp | 1 + src/SomfyFrame.h | 2 ++ test/test_frame/test_frame.cpp | 6 ++++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/MQTT.cpp b/src/MQTT.cpp index 4332227..dba7aaf 100644 --- a/src/MQTT.cpp +++ b/src/MQTT.cpp @@ -127,8 +127,7 @@ void MQTTClass::receive(const char *topic, byte*payload, uint32_t length) { shade->setMyPosition(shade->myPos, val); } else if(strncmp(command, "sunFlag", sizeof(command)) == 0) { - if(val > 0) shade->sendCommand(somfy_commands::SunFlag); - else shade->sendCommand(somfy_commands::Flag); + shade->sendCommand(sunFlagCommand(val > 0)); } else if(strncmp(command, "position", sizeof(command)) == 0) { if(val >= 0 && val <= 100) { @@ -164,10 +163,7 @@ void MQTTClass::receive(const char *topic, byte*payload, uint32_t length) { group->sendCommand(somfy_commands::My); } else if(strncmp(command, "sunFlag", sizeof(command)) == 0) { - if(val > 0) - group->sendCommand(somfy_commands::Flag); - else - group->sendCommand(somfy_commands::SunFlag); + group->sendCommand(sunFlagCommand(val > 0)); } else if(strncmp(command, "sunny", sizeof(command)) == 0) { if(val >= 0) group->sendSensorCommand(-1, val, group->repeats); diff --git a/src/SomfyFrame.cpp b/src/SomfyFrame.cpp index 629fe74..893f2af 100644 --- a/src/SomfyFrame.cpp +++ b/src/SomfyFrame.cpp @@ -103,6 +103,7 @@ somfy_commands flipCommand(somfy_commands cmd) { return cmd; } } +somfy_commands sunFlagCommand(bool on) { return on ? somfy_commands::SunFlag : somfy_commands::Flag; } byte somfy_frame_t::calc80Checksum(byte b0, byte b1, byte b2) { byte cs80 = 0; cs80 = (((b0 & 0xF0) >> 4) ^ ((b1 & 0xF0) >> 4)); diff --git a/src/SomfyFrame.h b/src/SomfyFrame.h index 011de09..d578f73 100644 --- a/src/SomfyFrame.h +++ b/src/SomfyFrame.h @@ -37,6 +37,8 @@ String translateSomfyCommand(const somfy_commands cmd); somfy_commands translateSomfyCommand(const String& string); // The command with up and down swapped, for motors wired the other way round. somfy_commands flipCommand(somfy_commands cmd); +// The command that turns the sun flag on (SunFlag) or off (Flag). +somfy_commands sunFlagCommand(bool on); struct somfy_rx_t; struct somfy_frame_t { diff --git a/test/test_frame/test_frame.cpp b/test/test_frame/test_frame.cpp index 66f00cf..2f6fb1f 100644 --- a/test/test_frame/test_frame.cpp +++ b/test/test_frame/test_frame.cpp @@ -240,6 +240,11 @@ static void test_command_names_round_trip() { } TEST_ASSERT_EQUAL_STRING_MESSAGE("", failed.c_str(), "These names don't parse back to their command"); } +static void test_sun_flag_command() { + // sunFlag/set 1 must turn the flag on for shades and groups alike. + TEST_ASSERT_EQUAL_UINT8((uint8_t)somfy_commands::SunFlag, (uint8_t)sunFlagCommand(true)); + TEST_ASSERT_EQUAL_UINT8((uint8_t)somfy_commands::Flag, (uint8_t)sunFlagCommand(false)); +} int main(int argc, char **argv) { (void)argc; @@ -262,5 +267,6 @@ int main(int argc, char **argv) { RUN_TEST(test_parse_command_names); RUN_TEST(test_command_display_names); RUN_TEST(test_command_names_round_trip); + RUN_TEST(test_sun_flag_command); return UNITY_END(); }