Skip to content

Commit

Permalink
Merge pull request #80 from atanasenko/presets
Browse files Browse the repository at this point in the history
WindFree and other presets
  • Loading branch information
lanwin authored Feb 7, 2024
2 parents 21a5710 + f680a47 commit d5d1634
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 103 deletions.
166 changes: 166 additions & 0 deletions components/samsung_ac/converions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include "conversions.h"

namespace esphome
{
namespace samsung_ac
{
Mode str_to_mode(const std::string &value)
{
if (value == "Auto")
return Mode::Auto;
if (value == "Cool")
return Mode::Cool;
if (value == "Dry")
return Mode::Dry;
if (value == "Fan")
return Mode::Fan;
if (value == "Heat")
return Mode::Heat;
return Mode::Unknown;
}

std::string mode_to_str(Mode mode)
{
switch (mode)
{
case Mode::Auto:
return "Auto";
case Mode::Cool:
return "Cool";
case Mode::Dry:
return "Dry";
case Mode::Fan:
return "Fan";
case Mode::Heat:
return "Heat";
default:
return "";
};
}

optional<climate::ClimateMode> mode_to_climatemode(Mode mode)
{
switch (mode)
{
case Mode::Auto:
return climate::ClimateMode::CLIMATE_MODE_AUTO;
case Mode::Cool:
return climate::ClimateMode::CLIMATE_MODE_COOL;
case Mode::Dry:
return climate::ClimateMode::CLIMATE_MODE_DRY;
case Mode::Fan:
return climate::ClimateMode::CLIMATE_MODE_FAN_ONLY;
case Mode::Heat:
return climate::ClimateMode::CLIMATE_MODE_HEAT;
default:
return nullopt;
}
}

Mode climatemode_to_mode(climate::ClimateMode mode)
{
switch (mode)
{
case climate::ClimateMode::CLIMATE_MODE_COOL:
return Mode::Cool;
case climate::ClimateMode::CLIMATE_MODE_HEAT:
return Mode::Heat;
case climate::ClimateMode::CLIMATE_MODE_FAN_ONLY:
return Mode::Fan;
case climate::ClimateMode::CLIMATE_MODE_DRY:
return Mode::Dry;
case climate::ClimateMode::CLIMATE_MODE_AUTO:
return Mode::Auto;
default:
return Mode::Unknown;
}
}

climate::ClimateFanMode fanmode_to_climatefanmode(FanMode fanmode)
{
switch (fanmode)
{
case FanMode::Low:
return climate::ClimateFanMode::CLIMATE_FAN_LOW;
case FanMode::Mid:
return climate::ClimateFanMode::CLIMATE_FAN_MIDDLE;
case FanMode::Hight:
return climate::ClimateFanMode::CLIMATE_FAN_HIGH;
default:
case FanMode::Auto:
return climate::ClimateFanMode::CLIMATE_FAN_AUTO;
}
}

FanMode climatefanmode_to_fanmode(climate::ClimateFanMode fanmode)
{
switch (fanmode)
{
case climate::ClimateFanMode::CLIMATE_FAN_LOW:
return FanMode::Low;
case climate::ClimateFanMode::CLIMATE_FAN_MIDDLE:
return FanMode::Mid;
case climate::ClimateFanMode::CLIMATE_FAN_HIGH:
return FanMode::Hight;
case climate::ClimateFanMode::CLIMATE_FAN_AUTO:
default:
return FanMode::Auto;
}
}

AltMode preset_to_altmode(climate::ClimatePreset preset)
{
switch (preset)
{
case climate::ClimatePreset::CLIMATE_PRESET_SLEEP:
return AltMode::Sleep;
case climate::ClimatePreset::CLIMATE_PRESET_NONE:
default:
return AltMode::None;
}
}

AltMode custompreset_to_altmode(const std::string &value)
{
if (value == "Quiet")
return AltMode::Quiet;
if (value == "Fast")
return AltMode::Fast;
if (value == "Long Reach")
return AltMode::LongReach;
if (value == "WindFree")
return AltMode::Windfree;
return AltMode::Unknown;
}

optional<climate::ClimatePreset> altmode_to_preset(AltMode mode)
{
switch (mode)
{
case AltMode::None:
return climate::ClimatePreset::CLIMATE_PRESET_NONE;
case AltMode::Sleep:
return climate::ClimatePreset::CLIMATE_PRESET_SLEEP;
default:
return nullopt;
};
}

std::string altmode_to_custompreset(AltMode mode)
{
switch (mode)
{
case AltMode::Quiet:
return "Quiet";
case AltMode::Fast:
return "Fast";
case AltMode::LongReach:
return "Long Reach";
case AltMode::Windfree:
return "WindFree";
default:
return "";
};
}
} // namespace samsung_ac
} // namespace esphome
26 changes: 26 additions & 0 deletions components/samsung_ac/conversions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <optional>
#include "esphome/components/climate/climate.h"
#include "protocol.h"

