From c028b2d26882ae6551c6be7488a97aa24554497d Mon Sep 17 00:00:00 2001 From: Steve Wagner Date: Wed, 9 Oct 2024 18:13:18 +0200 Subject: [PATCH] Simplfy custom_sensor - its simpler to just call set_custom_sensor and the code does nothing if this sensor did not exists. --- .vscode/settings.json | 9 +- components/samsung_ac/protocol.h | 1 - components/samsung_ac/protocol_nasa.cpp | 10 +- components/samsung_ac/samsung_ac.h | 8 - components/samsung_ac/samsung_ac_device.h | 8 - test/test_stuff.h | 269 +++++++++++----------- 6 files changed, 142 insertions(+), 163 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 467e1ae9..55851a94 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" diff --git a/components/samsung_ac/protocol.h b/components/samsung_ac/protocol.h index b7e0bfc4..1c951c19 100644 --- a/components/samsung_ac/protocol.h +++ b/components/samsung_ac/protocol.h @@ -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> 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; }; diff --git a/components/samsung_ac/protocol_nasa.cpp b/components/samsung_ac/protocol_nasa.cpp index 6baa9198..5faab74f 100644 --- a/components/samsung_ac/protocol_nasa.cpp +++ b/components/samsung_ac/protocol_nasa.cpp @@ -538,7 +538,7 @@ namespace esphome } } - void process_messageset(std::string source, std::string dest, MessageSet &message, optional> &custom, MessageTarget *target) + void process_messageset(std::string source, std::string dest, MessageSet &message, MessageTarget *target) { if (debug_mqtt_connected()) { @@ -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) { @@ -849,10 +846,9 @@ namespace esphome if (packet_.command.dataType != DataType::Notification) return; - optional> custom = target->get_custom_sensors(source); for (auto &message : packet_.messages) { - process_messageset(source, dest, message, custom, target); + process_messageset(source, dest, message, target); } } diff --git a/components/samsung_ac/samsung_ac.h b/components/samsung_ac/samsung_ac.h index 2919487e..2891bb77 100644 --- a/components/samsung_ac/samsung_ac.h +++ b/components/samsung_ac/samsung_ac.h @@ -180,14 +180,6 @@ namespace esphome dev->update_swing_horizontal(horizontal); } - optional> /*MessageTarget::*/ get_custom_sensors(const std::string address) override - { - Samsung_AC_Device *dev = find_device(address); - if (dev != nullptr) - return optional>(dev->get_custom_sensors()); - return optional>(); - } - void /*MessageTarget::*/ set_custom_sensor(const std::string address, uint16_t message_number, float value) override { Samsung_AC_Device *dev = find_device(address); diff --git a/components/samsung_ac/samsung_ac_device.h b/components/samsung_ac/samsung_ac_device.h index e80c8e93..f18894c5 100644 --- a/components/samsung_ac/samsung_ac_device.h +++ b/components/samsung_ac/samsung_ac_device.h @@ -153,14 +153,6 @@ namespace esphome custom_sensors.push_back(std::move(cust_sensor)); } - std::set get_custom_sensors() - { - std::set numbers; - for (auto &sensor : custom_sensors) - numbers.insert(sensor.message_number); - return numbers; - } - void set_power_switch(Samsung_AC_Switch *switch_) { power = switch_; diff --git a/test/test_stuff.h b/test/test_stuff.h index 66b069bc..d74607d7 100644 --- a/test/test_stuff.h +++ b/test/test_stuff.h @@ -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 last_custom_sensors; - - esphome::optional> 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