From 7183d552ae2686f2ccd73e54cd6cba4b743b9761 Mon Sep 17 00:00:00 2001 From: Anton Tanasenko Date: Tue, 13 Feb 2024 15:25:48 +0200 Subject: [PATCH] Move nasa-specific sensors into python config --- components/samsung_ac/__init__.py | 56 +++++++++++++---------- components/samsung_ac/protocol.h | 2 - components/samsung_ac/protocol_nasa.cpp | 7 +-- components/samsung_ac/samsung_ac.h | 14 ------ components/samsung_ac/samsung_ac_device.h | 29 ++---------- 5 files changed, 37 insertions(+), 71 deletions(-) diff --git a/components/samsung_ac/__init__.py b/components/samsung_ac/__init__.py index 862c99bd..9db631e3 100644 --- a/components/samsung_ac/__init__.py +++ b/components/samsung_ac/__init__.py @@ -51,9 +51,7 @@ CONF_DEVICE_ID = "samsung_ac_device_id" CONF_DEVICE_ADDRESS = "address" CONF_DEVICE_ROOM_TEMPERATURE = "room_temperature" -CONF_DEVICE_ROOM_HUMIDITY = "room_humidity" CONF_DEVICE_TARGET_TEMPERATURE = "target_temperature" -CONF_DEVICE_WATER_TEMPERATURE = "water_temperature" CONF_DEVICE_OUTDOOR_TEMPERATURE = "outdoor_temperature" CONF_DEVICE_POWER = "power" CONF_DEVICE_MODE = "mode" @@ -61,6 +59,10 @@ CONF_DEVICE_CUSTOM = "custom_sensor" CONF_RAW_FILTERS = "raw_filters" CONF_DEVICE_MESSAGE = "message" + +CONF_DEVICE_ROOM_HUMIDITY = "room_humidity" +CONF_DEVICE_WATER_TEMPERATURE = "water_temperature" + CUSTOM_SENSOR_SCHEMA = sensor.sensor_schema().extend({ cv.Required(CONF_DEVICE_MESSAGE): cv.hex_int, }) @@ -100,6 +102,15 @@ def temperature_sensor_schema(message: int): ], ) +def humidity_sensor_schema(message: int): + return custom_sensor_schema( + message=message, + unit_of_measurement=UNIT_PERCENT, + accuracy_decimals=0, + device_class=DEVICE_CLASS_HUMIDITY, + state_class=STATE_CLASS_MEASUREMENT, + ) + DEVICE_SCHEMA = ( cv.Schema( { @@ -111,33 +122,31 @@ def temperature_sensor_schema(message: int): device_class=DEVICE_CLASS_TEMPERATURE, state_class=STATE_CLASS_MEASUREMENT, ), - cv.Optional(CONF_DEVICE_WATER_TEMPERATURE): sensor.sensor_schema( - unit_of_measurement=UNIT_CELSIUS, - accuracy_decimals=1, - device_class=DEVICE_CLASS_TEMPERATURE, - state_class=STATE_CLASS_MEASUREMENT, - ), cv.Optional(CONF_DEVICE_OUTDOOR_TEMPERATURE): sensor.sensor_schema( unit_of_measurement=UNIT_CELSIUS, accuracy_decimals=1, device_class=DEVICE_CLASS_TEMPERATURE, state_class=STATE_CLASS_MEASUREMENT, ), - cv.Optional(CONF_DEVICE_ROOM_HUMIDITY): sensor.sensor_schema( - unit_of_measurement=UNIT_PERCENT, - accuracy_decimals=0, - device_class=DEVICE_CLASS_HUMIDITY, - state_class=STATE_CLASS_MEASUREMENT, - ), + cv.Optional(CONF_DEVICE_TARGET_TEMPERATURE): NUMBER_SCHEMA, cv.Optional(CONF_DEVICE_POWER): switch.switch_schema(Samsung_AC_Switch), cv.Optional(CONF_DEVICE_MODE): SELECT_MODE_SCHEMA, cv.Optional(CONF_DEVICE_CLIMATE): CLIMATE_SCHEMA, cv.Optional(CONF_DEVICE_CUSTOM, default=[]): cv.ensure_list(CUSTOM_SENSOR_SCHEMA), + + # keep CUSTOM_SENSOR_KEYS in sync with these + cv.Optional(CONF_DEVICE_WATER_TEMPERATURE): temperature_sensor_schema(0x4237), + cv.Optional(CONF_DEVICE_ROOM_HUMIDITY): humidity_sensor_schema(0x4038), } ) ) +CUSTOM_SENSOR_KEYS = [ + CONF_DEVICE_WATER_TEMPERATURE, + CONF_DEVICE_ROOM_HUMIDITY, +] + CONF_DEVICES = "devices" CONF_DEBUG_MQTT_HOST = "debug_mqtt_host" @@ -192,16 +201,6 @@ async def to_code(config): sens = await sensor.new_sensor(conf) cg.add(var_dev.set_outdoor_temperature_sensor(sens)) - if CONF_DEVICE_WATER_TEMPERATURE in device: - conf = device[CONF_DEVICE_WATER_TEMPERATURE] - sens = await sensor.new_sensor(conf) - cg.add(var_dev.set_water_temperature_sensor(sens)) - - if CONF_DEVICE_ROOM_HUMIDITY in device: - conf = device[CONF_DEVICE_ROOM_HUMIDITY] - sens = await sensor.new_sensor(conf) - cg.add(var_dev.set_room_humidity_sensor(sens)) - if CONF_DEVICE_TARGET_TEMPERATURE in device: conf = device[CONF_DEVICE_TARGET_TEMPERATURE] conf[CONF_UNIT_OF_MEASUREMENT] = UNIT_CELSIUS @@ -228,6 +227,15 @@ async def to_code(config): for cust_sens in device[CONF_DEVICE_CUSTOM]: sens = await sensor.new_sensor(cust_sens) cg.add(var_dev.add_custom_sensor(cust_sens[CONF_DEVICE_MESSAGE], sens)) + + for key in CUSTOM_SENSOR_KEYS: + if key in device: + conf = device[key] + # combine raw filters with any user-defined filters + conf_copy = conf.copy() + conf_copy[CONF_FILTERS] = (conf[CONF_RAW_FILTERS] if CONF_RAW_FILTERS in conf else []) + (conf[CONF_FILTERS] if CONF_FILTERS in conf else []) + sens = await sensor.new_sensor(conf_copy) + cg.add(var_dev.add_custom_sensor(conf[CONF_DEVICE_MESSAGE], sens)) cg.add(var.register_device(var_dev)) diff --git a/components/samsung_ac/protocol.h b/components/samsung_ac/protocol.h index 265d5e9c..f1d977ae 100644 --- a/components/samsung_ac/protocol.h +++ b/components/samsung_ac/protocol.h @@ -69,8 +69,6 @@ namespace esphome virtual void register_address(const std::string address) = 0; virtual void set_power(const std::string address, bool value) = 0; virtual void set_room_temperature(const std::string address, float value) = 0; - virtual void set_water_temperature(const std::string address, float value) = 0; - virtual void set_room_humidity(const std::string address, float value) = 0; virtual void set_target_temperature(const std::string address, float value) = 0; virtual void set_outdoor_temperature(const std::string address, float value) = 0; virtual void set_mode(const std::string address, Mode mode) = 0; diff --git a/components/samsung_ac/protocol_nasa.cpp b/components/samsung_ac/protocol_nasa.cpp index 9d9fe920..117a7149 100644 --- a/components/samsung_ac/protocol_nasa.cpp +++ b/components/samsung_ac/protocol_nasa.cpp @@ -566,7 +566,6 @@ namespace esphome { // XML Enum no value but in Code it adds unit ESP_LOGW(TAG, "s:%s d:%s ENUM_in_state_humidity_percent %li", source.c_str(), dest.c_str(), message.value); - target->set_room_humidity(source, message.value); return; } case MessageNumber::ENUM_in_operation_power: @@ -623,14 +622,12 @@ namespace esphome } case MessageNumber::VAR_in_temp_water_tank_f: { - double temp = (double)message.value / (double)10; - ESP_LOGW(TAG, "s:%s d:%s VAR_in_temp_water_tank_f %f", source.c_str(), dest.c_str(), temp); - target->set_water_temperature(source, temp); + ESP_LOGW(TAG, "s:%s d:%s VAR_in_temp_water_tank_f %f", source.c_str(), dest.c_str(), message.value); return; } case MessageNumber::VAR_out_sensor_airout: { - double temp = (double)message.value / (double)10; + double temp = (double) ((int16_t)message.value) / (double)10; ESP_LOGW(TAG, "s:%s d:%s VAR_out_sensor_airout %li", source.c_str(), dest.c_str(), message.value); target->set_outdoor_temperature(source, temp); return; diff --git a/components/samsung_ac/samsung_ac.h b/components/samsung_ac/samsung_ac.h index ceb3e1c3..a3baabd3 100644 --- a/components/samsung_ac/samsung_ac.h +++ b/components/samsung_ac/samsung_ac.h @@ -68,20 +68,6 @@ namespace esphome dev->update_room_temperature(value); } - void /*MessageTarget::*/ set_water_temperature(const std::string address, float value) override - { - Samsung_AC_Device *dev = find_device(address); - if (dev != nullptr) - dev->update_water_temperature(value); - } - - void /*MessageTarget::*/ set_room_humidity(const std::string address, float value) override - { - Samsung_AC_Device *dev = find_device(address); - if (dev != nullptr) - dev->update_room_humidity(value); - } - void /*MessageTarget::*/ set_outdoor_temperature(const std::string address, 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 37647504..6b84bb35 100644 --- a/components/samsung_ac/samsung_ac_device.h +++ b/components/samsung_ac/samsung_ac_device.h @@ -83,9 +83,7 @@ namespace esphome std::string address; sensor::Sensor *room_temperature{nullptr}; - sensor::Sensor *water_temperature{nullptr}; sensor::Sensor *outdoor_temperature{nullptr}; - sensor::Sensor *room_humidity{nullptr}; Samsung_AC_Number *target_temperature{nullptr}; Samsung_AC_Switch *power{nullptr}; Samsung_AC_Mode_Select *mode{nullptr}; @@ -102,15 +100,6 @@ namespace esphome outdoor_temperature = sensor; } - void set_water_temperature_sensor(sensor::Sensor *sensor) - { - water_temperature = sensor; - } - - void set_room_humidity_sensor(sensor::Sensor *sensor) - { - room_humidity = sensor; - } void add_custom_sensor(int message_number, sensor::Sensor *sensor) { @@ -167,12 +156,6 @@ namespace esphome climate->device = this; } - void update_outdoor_temperature(float value) - { - if (outdoor_temperature != nullptr) - outdoor_temperature->publish_state(value); - } - void update_target_temperature(float value) { if (target_temperature != nullptr) @@ -274,16 +257,10 @@ namespace esphome } } - void update_water_temperature(float value) - { - if (water_temperature != nullptr) - water_temperature->publish_state(value); - } - - void update_room_humidity(float value) + void update_outdoor_temperature(float value) { - if (room_humidity != nullptr) - room_humidity->publish_state(value); + if (outdoor_temperature != nullptr) + outdoor_temperature->publish_state(value); } void update_custom_sensor(uint16_t message_number, float value)