Skip to content

Commit

Permalink
Refactor to use one request object (#84)
Browse files Browse the repository at this point in the history
* test

* Tests for ProtocolRequest

* Min

* use esphome optioanl and fake it for tests

* Start impl it

* Add test impl for non nasa code

* Add swingmode to ProtocolRequest

* Add alt_mode to ProtocolRequest

* Add fan_mode to ProtocolRequest

* Add target temp to ProtocolRequest

* Impl ProtocolRequest for non Nasa

* Remove oldstuff

* Fix tests

* Fix device access

* Fix publishing
  • Loading branch information
lanwin authored Feb 8, 2024
1 parent af9304b commit 2b0330c
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 161 deletions.
1 change: 0 additions & 1 deletion components/samsung_ac/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ namespace esphome

climate::ClimateSwingMode swingmode_to_climateswingmode(SwingMode swingMode);
SwingMode climateswingmode_to_swingmode(climate::ClimateSwingMode swingMode);

} // namespace samsung_ac
} // namespace esphome
23 changes: 14 additions & 9 deletions components/samsung_ac/protocol.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <vector>
#include <iostream>
#include "esphome/core/optional.h"
#include "util.h"

namespace esphome
{
Expand Down Expand Up @@ -41,7 +41,6 @@ namespace esphome
Off = 5
};


enum class AltMode
{
Unknown = -1,
Expand Down Expand Up @@ -78,15 +77,21 @@ namespace esphome
virtual void set_swing_horizontal(const std::string address, bool horizontal) = 0;
};

struct ProtocolRequest
{
public:
optional<bool> power;
optional<Mode> mode;
optional<float> target_temp;
optional<FanMode> fan_mode;
optional<SwingMode> swing_mode;
optional<AltMode> alt_mode;
};

class Protocol
{
public:
virtual void publish_power_message(MessageTarget *target, const std::string &address, bool value) = 0;
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;
virtual void publish_swing_mode_message(MessageTarget *target, const std::string &address, SwingMode value) = 0;
virtual void publish_request(MessageTarget *target, const std::string &address, ProtocolRequest &request) = 0;
};

enum class DataResult
Expand Down
100 changes: 55 additions & 45 deletions components/samsung_ac/protocol_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,30 +347,6 @@ namespace esphome
return str;
}

void NasaProtocol::publish_power_message(MessageTarget *target, const std::string &address, bool value)
{
auto packet = Packet::create(Address::parse(address), DataType::Request, MessageNumber::ENUM_in_operation_power, value ? 1 : 0);
auto data = packet.encode();
target->publish_data(data);
}

void NasaProtocol::publish_target_temp_message(MessageTarget *target, const std::string &address, float value)
{
auto packet = Packet::create(Address::parse(address), DataType::Request, MessageNumber::VAR_in_temp_target_f, value * 10.0);
auto data = packet.encode();
target->publish_data(data);
}

void NasaProtocol::publish_mode_message(MessageTarget *target, const std::string &address, Mode value)
{
auto packet = Packet::create(Address::parse(address), DataType::Request, MessageNumber::ENUM_in_operation_mode, (int)value);
MessageSet power(MessageNumber::ENUM_in_operation_power);
power.value = 1;
packet.messages.push_back(power);
auto data = packet.encode();
target->publish_data(data);
}

int fanmode_to_nasa_fanmode(FanMode mode)
{
// This stuff did not exists in XML only in Remcode.dll
Expand All @@ -390,14 +366,6 @@ namespace esphome
}
}

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

int altmode_to_nasa_altmode(AltMode mode)
{
switch (mode)
Expand All @@ -418,22 +386,64 @@ namespace esphome
}
}

void NasaProtocol::publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value)
void NasaProtocol::publish_request(MessageTarget *target, const std::string &address, ProtocolRequest &request)
{
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);
}
Packet packet = Packet::createa_partial(Address::parse(address), DataType::Request);

