Skip to content

Commit

Permalink
Merge pull request #3 from mill1000/feature/filter_lifetime
Browse files Browse the repository at this point in the history
Expose the filter lifetime value as a sensor.
  • Loading branch information
mill1000 authored Nov 7, 2023
2 parents b7a9c29 + aaf7d38 commit 255f18d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ sensor:
- platform: winix_c545
filter_age:
name: Filter Age
filter_lifetime:
name: Filter Lifetime
aqi:
name: AQI
light:
Expand Down
16 changes: 15 additions & 1 deletion components/winix_c545/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
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

DEPENDENCIES = ["winix_c545"]

CONF_AQI = "aqi"
CONF_FILTER_AGE = "filter_age"
CONF_FILTER_LIFETIME = "filter_lifetime"
CONF_LIGHT = "light"

CONFIG_SCHEMA = cv.Schema(
Expand All @@ -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,
Expand All @@ -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))
Expand Down
22 changes: 14 additions & 8 deletions components/winix_c545/winix_c545.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const std::unordered_map<std::string, StateKey> 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},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions components/winix_c545/winix_c545.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -42,6 +43,7 @@ enum class StateKey {

// Sensor keys
FilterAge,
FilterLifetime,
AQIIndicator,
AQI,
Light
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ sensor:
- platform: winix_c545
filter_age:
name: Filter Age
filter_lifetime:
name: Filter Lifetime
aqi:
name: AQI
light:
Expand Down

0 comments on commit 255f18d

Please sign in to comment.