namespace esphome
{
namespace samsung_ac{

Mode str_to_mode(const std::string &value);
std::string mode_to_str(Mode mode);

optional<climate::ClimateMode> mode_to_climatemode(Mode mode);
Mode climatemode_to_mode(climate::ClimateMode mode);

climate::ClimateFanMode fanmode_to_climatefanmode(FanMode fanmode);
FanMode climatefanmode_to_fanmode(climate::ClimateFanMode fanmode);

optional<climate::ClimatePreset> altmode_to_preset(AltMode mode);
std::string altmode_to_custompreset(AltMode mode);
AltMode preset_to_altmode(climate::ClimatePreset preset);
AltMode custompreset_to_altmode(const std::string &value);

} // namespace samsung_ac
} // namespace esphome
14 changes: 14 additions & 0 deletions components/samsung_ac/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ namespace esphome
Off = 5
};


enum class AltMode
{
Unknown = -1,
None = 0,
Sleep = 1,
Quiet = 2,
Fast = 3,
LongReach = 4,
Windfree = 5
};

class MessageTarget
{
public:
Expand All @@ -53,6 +65,7 @@ namespace esphome
virtual void set_target_temperature(const std::string address, float value) = 0;
virtual void set_mode(const std::string address, Mode mode) = 0;
virtual void set_fanmode(const std::string address, FanMode fanmode) = 0;
virtual void set_altmode(const std::string address, AltMode fanmode) = 0;
};

class Protocol
Expand All @@ -62,6 +75,7 @@ namespace esphome
virtual void publish_target_temp_message(MessageTarget *target, const std::string &address, float value) = 0;
virtual void publish_mode_message(MessageTarget *target, const std::string &address, Mode value) = 0;
virtual void publish_fanmode_message(MessageTarget *target, const std::string &address, FanMode value) = 0;
virtual void publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value) = 0;
};

enum class DataResult
Expand Down
55 changes: 55 additions & 0 deletions components/samsung_ac/protocol_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,34 @@ namespace esphome
target->publish_data(data);
}

int altmode_to_nasa_altmode(AltMode mode)
{
switch (mode)
{
case AltMode::Sleep:
return 1;
case AltMode::Quiet:
return 2;
case AltMode::Fast:
return 3;
case AltMode::LongReach:
return 6;
case AltMode::Windfree:
return 9;
case AltMode::None:
default:
return 0;
}
}

void NasaProtocol::publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value)
{
auto packet = Packet::create(Address::parse(address), DataType::Request, MessageNumber::ENUM_in_alt_mode, altmode_to_nasa_altmode(value));
ESP_LOGW(TAG, "publish_altmode_message %s", packet.to_string().c_str());
auto data = packet.encode();
target->publish_data(data);
}

Mode operation_mode_to_mode(int value)
{
switch (value)
Expand Down Expand Up @@ -446,6 +474,27 @@ namespace esphome
}
}