void NasaProtocol::publish_swing_mode_message(MessageTarget *target, const std::string &address, SwingMode value)
{
auto packet = Packet::create(Address::parse(address), DataType::Request, MessageNumber::ENUM_in_louver_hl_swing, static_cast<uint8_t>(value) & 1);
ESP_LOGW(TAG, "publish_swing_mode_message %s", packet.to_string().c_str());
if (request.mode)
{
request.power = true; // ensure system turns on when mode is set

MessageSet mode(MessageNumber::ENUM_in_operation_mode);
mode.value = (int)request.mode.value();
packet.messages.push_back(mode);
}

if (request.power)
{
MessageSet power(MessageNumber::ENUM_in_operation_power);
power.value = request.power.value() ? 1 : 0;
packet.messages.push_back(power);
}

if (request.target_temp)
{
MessageSet targettemp(MessageNumber::VAR_in_temp_target_f);
targettemp.value = request.target_temp.value() * 10.0;
packet.messages.push_back(targettemp);
}

if (request.fan_mode)
{
MessageSet fanmode(MessageNumber::ENUM_in_fan_mode);
fanmode.value = fanmode_to_nasa_fanmode(request.fan_mode.value());
packet.messages.push_back(fanmode);
}

MessageSet lr_swing(MessageNumber::ENUM_in_louver_lr_swing);
lr_swing.value = (static_cast<uint8_t>(value) >> 1) & 1;
packet.messages.push_back(lr_swing);
if (request.alt_mode)
{
MessageSet altmode(MessageNumber::ENUM_in_alt_mode);
altmode.value = altmode_to_nasa_altmode(request.alt_mode.value());
packet.messages.push_back(altmode);
}

if (request.swing_mode)
{
MessageSet hl_swing(MessageNumber::ENUM_in_louver_hl_swing);
hl_swing.value = static_cast<uint8_t>(request.swing_mode.value()) & 1;
packet.messages.push_back(hl_swing);

MessageSet lr_swing(MessageNumber::ENUM_in_louver_lr_swing);
lr_swing.value = (static_cast<uint8_t>(request.swing_mode.value()) >> 1) & 1;
packet.messages.push_back(lr_swing);
}

if (packet.messages.size() == 0)
return;

ESP_LOGW(TAG, "publish packet %s", packet.to_string().c_str());

out.push_back(packet);

auto data = packet.encode();
target->publish_data(data);
Expand Down
7 changes: 1 addition & 6 deletions components/samsung_ac/protocol_nasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,7 @@ namespace esphome
public:
NasaProtocol() = default;

void publish_power_message(MessageTarget *target, const std::string &address, bool value) override;
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;
void publish_swing_mode_message(MessageTarget *target, const std::string &address, SwingMode value) override;
void publish_request(MessageTarget *target, const std::string &address, ProtocolRequest &request) override;
};

} // namespace samsung_ac
Expand Down
61 changes: 27 additions & 34 deletions components/samsung_ac/protocol_non_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,6 @@ namespace esphome

std::queue<NonNasaRequest> nonnasa_requests;

void NonNasaProtocol::publish_power_message(MessageTarget *target, const std::string &address, bool value)
{
auto request = NonNasaRequest::create(address);
request.power = value;
nonnasa_requests.push(request);
}

void NonNasaProtocol::publish_target_temp_message(MessageTarget *target, const std::string &address, float value)
{
auto request = NonNasaRequest::create(address);
request.target_temp = value;
nonnasa_requests.push(request);
}

NonNasaMode mode_to_nonnasa_mode(Mode value)
{
switch (value)
Expand All @@ -392,14 +378,6 @@ namespace esphome
}
}

void NonNasaProtocol::publish_mode_message(MessageTarget *target, const std::string &address, Mode value)
{
auto request = NonNasaRequest::create(address);
request.power = true;
request.mode = mode_to_nonnasa_mode(value);
nonnasa_requests.push(request);
}

NonNasaFanspeed fanmode_to_nonnasa_fanspeed(FanMode value)
{
switch (value)
Expand All @@ -416,21 +394,36 @@ namespace esphome
}
}

void NonNasaProtocol::publish_fanmode_message(MessageTarget *target, const std::string &address, FanMode value)
void NonNasaProtocol::publish_request(MessageTarget *target, const std::string &address, ProtocolRequest &request)
{
auto request = NonNasaRequest::create(address);
request.fanspeed = fanmode_to_nonnasa_fanspeed(value);
nonnasa_requests.push(request);
}
auto req = NonNasaRequest::create(address);

void NonNasaProtocol::publish_altmode_message(MessageTarget *target, const std::string &address, AltMode value)
{
ESP_LOGW(TAG, "change altmode is currently not implemented");
}
if (request.mode)
{
request.power = true; // ensure system turns on when mode is set
req.mode = mode_to_nonnasa_mode(request.mode.value());
}

void NonNasaProtocol::publish_swing_mode_message(MessageTarget *target, const std::string &address, SwingMode value)
{
ESP_LOGW(TAG, "change swingmode is currently not implemented");
if (request.power)
req.power = request.power.value();

if (request.target_temp)
req.target_temp = request.target_temp.value();

if (request.fan_mode)
req.fanspeed = fanmode_to_nonnasa_fanspeed(request.fan_mode.value());

if (request.alt_mode)
{
ESP_LOGW(TAG, "change altmode is currently not implemented");
}

if (request.swing_mode)
{
ESP_LOGW(TAG, "change swingmode is currently not implemented");
}

nonnasa_requests.push(req);
}

Mode nonnasa_mode_to_mode(NonNasaMode value)
Expand Down
10 changes: 2 additions & 8 deletions components/samsung_ac/protocol_non_nasa.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ namespace esphome
std::string to_string();
};


struct NonNasaCommandF3 // from outdoor unit
{
uint8_t inverter_max_frequency_hz = 0;
Expand Down Expand Up @@ -141,7 +140,7 @@ namespace esphome
CmdC6 = 0xc6,
CmdF0 = 0xf0,
CmdF1 = 0xf1,
CmdF3 = 0xf3,
CmdF3 = 0xf3,
CmdF8 = 0xF8,
};

Expand Down Expand Up @@ -197,12 +196,7 @@ namespace esphome
public:
NonNasaProtocol() = default;

void publish_power_message(MessageTarget *target, const std::string &address, bool value) override;
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;
void publish_swing_mode_message(MessageTarget *target, const std::string &address, SwingMode value) override;
void publish_request(MessageTarget *target, const std::string &address, ProtocolRequest &request) override;
};
} // namespace samsung_ac
} // namespace esphome
Loading

0 comments on commit 2b0330c

Please sign in to comment.