From aaf7d38c2a30c7d399f87c3d8c26c36cd0eb288f Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Tue, 7 Nov 2023 16:26:40 -0700 Subject: [PATCH] Expose the filter lifetime value as a sensor. I would have preferred an attribute but those aren't supported by ESPHome --- README.md | 2 ++ components/winix_c545/sensor.py | 16 +++++++++++++++- components/winix_c545/winix_c545.cpp | 22 ++++++++++++++-------- components/winix_c545/winix_c545.h | 3 +++ example.yaml | 2 ++ 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9369a42..22c7b07 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ sensor: - platform: winix_c545 filter_age: name: Filter Age + filter_lifetime: + name: Filter Lifetime aqi: name: AQI light: diff --git a/components/winix_c545/sensor.py b/components/winix_c545/sensor.py index 71ec963..5fce74a 100644 --- a/components/winix_c545/sensor.py +++ b/components/winix_c545/sensor.py @@ -2,7 +2,8 @@ import esphome.config_validation as cv from esphome.components import sensor from esphome.const import (DEVICE_CLASS_AQI, DEVICE_CLASS_DURATION, - STATE_CLASS_MEASUREMENT, UNIT_EMPTY, UNIT_HOUR) + ENTITY_CATEGORY_DIAGNOSTIC, STATE_CLASS_MEASUREMENT, + UNIT_EMPTY, UNIT_HOUR) from . import CONF_WINIX_C545_ID, WinixC545Component @@ -10,6 +11,7 @@ CONF_AQI = "aqi" CONF_FILTER_AGE = "filter_age" +CONF_FILTER_LIFETIME = "filter_lifetime" CONF_LIGHT = "light" CONFIG_SCHEMA = cv.Schema( @@ -20,6 +22,14 @@ accuracy_decimals=0, device_class=DEVICE_CLASS_DURATION, state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + cv.Optional(CONF_FILTER_LIFETIME): sensor.sensor_schema( + unit_of_measurement=UNIT_HOUR, + accuracy_decimals=0, + device_class=DEVICE_CLASS_DURATION, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), cv.Optional(CONF_AQI): sensor.sensor_schema( unit_of_measurement=UNIT_EMPTY, @@ -43,6 +53,10 @@ async def to_code(config) -> None: sens = await sensor.new_sensor(sensor_config) cg.add(component.set_filter_age_sensor(sens)) + if sensor_config := config.get(CONF_FILTER_LIFETIME): + sens = await sensor.new_sensor(sensor_config) + cg.add(component.set_filter_lifetime_sensor(sens)) + if sensor_config := config.get(CONF_AQI): sens = await sensor.new_sensor(sensor_config) cg.add(component.set_aqi_sensor(sens)) diff --git a/components/winix_c545/winix_c545.cpp b/components/winix_c545/winix_c545.cpp index cf905a0..33a4d7f 100644 --- a/components/winix_c545/winix_c545.cpp +++ b/components/winix_c545/winix_c545.cpp @@ -18,6 +18,7 @@ const std::unordered_map WinixC545Component::ENUM_KEY_MAP {KEY_PLASMAWAVE, StateKey::Plasmawave}, {KEY_FILTER_AGE, StateKey::FilterAge}, + {KEY_FILTER_LIFETIME, StateKey::FilterLifetime}, {KEY_AQI_INDICATOR, StateKey::AQIIndicator}, {KEY_AQI, StateKey::AQI}, {KEY_LIGHT, StateKey::Light}, @@ -142,6 +143,16 @@ void WinixC545Component::publish_state_() { break; } + case StateKey::FilterLifetime: { + // Filter lifetime + if (this->filter_lifetime_sensor_ == nullptr) + continue; + + if (value != this->filter_lifetime_sensor_->raw_state) + this->filter_lifetime_sensor_->publish_state(value); + break; + } + case StateKey::Plasmawave: { // Plasmawave if (this->plasmawave_switch_ == nullptr) @@ -210,6 +221,8 @@ void WinixC545Component::parse_aws_sentence_(char *sentence) { case 210: // Overall device state case 220: // Sensor update + case 230: // Error code + case 240: // Version information & filter lifetime { // Advance sentence to first token sentence += strlen("AWS_SEND=A2XX {"); @@ -237,14 +250,6 @@ void WinixC545Component::parse_aws_sentence_(char *sentence) { break; } - case 230: // Error code - case 240: // Version information - { - ESP_LOGI(TAG, "Misc update: %s", sentence); - valid = true; - break; - } - default: ESP_LOGW(TAG, "Unknown API code %d: %s", api_code, sentence); break; @@ -422,6 +427,7 @@ void WinixC545Component::dump_config() { #ifdef USE_SENSOR LOG_SENSOR(" ", "Filter Age Sensor", this->filter_age_sensor_); + LOG_SENSOR(" ", "Filter Lifetime Sensor", this->filter_lifetime_sensor_); LOG_SENSOR(" ", "AQI Sensor", this->aqi_sensor_); LOG_SENSOR(" ", "Light Sensor", this->light_sensor_); #endif diff --git a/components/winix_c545/winix_c545.h b/components/winix_c545/winix_c545.h index 63bb576..8dade39 100644 --- a/components/winix_c545/winix_c545.h +++ b/components/winix_c545/winix_c545.h @@ -29,6 +29,7 @@ static constexpr const char *KEY_PLASMAWAVE = "A07"; // Sensor keys static constexpr const char *KEY_FILTER_AGE = "A21"; +static constexpr const char *KEY_FILTER_LIFETIME = "P01"; static constexpr const char *KEY_AQI_INDICATOR = "S07"; static constexpr const char *KEY_AQI = "S08"; static constexpr const char *KEY_LIGHT = "S14"; @@ -42,6 +43,7 @@ enum class StateKey { // Sensor keys FilterAge, + FilterLifetime, AQIIndicator, AQI, Light @@ -55,6 +57,7 @@ class WinixC545Fan; class WinixC545Component : public uart::UARTDevice, public Component { #ifdef USE_SENSOR SUB_SENSOR(filter_age) + SUB_SENSOR(filter_lifetime) SUB_SENSOR(aqi) SUB_SENSOR(light) #endif diff --git a/example.yaml b/example.yaml index 5a611f2..fb19c90 100644 --- a/example.yaml +++ b/example.yaml @@ -21,6 +21,8 @@ sensor: - platform: winix_c545 filter_age: name: Filter Age + filter_lifetime: + name: Filter Lifetime aqi: name: AQI light: