Skip to content

Commit

Permalink
Simplfy custom_sensor - its simpler to just call set_custom_sensor an…
Browse files Browse the repository at this point in the history
…d the code does nothing if this sensor did not exists.
  • Loading branch information
lanwin committed Oct 9, 2024
1 parent 1917619 commit c028b2d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 163 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@
"future": "cpp",
"mutex": "cpp",
"thread": "cpp",
"variant": "cpp"
"variant": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"forward_list": "cpp",
"iomanip": "cpp",
"ranges": "cpp",
"span": "cpp",
"stop_token": "cpp"
},
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
Expand Down
1 change: 0 additions & 1 deletion components/samsung_ac/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ namespace esphome
virtual void set_altmode(const std::string address, AltMode altmode) = 0;
virtual void set_swing_vertical(const std::string address, bool vertical) = 0;
virtual void set_swing_horizontal(const std::string address, bool horizontal) = 0;
virtual optional<std::set<uint16_t>> get_custom_sensors(const std::string address) = 0;
virtual void set_custom_sensor(const std::string address, uint16_t message_number, float value) = 0;
virtual void set_error_code(const std::string address, int error_code) = 0;
};
Expand Down
10 changes: 3 additions & 7 deletions components/samsung_ac/protocol_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace esphome
}
}

void process_messageset(std::string source, std::string dest, MessageSet &message, optional<std::set<uint16_t>> &custom, MessageTarget *target)
void process_messageset(std::string source, std::string dest, MessageSet &message, MessageTarget *target)
{
if (debug_mqtt_connected())
{
Expand Down Expand Up @@ -573,10 +573,7 @@ namespace esphome
}
}

if (custom && custom.value().find((uint16_t)message.messageNumber) != custom.value().end())
{
target->set_custom_sensor(source, (uint16_t)message.messageNumber, (float)message.value);
}
target->set_custom_sensor(source, (uint16_t)message.messageNumber, (float)message.value);

switch (message.messageNumber)
{
Expand Down Expand Up @@ -849,10 +846,9 @@ namespace esphome
if (packet_.command.dataType != DataType::Notification)
return;

optional<std::set<uint16_t>> custom = target->get_custom_sensors(source);
for (auto &message : packet_.messages)
{
process_messageset(source, dest, message, custom, target);
process_messageset(source, dest, message, target);
}
}

Expand Down
8 changes: 0 additions & 8 deletions components/samsung_ac/samsung_ac.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ namespace esphome
dev->update_swing_horizontal(horizontal);
}

optional<std::set<uint16_t>> /*MessageTarget::*/ get_custom_sensors(const std::string address) override
{
Samsung_AC_Device *dev = find_device(address);
if (dev != nullptr)
return optional<std::set<uint16_t>>(dev->get_custom_sensors());
return optional<std::set<uint16_t>>();
}

void /*MessageTarget::*/ set_custom_sensor(const std::string address, uint16_t message_number, float value) override
{
Samsung_AC_Device *dev = find_device(address);
Expand Down
8 changes: 0 additions & 8 deletions components/samsung_ac/samsung_ac_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,6 @@ namespace esphome
custom_sensors.push_back(std::move(cust_sensor));
}

std::set<uint16_t> get_custom_sensors()
{
std::set<uint16_t> numbers;
for (auto &sensor : custom_sensors)
numbers.insert(sensor.message_number);
return numbers;
}

void set_power_switch(Samsung_AC_Switch *switch_)
{
power = switch_;
Expand Down
269 changes: 131 additions & 138 deletions test/test_stuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,142 +76,135 @@ class DebugTarget : public MessageTarget
cout << "> " << address << " set_outdoor_temperature=" << to_string(value) << endl;
last_set_outdoor_temperature_address = address;
last_set_outdoor_temperature_value = value;

std::string last_set_target_water_temperature_address;
float last_set_target_water_temperature_value;
void set_target_water_temperature(const std::string address, float value)
{
cout << "> " << address << " set_target_water_temperature=" << to_string(value) << endl;
last_set_target_water_temperature_address = address;
last_set_target_water_temperature_value = value;
}

std::string last_set_room_humidity_address;
float last_set_room_humidity_value;
void set_room_humidity(const std::string address, float value)
{
cout << "> " << address << " set_room_humidity=" << to_string(value) << endl;
last_set_room_humidity_address = address;
last_set_room_humidity_value = value;
}

std::string last_set_mode_address;
Mode last_set_mode_mode;
void set_mode(const std::string address, Mode mode)
{
cout << "> " << address << " set_mode=" << to_string((int)mode) << endl;
last_set_mode_address = address;
last_set_mode_mode = mode;
}

std::string last_set_fanmode_address;
FanMode last_set_fanmode_mode;
void set_fanmode(const std::string address, FanMode fanmode)
{
cout << "> " << address << " set_fanmode=" << to_string((int)fanmode) << endl;
last_set_fanmode_address = address;
last_set_fanmode_mode = fanmode;
}

void set_altmode(const std::string address, AltMode altmode)
{
cout << "> " << address << " set_altmode=" << to_string((int)altmode) << endl;
}

void set_swing_vertical(const std::string address, bool vertical)
{
cout << "> " << address << " set_swing_vertical=" << to_string((int)vertical) << endl;
}

void set_swing_horizontal(const std::string address, bool horizontal)
{
cout << "> " << address << " set_swing_horizontal=" << to_string((int)horizontal) << endl;
}

std::set<uint16_t> last_custom_sensors;

esphome::optional<std::set<uint16_t>> get_custom_sensors(const std::string address)
{
return last_custom_sensors;
}

void set_custom_sensor(const std::string address, uint16_t message_number, float value)
{
last_custom_sensors.insert(message_number);
}

void assert_only_address(const std::string address)
{
assert(last_register_address == address);
assert(last_set_power_address == "");
assert(last_set_room_temperature_address == "");
assert(last_set_target_temperature_address == "");
assert(last_set_mode_address == "");
assert(last_set_fanmode_address == "");
}

void assert_values(const std::string address, bool power, float room_temp, float target_temp, Mode mode, FanMode fanmode)
{
assert(last_register_address == address);

assert(last_set_power_address == address);
assert(last_set_power_value == power);

assert(last_set_room_temperature_address == address);
assert(last_set_room_temperature_value == room_temp);

assert(last_set_target_temperature_address == address);
assert(last_set_target_temperature_value == target_temp);

assert(last_set_mode_address == address);
assert(last_set_mode_mode == mode);

assert(last_set_fanmode_address == address);
assert(last_set_fanmode_mode == fanmode);
}

void assert_values(const std::string address, bool power, float room_temp, float target_temp, Mode mode, FanMode fanmode, float humidity)
{
assert_values(address, power, room_temp, target_temp, mode, fanmode);

assert(last_set_room_humidity_address == address);
assert(last_set_room_humidity_value == humidity);
}
};

void test_process_data(const std::string &hex, DebugTarget &target)
{
cout << "test: " << hex << std::endl;
auto bytes = hex_to_bytes(hex);
assert(process_data(bytes, &target) == DataResult::Clear);
}

DebugTarget test_process_data(const std::string &hex)
{
DebugTarget target;
test_process_data(hex, target);
return target;
}

void assert_str(const std::string actual, const std::string expected)
{
if (actual != expected)
{
cout << "actual: " << actual << std::endl;
cout << "expected: " << expected << std::endl;
}
assert(actual == expected);
}

namespace esphome
{
uint32_t millis()
{
return 0;
}
uint32_t micros()
{
return 0;
}
void delay(uint32_t ms) {}
} // namespace esphome
std::string last_set_target_water_temperature_address;
float last_set_target_water_temperature_value;
void set_target_water_temperature(const std::string address, float value)
{
cout << "> " << address << " set_target_water_temperature=" << to_string(value) << endl;
last_set_target_water_temperature_address = address;
last_set_target_water_temperature_value = value;
}

std::string last_set_room_humidity_address;
float last_set_room_humidity_value;
void set_room_humidity(const std::string address, float value)
{
cout << "> " << address << " set_room_humidity=" << to_string(value) << endl;
last_set_room_humidity_address = address;
last_set_room_humidity_value = value;
}

std::string last_set_mode_address;
Mode last_set_mode_mode;
void set_mode(const std::string address, Mode mode)
{
cout << "> " << address << " set_mode=" << to_string((int)mode) << endl;
last_set_mode_address = address;
last_set_mode_mode = mode;
}

std::string last_set_fanmode_address;
FanMode last_set_fanmode_mode;
void set_fanmode(const std::string address, FanMode fanmode)
{
cout << "> " << address << " set_fanmode=" << to_string((int)fanmode) << endl;
last_set_fanmode_address = address;
last_set_fanmode_mode = fanmode;
}

void set_altmode(const std::string address, AltMode altmode)
{
cout << "> " << address << " set_altmode=" << to_string((int)altmode) << endl;
}

void set_swing_vertical(const std::string address, bool vertical)
{
cout << "> " << address << " set_swing_vertical=" << to_string((int)vertical) << endl;
}

void set_swing_horizontal(const std::string address, bool horizontal)
{
cout << "> " << address << " set_swing_horizontal=" << to_string((int)horizontal) << endl;
}

void set_custom_sensor(const std::string address, uint16_t message_number, float value)
{
last_custom_sensors.insert(message_number);
}

void assert_only_address(const std::string address)
{
assert(last_register_address == address);
assert(last_set_power_address == "");
assert(last_set_room_temperature_address == "");
assert(last_set_target_temperature_address == "");
assert(last_set_mode_address == "");
assert(last_set_fanmode_address == "");
}

void assert_values(const std::string address, bool power, float room_temp, float target_temp, Mode mode, FanMode fanmode)
{
assert(last_register_address == address);

assert(last_set_power_address == address);
assert(last_set_power_value == power);

assert(last_set_room_temperature_address == address);
assert(last_set_room_temperature_value == room_temp);

assert(last_set_target_temperature_address == address);
assert(last_set_target_temperature_value == target_temp);

assert(last_set_mode_address == address);
assert(last_set_mode_mode == mode);

assert(last_set_fanmode_address == address);
assert(last_set_fanmode_mode == fanmode);
}

void assert_values(const std::string address, bool power, float room_temp, float target_temp, Mode mode, FanMode fanmode, float humidity)
{
assert_values(address, power, room_temp, target_temp, mode, fanmode);

assert(last_set_room_humidity_address == address);
assert(last_set_room_humidity_value == humidity);
}
};

void test_process_data(const std::string &hex, DebugTarget &target)
{
cout << "test: " << hex << std::endl;
auto bytes = hex_to_bytes(hex);
assert(process_data(bytes, &target) == DataResult::Clear);
}

DebugTarget test_process_data(const std::string &hex)
{
DebugTarget target;
test_process_data(hex, target);
return target;
}

void assert_str(const std::string actual, const std::string expected)
{
if (actual != expected)
{
cout << "actual: " << actual << std::endl;
cout << "expected: " << expected << std::endl;
}
assert(actual == expected);
}

namespace esphome
{
uint32_t millis()
{
return 0;
}
uint32_t micros()
{
return 0;
}
void delay(uint32_t ms) {}
} // namespace esphome

0 comments on commit c028b2d

Please sign in to comment.