Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IRAc::opmodeToString() output nicer for humans. #1613

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/IRMQTTServer/IRMQTTServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2875,10 +2875,10 @@ void sendJsonState(const stdAc::state_t state, const String topic,
json[KEY_PROTOCOL] = typeToString(state.protocol);
json[KEY_MODEL] = state.model;
json[KEY_POWER] = IRac::boolToString(state.power);
json[KEY_MODE] = IRac::opmodeToString(state.mode);
json[KEY_MODE] = IRac::opmodeToString(state.mode, ha_mode);
// Home Assistant wants mode to be off if power is also off & vice-versa.
if (ha_mode && (state.mode == stdAc::opmode_t::kOff || !state.power)) {
json[KEY_MODE] = IRac::opmodeToString(stdAc::opmode_t::kOff);
json[KEY_MODE] = IRac::opmodeToString(stdAc::opmode_t::kOff, ha_mode);
json[KEY_POWER] = IRac::boolToString(false);
}
json[KEY_CELSIUS] = IRac::boolToString(state.celsius);
Expand Down Expand Up @@ -3024,7 +3024,11 @@ bool sendClimate(const String topic_prefix, const bool retain,
diff = true;
success &= sendInt(topic_prefix + KEY_MODEL, next.model, retain);
}
#ifdef MQTT_CLIMATE_HA_MODE
String mode_str = IRac::opmodeToString(next.mode, MQTT_CLIMATE_HA_MODE);
#else // MQTT_CLIMATE_HA_MODE
String mode_str = IRac::opmodeToString(next.mode);
#endif // MQTT_CLIMATE_HA_MODE
// I don't know why, but the modes need to be lower case to work with
// Home Assistant & Google Home.
mode_str.toLowerCase();
Expand Down
96 changes: 33 additions & 63 deletions src/IRac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3308,23 +3308,17 @@ String IRac::boolToString(const bool value) {

/// Convert the supplied operation mode into the appropriate String.
/// @param[in] mode The enum to be converted.
/// @param[in] ha A flag to indicate we want GoogleHome/HomeAssistant output.
/// @return The equivalent String for the locale.
String IRac::opmodeToString(const stdAc::opmode_t mode) {
String IRac::opmodeToString(const stdAc::opmode_t mode, const bool ha) {
switch (mode) {
case stdAc::opmode_t::kOff:
return kOffStr;
case stdAc::opmode_t::kAuto:
return kAutoStr;
case stdAc::opmode_t::kCool:
return kCoolStr;
case stdAc::opmode_t::kHeat:
return kHeatStr;
case stdAc::opmode_t::kDry:
return kDryStr;
case stdAc::opmode_t::kFan:
return kFanOnlyStr;
default:
return kUnknownStr;
case stdAc::opmode_t::kOff: return kOffStr;
case stdAc::opmode_t::kAuto: return kAutoStr;
case stdAc::opmode_t::kCool: return kCoolStr;
case stdAc::opmode_t::kHeat: return kHeatStr;
case stdAc::opmode_t::kDry: return kDryStr;
case stdAc::opmode_t::kFan: return ha ? kFanOnlyStr : kFanStr;
default: return kUnknownStr;
}
}

Expand All @@ -3333,20 +3327,13 @@ String IRac::opmodeToString(const stdAc::opmode_t mode) {
/// @return The equivalent String for the locale.
String IRac::fanspeedToString(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kAuto:
return kAutoStr;
case stdAc::fanspeed_t::kMax:
return kMaxStr;
case stdAc::fanspeed_t::kHigh:
return kHighStr;
case stdAc::fanspeed_t::kMedium:
return kMediumStr;
case stdAc::fanspeed_t::kLow:
return kLowStr;
case stdAc::fanspeed_t::kMin:
return kMinStr;
default:
return kUnknownStr;
case stdAc::fanspeed_t::kAuto: return kAutoStr;
case stdAc::fanspeed_t::kMax: return kMaxStr;
case stdAc::fanspeed_t::kHigh: return kHighStr;
case stdAc::fanspeed_t::kMedium: return kMediumStr;
case stdAc::fanspeed_t::kLow: return kLowStr;
case stdAc::fanspeed_t::kMin: return kMinStr;
default: return kUnknownStr;
}
}

Expand All @@ -3355,22 +3342,14 @@ String IRac::fanspeedToString(const stdAc::fanspeed_t speed) {
/// @return The equivalent String for the locale.
String IRac::swingvToString(const stdAc::swingv_t swingv) {
switch (swingv) {
case stdAc::swingv_t::kOff:
return kOffStr;
case stdAc::swingv_t::kAuto:
return kAutoStr;
case stdAc::swingv_t::kHighest:
return kHighestStr;
case stdAc::swingv_t::kHigh:
return kHighStr;
case stdAc::swingv_t::kMiddle:
return kMiddleStr;
case stdAc::swingv_t::kLow:
return kLowStr;
case stdAc::swingv_t::kLowest:
return kLowestStr;
default:
return kUnknownStr;
case stdAc::swingv_t::kOff: return kOffStr;
case stdAc::swingv_t::kAuto: return kAutoStr;
case stdAc::swingv_t::kHighest: return kHighestStr;
case stdAc::swingv_t::kHigh: return kHighStr;
case stdAc::swingv_t::kMiddle: return kMiddleStr;
case stdAc::swingv_t::kLow: return kLowStr;
case stdAc::swingv_t::kLowest: return kLowestStr;
default: return kUnknownStr;
}
}

Expand All @@ -3379,24 +3358,15 @@ String IRac::swingvToString(const stdAc::swingv_t swingv) {
/// @return The equivalent String for the locale.
String IRac::swinghToString(const stdAc::swingh_t swingh) {
switch (swingh) {
case stdAc::swingh_t::kOff:
return kOffStr;
case stdAc::swingh_t::kAuto:
return kAutoStr;
case stdAc::swingh_t::kLeftMax:
return kLeftMaxStr;
case stdAc::swingh_t::kLeft:
return kLeftStr;
case stdAc::swingh_t::kMiddle:
return kMiddleStr;
case stdAc::swingh_t::kRight:
return kRightStr;
case stdAc::swingh_t::kRightMax:
return kRightMaxStr;
case stdAc::swingh_t::kWide:
return kWideStr;
default:
return kUnknownStr;
case stdAc::swingh_t::kOff: return kOffStr;
case stdAc::swingh_t::kAuto: return kAutoStr;
case stdAc::swingh_t::kLeftMax: return kLeftMaxStr;
case stdAc::swingh_t::kLeft: return kLeftStr;
case stdAc::swingh_t::kMiddle: return kMiddleStr;
case stdAc::swingh_t::kRight: return kRightStr;
case stdAc::swingh_t::kRightMax: return kRightMaxStr;
case stdAc::swingh_t::kWide: return kWideStr;
default: return kUnknownStr;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/IRac.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class IRac {
static stdAc::swingh_t strToSwingH(
const char *str, const stdAc::swingh_t def = stdAc::swingh_t::kOff);
static String boolToString(const bool value);
static String opmodeToString(const stdAc::opmode_t mode);
static String opmodeToString(const stdAc::opmode_t mode,
const bool ha = false);
static String fanspeedToString(const stdAc::fanspeed_t speed);
static String swingvToString(const stdAc::swingv_t swingv);
static String swinghToString(const stdAc::swingh_t swingh);
Expand Down
4 changes: 4 additions & 0 deletions test/IRac_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,10 @@ TEST(TestIRac, opmodeToString) {
EXPECT_EQ("Auto", IRac::opmodeToString(stdAc::opmode_t::kAuto));
EXPECT_EQ("Cool", IRac::opmodeToString(stdAc::opmode_t::kCool));
EXPECT_EQ("UNKNOWN", IRac::opmodeToString((stdAc::opmode_t)500));
// Home Assistant/Google Home differences.
EXPECT_EQ("Fan", IRac::opmodeToString(stdAc::opmode_t::kFan, false));
EXPECT_EQ("fan-only", IRac::opmodeToString(stdAc::opmode_t::kFan, true));
EXPECT_EQ("Fan", IRac::opmodeToString(stdAc::opmode_t::kFan)); // Default
}

TEST(TestIRac, fanspeedToString) {
Expand Down