AltMode altmode_to_nasa_altmode(int value)
{
switch (value)
{
case 0:
return AltMode::None;
case 1:
return AltMode::Sleep;
case 2:
return AltMode::Quiet;
case 3:
return AltMode::Fast;
case 6:
return AltMode::LongReach;
case 9:
return AltMode::Windfree;
default:
return AltMode::Unknown;
}
}

void process_messageset(std::string source, std::string dest, MessageSet &message, MessageTarget *target)
{
if (debug_mqtt_connected())
Expand Down Expand Up @@ -520,6 +569,12 @@ namespace esphome
ESP_LOGW(TAG, "s:%s d:%s ENUM_in_fan_mode_real %li", source.c_str(), dest.c_str(), message.value);
return;
}
case MessageNumber::ENUM_in_alt_mode:
{
ESP_LOGW(TAG, "s:%s d:%s ENUM_in_alt_mode %li", source.c_str(), dest.c_str(), message.value);
target->set_altmode(source, altmode_to_nasa_altmode(message.value));
return;
}
default:
{
// Test stuff
Expand Down
2 changes: 2 additions & 0 deletions components/samsung_ac/protocol_nasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace esphome
ENUM_in_operation_mode = 0x4001,
ENUM_in_fan_mode = 0x4006, // Did not exists in xml...only in Remocon.dll code
ENUM_in_fan_mode_real = 0x4007,
ENUM_in_alt_mode = 0x4060,
ENUM_in_state_humidity_percent = 0x4038,
VAR_in_temp_room_f = 0x4203,
VAR_in_temp_target_f = 0x4201,
Expand Down Expand Up @@ -168,6 +169,7 @@ namespace esphome
void publish_target_temp_message(MessageTarget *target, const std::string &address, float value) override;
void publish_mode_message(MessageTarget *target, const std::string &address, Mode value) override;
void publish_fanmode_message(MessageTarget *target, const std::string &address, FanMode value) override;
void publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value) override;
};

} // namespace samsung_ac
Expand Down
7 changes: 7 additions & 0 deletions components/samsung_ac/protocol_non_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ namespace esphome
nonnasa_requests.push(request);
}

void NonNasaProtocol::publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value)
{
// TODO
}

Mode nonnasa_mode_to_mode(NonNasaMode value)
{
switch (value)
Expand Down Expand Up @@ -380,6 +385,8 @@ namespace esphome
target->set_power(nonpacket_.src, nonpacket_.command20.power);
target->set_mode(nonpacket_.src, nonnasa_mode_to_mode(nonpacket_.command20.mode));
target->set_fanmode(nonpacket_.src, nonnasa_fanspeed_to_fanmode(nonpacket_.command20.fanspeed));
// TODO
target->set_altmode(nonpacket_.src, AltMode::None);
}
else if (nonpacket_.cmd == NonNasaCommand::CmdF8)
{
Expand Down
1 change: 1 addition & 0 deletions components/samsung_ac/protocol_non_nasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ namespace esphome
void publish_target_temp_message(MessageTarget *target, const std::string &address, float value) override;
void publish_mode_message(MessageTarget *target, const std::string &address, Mode value) override;
void publish_fanmode_message(MessageTarget *target, const std::string &address, FanMode value) override;
void publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value) override;
};
} // namespace samsung_ac
} // namespace esphome
7 changes: 7 additions & 0 deletions components/samsung_ac/samsung_ac.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ namespace esphome
dev->publish_fanmode(fanmode);
}

void /*MessageTarget::*/ set_altmode(const std::string address, AltMode altmode) override
{
Samsung_AC_Device *dev = find_device(address);
if (dev != nullptr)
dev->publish_altmode(altmode);
}

protected:
Samsung_AC_Device *find_device(const std::string address)
{
Expand Down
Loading

0 comments on commit d5d1634

Please sign in to comment.