From f791e5b765590e09a05d29b649c203bdaa5f90ed Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 24 Dec 2018 14:14:50 +0100 Subject: [PATCH] Fix some stuff --- src/esphomelib/api/api.proto | 1 + src/esphomelib/api/api_server.cpp | 32 ++++++++++------- src/esphomelib/api/api_server.h | 19 ++++++---- src/esphomelib/api/list_entities.cpp | 2 +- src/esphomelib/api/util.cpp | 3 ++ src/esphomelib/api/util.h | 1 + src/esphomelib/application.cpp | 26 +++++++++++--- src/esphomelib/application.h | 13 +++++++ src/esphomelib/binary_sensor/binary_sensor.h | 8 +++++ .../binary_sensor/custom_binary_sensor.cpp | 8 +++++ .../binary_sensor/custom_binary_sensor.h | 5 ++- .../esp32_touch_binary_sensor.cpp | 4 +-- .../gpio_binary_sensor_component.cpp | 2 +- .../mqtt_binary_sensor_component.cpp | 3 -- .../binary_sensor/pn532_component.cpp | 4 +++ .../binary_sensor/status_binary_sensor.cpp | 6 ++++ .../binary_sensor/status_binary_sensor.h | 1 + .../binary_sensor/template_binary_sensor.cpp | 6 ++++ .../binary_sensor/template_binary_sensor.h | 1 + src/esphomelib/cover/cover.h | 8 +++++ src/esphomelib/cover/mqtt_cover_component.cpp | 3 -- src/esphomelib/cover/template_cover.cpp | 6 ++++ src/esphomelib/cover/template_cover.h | 1 + src/esphomelib/display/display.cpp | 13 ------- src/esphomelib/display/display.h | 15 +++++--- src/esphomelib/display/ssd1306.cpp | 6 ++-- src/esphomelib/display/waveshare_epaper.cpp | 12 +++---- src/esphomelib/esp32_ble_tracker.cpp | 19 ++++++++++ src/esphomelib/esp32_ble_tracker.h | 1 + src/esphomelib/fan/basic_fan_component.cpp | 10 +++--- src/esphomelib/light/light_state.cpp | 11 ++++++ src/esphomelib/light/light_state.h | 1 + src/esphomelib/log_component.cpp | 7 +++- src/esphomelib/mqtt/mqtt_client_component.h | 2 +- src/esphomelib/remote/remote_receiver.cpp | 8 +++++ src/esphomelib/remote/remote_transmitter.cpp | 9 ++++- src/esphomelib/sensor/adc.cpp | 2 +- src/esphomelib/sensor/ads1115_component.cpp | 2 +- src/esphomelib/sensor/bh1750_sensor.cpp | 2 +- src/esphomelib/sensor/bme280_component.cpp | 13 ++++--- src/esphomelib/sensor/bme680_component.cpp | 14 +++++--- src/esphomelib/sensor/bmp085_component.cpp | 3 ++ src/esphomelib/sensor/bmp280_component.cpp | 9 +++-- src/esphomelib/sensor/cse7766.cpp | 3 ++ src/esphomelib/sensor/custom_sensor.cpp | 8 +++++ src/esphomelib/sensor/custom_sensor.h | 5 ++- src/esphomelib/sensor/dallas_component.cpp | 3 +- src/esphomelib/sensor/dht12_component.cpp | 2 ++ src/esphomelib/sensor/dht_component.cpp | 3 ++ src/esphomelib/sensor/duty_cycle_sensor.cpp | 2 +- src/esphomelib/sensor/esp32_hall_sensor.cpp | 11 ++++-- src/esphomelib/sensor/esp32_hall_sensor.h | 2 ++ src/esphomelib/sensor/hdc1080_component.cpp | 2 ++ src/esphomelib/sensor/hlw8012.cpp | 3 ++ src/esphomelib/sensor/hmc5883l.cpp | 6 +++- .../sensor/homeassistant_sensor.cpp | 12 ++++++- src/esphomelib/sensor/homeassistant_sensor.h | 5 ++- src/esphomelib/sensor/htu21d_component.cpp | 2 ++ src/esphomelib/sensor/hx711.cpp | 2 +- src/esphomelib/sensor/ina219.cpp | 6 +++- src/esphomelib/sensor/ina3221.cpp | 14 +++++++- src/esphomelib/sensor/max31855_sensor.cpp | 2 +- src/esphomelib/sensor/max6675_sensor.cpp | 2 +- src/esphomelib/sensor/mhz19_component.cpp | 5 +++ src/esphomelib/sensor/mhz19_component.h | 1 + src/esphomelib/sensor/mpu6050_component.cpp | 7 ++++ .../sensor/mqtt_sensor_component.cpp | 8 ----- .../sensor/mqtt_subscribe_sensor.cpp | 2 +- src/esphomelib/sensor/ms5611.cpp | 2 ++ src/esphomelib/sensor/pmsx003.cpp | 9 +++++ src/esphomelib/sensor/pmsx003.h | 1 + src/esphomelib/sensor/pulse_counter.cpp | 2 +- src/esphomelib/sensor/rotary_encoder.cpp | 5 ++- src/esphomelib/sensor/sensor.cpp | 17 ++++----- src/esphomelib/sensor/sensor.h | 13 +++++++ src/esphomelib/sensor/sht3xd_component.cpp | 3 ++ src/esphomelib/sensor/tcs34725.cpp | 7 ++++ src/esphomelib/sensor/template_sensor.cpp | 2 +- src/esphomelib/sensor/total_daily_energy.cpp | 7 +++- src/esphomelib/sensor/total_daily_energy.h | 1 + src/esphomelib/sensor/tsl2561_sensor.cpp | 2 +- src/esphomelib/sensor/ultrasonic_sensor.cpp | 2 +- src/esphomelib/sensor/uptime_sensor.cpp | 2 +- src/esphomelib/sensor/wifi_signal_sensor.cpp | 6 ++++ src/esphomelib/sensor/wifi_signal_sensor.h | 1 + src/esphomelib/switch_/custom_switch.cpp | 9 +++++ src/esphomelib/switch_/custom_switch.h | 5 ++- src/esphomelib/switch_/gpio_switch.cpp | 3 +- .../switch_/mqtt_switch_component.cpp | 6 ---- src/esphomelib/switch_/output_switch.cpp | 6 +++- src/esphomelib/switch_/output_switch.h | 1 + src/esphomelib/switch_/restart_switch.cpp | 3 ++ src/esphomelib/switch_/restart_switch.h | 5 ++- src/esphomelib/switch_/shutdown_switch.cpp | 3 ++ src/esphomelib/switch_/shutdown_switch.h | 4 ++- src/esphomelib/switch_/switch.cpp | 3 ++ src/esphomelib/switch_/switch.h | 17 +++++++-- src/esphomelib/switch_/template_switch.cpp | 4 +-- src/esphomelib/switch_/uart_switch.cpp | 3 ++ src/esphomelib/switch_/uart_switch.h | 4 ++- .../text_sensor/custom_text_sensor.cpp | 9 +++++ .../text_sensor/custom_text_sensor.h | 5 ++- .../text_sensor/homeassistant_text_sensor.cpp | 9 ++++- .../text_sensor/homeassistant_text_sensor.h | 4 ++- .../mqtt_subscribe_text_sensor.cpp | 2 +- .../text_sensor/mqtt_text_sensor.cpp | 6 ---- .../text_sensor/template_text_sensor.cpp | 6 ++++ .../text_sensor/template_text_sensor.h | 2 ++ src/esphomelib/text_sensor/text_sensor.h | 11 ++++++ .../text_sensor/version_text_sensor.cpp | 6 ++++ .../text_sensor/version_text_sensor.h | 1 + src/esphomelib/wifi_component.cpp | 35 +++++++++++-------- 112 files changed, 524 insertions(+), 170 deletions(-) diff --git a/src/esphomelib/api/api.proto b/src/esphomelib/api/api.proto index 33d08709..bde079be 100644 --- a/src/esphomelib/api/api.proto +++ b/src/esphomelib/api/api.proto @@ -290,6 +290,7 @@ message SubscribeLogsResponse { LogLevel level = 1; string tag = 2; string message = 3; + bool send_failed = 4; } message SubscribeServiceCallsRequest { diff --git a/src/esphomelib/api/api_server.cpp b/src/esphomelib/api/api_server.cpp index b4e0603b..65e58177 100644 --- a/src/esphomelib/api/api_server.cpp +++ b/src/esphomelib/api/api_server.cpp @@ -202,7 +202,7 @@ APIConnection::APIConnection(AsyncClient *client, APIServer *parent) ((APIConnection *) s)->on_data_(reinterpret_cast(buf), len); }, this); - this->buffer_size_ = 128; + this->buffer_size_ = 256; this->buffer_ = new uint8_t[this->buffer_size_]; this->client_info_ = this->client_->remoteIP().toString().c_str(); this->last_traffic_ = millis(); @@ -607,16 +607,11 @@ bool APIConnection::valid_rx_message_type_() { return this->connection_state_ == ConnectionState::CONNECTED; } } -bool APIConnection::send_message(APIMessage &msg) { - APIBuffer buf(this->buffer_, this->buffer_size_); - msg.encode(buf); - return this->send_buffer(msg.message_type(), buf); -} -bool APIConnection::send_message_resize(APIMessage &msg) { +bool APIConnection::send_message(APIMessage &msg, bool resize) { while (true) { APIBuffer buf(this->buffer_, this->buffer_size_); msg.encode(buf); - if (buf.get_overflow()) { + if (buf.get_overflow() && resize) { this->resize_buffer_(); } else { return this->send_buffer(msg.message_type(), buf); @@ -649,10 +644,12 @@ bool APIConnection::send_buffer(APIMessageType type, APIBuffer &buf) { needed_space += header_buffer.get_length(); if (needed_space > this->client_->space()) { - if (type != APIMessageType::SUBSCRIBE_LOGS_RESPONSE) { + delay(5); + if (needed_space > this->client_->space()) { ESP_LOGV(TAG, "Cannot send message because of TCP buffer space"); + delay(5); + return false; } - return false; } this->client_->add(reinterpret_cast(header_raw), header_buffer.get_length()); @@ -831,14 +828,23 @@ bool APIConnection::send_log_message(int level, if (this->log_subscription_ < level) return false; - return this->send_buffer([level, tag, line](APIBuffer &buffer) { + bool success = this->send_buffer([level, tag, line](APIBuffer &buffer) { // LogLevel level = 1; buffer.encode_uint32(1, static_cast(level)); // string tag = 2; buffer.encode_string(2, tag, strlen(tag)); // string message = 3; buffer.encode_string(3, line, strlen(line)); - }, APIMessageType::SUBSCRIBE_LOGS_RESPONSE); + }, APIMessageType::SUBSCRIBE_LOGS_RESPONSE, false); + + if (!success) { + return this->send_buffer([level, tag, line](APIBuffer &buffer) { + // bool send_failed = 4; + buffer.encode_bool(4, true); + }, APIMessageType::SUBSCRIBE_LOGS_RESPONSE, false); + } else { + return true; + } } bool APIConnection::send_disconnect_request(const char *reason) { DisconnectRequest req; @@ -935,7 +941,7 @@ void APIConnection::send_service_call(ServiceCallResponse &call) { if (!this->service_call_subscription_) return; - this->send_message_resize(call); + this->send_message(call); } void APIConnection::on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &req) { for (auto &it : this->parent_->get_state_subs()) { diff --git a/src/esphomelib/api/api_server.h b/src/esphomelib/api/api_server.h index e01d9a66..391d49c6 100644 --- a/src/esphomelib/api/api_server.h +++ b/src/esphomelib/api/api_server.h @@ -38,9 +38,8 @@ class APIConnection { void disconnect_client_(); bool send_buffer(APIMessageType type, APIBuffer &buf); template - bool send_buffer(T func, APIMessageType type); - bool send_message(APIMessage &msg); - bool send_message_resize(APIMessage &msg); + bool send_buffer(T func, APIMessageType type, bool resize = true); + bool send_message(APIMessage &msg, bool resize = true); bool send_empty_message(APIMessageType type); void loop(); @@ -242,10 +241,16 @@ void HomeAssistantServiceCallAction::play(T x) { } template -bool APIConnection::send_buffer(T func, APIMessageType type) { - APIBuffer buf(this->buffer_, this->buffer_size_); - func(buf); - return this->send_buffer(type, buf); +bool APIConnection::send_buffer(T func, APIMessageType type, bool resize) { + while (true) { + APIBuffer buf(this->buffer_, this->buffer_size_); + func(buf); + if (buf.get_overflow() && resize) { + this->resize_buffer_(); + } else { + return this->send_buffer(type, buf); + } + } } } // namespace api diff --git a/src/esphomelib/api/list_entities.cpp b/src/esphomelib/api/list_entities.cpp index 7d76095b..8891a4a5 100644 --- a/src/esphomelib/api/list_entities.cpp +++ b/src/esphomelib/api/list_entities.cpp @@ -92,7 +92,7 @@ bool ListEntitiesIterator::on_sensor(sensor::Sensor *sensor) { // string unit_of_measurement = 6; buffer.encode_string(6, sensor->get_unit_of_measurement()); // int32 accuracy_decimals = 7; - buffer.encode_sint32(7, sensor->get_accuracy_decimals()); + buffer.encode_uint32(7, sensor->get_accuracy_decimals()); }, APIMessageType::LIST_ENTITIES_SENSOR_RESPONSE); } #endif diff --git a/src/esphomelib/api/util.cpp b/src/esphomelib/api/util.cpp index 8bdde013..f88709da 100644 --- a/src/esphomelib/api/util.cpp +++ b/src/esphomelib/api/util.cpp @@ -34,6 +34,9 @@ void APIBuffer::encode_uint32(uint32_t field, uint32_t value) { this->encode_field_(field, 0); this->encode_varint_(value); } +void APIBuffer::encode_int32(uint32_t field, int32_t value) { + this->encode_uint32(field, static_cast(value)); +} void APIBuffer::encode_bool(uint32_t field, bool value) { if (!value) return; diff --git a/src/esphomelib/api/util.h b/src/esphomelib/api/util.h index 42fa96a3..031e4533 100644 --- a/src/esphomelib/api/util.h +++ b/src/esphomelib/api/util.h @@ -22,6 +22,7 @@ class APIBuffer { bool get_overflow() const; void write(uint8_t value); + void encode_int32(uint32_t field, int32_t value); void encode_uint32(uint32_t field, uint32_t value); void encode_sint32(uint32_t field, int32_t value); void encode_bool(uint32_t field, bool value); diff --git a/src/esphomelib/application.cpp b/src/esphomelib/application.cpp index 28ea716a..66b6cc57 100644 --- a/src/esphomelib/application.cpp +++ b/src/esphomelib/application.cpp @@ -555,7 +555,7 @@ Application::MakeStatusBinarySensor Application::make_status_binary_sensor(const #ifdef USE_RESTART_SWITCH Application::MakeRestartSwitch Application::make_restart_switch(const std::string &friendly_name) { - auto *switch_ = new RestartSwitch(friendly_name); // not a component + auto *switch_ = this->register_component(new RestartSwitch(friendly_name)); return MakeRestartSwitch{ .restart = switch_, .mqtt = this->register_switch(switch_), @@ -565,7 +565,7 @@ Application::MakeRestartSwitch Application::make_restart_switch(const std::strin #ifdef USE_SHUTDOWN_SWITCH Application::MakeShutdownSwitch Application::make_shutdown_switch(const std::string &friendly_name) { - auto *switch_ = new ShutdownSwitch(friendly_name); + auto *switch_ = this->register_component(new ShutdownSwitch(friendly_name)); return MakeShutdownSwitch{ .shutdown = switch_, .mqtt = this->register_switch(switch_), @@ -1228,7 +1228,7 @@ Application::MakeMQTTSubscribeTextSensor Application::make_mqtt_subscribe_text_s #ifdef USE_HOMEASSISTANT_TEXT_SENSOR Application::MakeHomeassistantTextSensor Application::make_homeassistant_text_sensor(const std::string &name, std::string entity_id) { - auto *sensor = new HomeassistantTextSensor(name, std::move(entity_id)); + auto *sensor = this->register_component(new HomeassistantTextSensor(name, std::move(entity_id))); return MakeHomeassistantTextSensor { .sensor = sensor, .mqtt = this->register_text_sensor(sensor), @@ -1260,7 +1260,7 @@ Application::MakeMQTTSubscribeSensor Application::make_mqtt_subscribe_sensor(con #ifdef USE_HOMEASSISTANT_SENSOR Application::MakeHomeassistantSensor Application::make_homeassistant_sensor(const std::string &name, std::string entity_id) { - auto *sensor = new sensor::HomeassistantSensor(name, std::move(entity_id)); + auto *sensor = this->register_component(new HomeassistantSensor(name, std::move(entity_id))); return MakeHomeassistantSensor { .sensor = sensor, @@ -1339,6 +1339,24 @@ api::APIServer *Application::init_api_server() { } #endif +#ifdef USE_CUSTOM_BINARY_SENSOR +binary_sensor::CustomBinarySensorConstructor *Application::make_custom_binary_sensor(const std::function()> &init) { + return this->register_component(new CustomBinarySensorConstructor(init)); +} +#endif + +#ifdef USE_CUSTOM_SENSOR +sensor::CustomSensorConstructor *Application::make_custom_sensor(const std::function()> &init) { + return this->register_component(new CustomSensorConstructor(init)); +} +#endif + +#ifdef USE_CUSTOM_SWITCH +switch_::CustomSwitchConstructor *Application::make_custom_switch(const std::function()> &init) { + return this->register_component(new CustomSwitchConstructor(init)); +} +#endif + Application App; // NOLINT ESPHOMELIB_NAMESPACE_END diff --git a/src/esphomelib/application.h b/src/esphomelib/application.h index 01af9b6d..e5a981c6 100644 --- a/src/esphomelib/application.h +++ b/src/esphomelib/application.h @@ -432,6 +432,10 @@ class Application { binary_sensor::RDM6300Component *make_rdm6300_component(UARTComponent *parent); #endif +#ifdef USE_CUSTOM_BINARY_SENSOR + binary_sensor::CustomBinarySensorConstructor *make_custom_binary_sensor(const std::function()> &init); +#endif + @@ -1010,6 +1014,11 @@ class Application { #endif +#ifdef USE_CUSTOM_SENSOR + sensor::CustomSensorConstructor *make_custom_sensor(const std::function()> &init); +#endif + + @@ -1258,6 +1267,10 @@ class Application { MakeUARTSwitch make_uart_switch(UARTComponent *parent, const std::string &name, const std::vector &data); #endif +#ifdef USE_CUSTOM_SWITCH + switch_::CustomSwitchConstructor *make_custom_switch(const std::function()> &init); +#endif + diff --git a/src/esphomelib/binary_sensor/binary_sensor.h b/src/esphomelib/binary_sensor/binary_sensor.h index be9e1d5f..fc71681d 100644 --- a/src/esphomelib/binary_sensor/binary_sensor.h +++ b/src/esphomelib/binary_sensor/binary_sensor.h @@ -32,6 +32,14 @@ struct MultiClickTriggerEvent { uint32_t max_length; }; +#define LOG_BINARY_SENSOR(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \ + if (!obj->get_device_class().empty()) { \ + ESP_LOGCONFIG(TAG, prefix " Device Class: '%s'", obj->get_device_class().c_str()); \ + } \ + } + /** Base class for all binary_sensor-type classes. * * This class includes a callback that components such as MQTT can subscribe to for state changes. diff --git a/src/esphomelib/binary_sensor/custom_binary_sensor.cpp b/src/esphomelib/binary_sensor/custom_binary_sensor.cpp index cd5322c9..0dc10d5c 100644 --- a/src/esphomelib/binary_sensor/custom_binary_sensor.cpp +++ b/src/esphomelib/binary_sensor/custom_binary_sensor.cpp @@ -3,17 +3,25 @@ #ifdef USE_CUSTOM_BINARY_SENSOR #include "esphomelib/binary_sensor/custom_binary_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace binary_sensor { +static const char *TAG = "binary_sensor.custom"; + CustomBinarySensorConstructor::CustomBinarySensorConstructor(const std::function()> &init) { this->binary_sensors_ = init(); } BinarySensor *CustomBinarySensorConstructor::get_binary_sensor(int i) { return this->binary_sensors_[i]; } +void CustomBinarySensorConstructor::dump_config() { + for (auto *child : this->binary_sensors_) { + LOG_BINARY_SENSOR("", "Custom Binary Sensor", child); + } +} } // namespace binary_sensor diff --git a/src/esphomelib/binary_sensor/custom_binary_sensor.h b/src/esphomelib/binary_sensor/custom_binary_sensor.h index 2f3a29be..a96d27cf 100644 --- a/src/esphomelib/binary_sensor/custom_binary_sensor.h +++ b/src/esphomelib/binary_sensor/custom_binary_sensor.h @@ -5,18 +5,21 @@ #ifdef USE_CUSTOM_BINARY_SENSOR +#include "esphomelib/component.h" #include "esphomelib/binary_sensor/binary_sensor.h" ESPHOMELIB_NAMESPACE_BEGIN namespace binary_sensor { -class CustomBinarySensorConstructor { +class CustomBinarySensorConstructor : public Component { public: CustomBinarySensorConstructor(const std::function()> &init); BinarySensor *get_binary_sensor(int i); + void dump_config() override; + protected: std::vector binary_sensors_; }; diff --git a/src/esphomelib/binary_sensor/esp32_touch_binary_sensor.cpp b/src/esphomelib/binary_sensor/esp32_touch_binary_sensor.cpp index e3f7b227..b644ecce 100644 --- a/src/esphomelib/binary_sensor/esp32_touch_binary_sensor.cpp +++ b/src/esphomelib/binary_sensor/esp32_touch_binary_sensor.cpp @@ -84,8 +84,8 @@ void ESP32TouchComponent::dump_config() { } for (auto *child : this->children_) { - ESP_LOGCONFIG(TAG, " Pad '%s'", child->get_name().c_str()); - ESP_LOGCONFIG(TAG, " Touch Pad: T%d", child->get_touch_pad()); + LOG_BINARY_SENSOR(" ", "Touch Pad", child); + ESP_LOGCONFIG(TAG, " Pad: T%d", child->get_touch_pad()); ESP_LOGCONFIG(TAG, " Threshold: %u", child->get_threshold()); } } diff --git a/src/esphomelib/binary_sensor/gpio_binary_sensor_component.cpp b/src/esphomelib/binary_sensor/gpio_binary_sensor_component.cpp index 5403a7c5..061f009e 100644 --- a/src/esphomelib/binary_sensor/gpio_binary_sensor_component.cpp +++ b/src/esphomelib/binary_sensor/gpio_binary_sensor_component.cpp @@ -20,7 +20,7 @@ void GPIOBinarySensorComponent::setup() { } void GPIOBinarySensorComponent::dump_config() { - ESP_LOGCONFIG(TAG, "GPIO Binary Sensor '%s':", this->name_.c_str()); + LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this); LOG_PIN(" Pin: ", this->pin_); } diff --git a/src/esphomelib/binary_sensor/mqtt_binary_sensor_component.cpp b/src/esphomelib/binary_sensor/mqtt_binary_sensor_component.cpp index b2bdda4e..db173253 100644 --- a/src/esphomelib/binary_sensor/mqtt_binary_sensor_component.cpp +++ b/src/esphomelib/binary_sensor/mqtt_binary_sensor_component.cpp @@ -23,9 +23,6 @@ void MQTTBinarySensorComponent::setup() { void MQTTBinarySensorComponent::dump_config() { ESP_LOGCONFIG(TAG, "MQTT Binary Sensor '%s':", this->binary_sensor_->get_name().c_str()); - if (!this->binary_sensor_->get_device_class().empty()) { - ESP_LOGCONFIG(TAG, " Device Class: '%s'", this->binary_sensor_->get_device_class().c_str()); - } LOG_MQTT_COMPONENT(true, false) } MQTTBinarySensorComponent::MQTTBinarySensorComponent(BinarySensor *binary_sensor) diff --git a/src/esphomelib/binary_sensor/pn532_component.cpp b/src/esphomelib/binary_sensor/pn532_component.cpp index 3bada92f..f008b89e 100644 --- a/src/esphomelib/binary_sensor/pn532_component.cpp +++ b/src/esphomelib/binary_sensor/pn532_component.cpp @@ -263,6 +263,10 @@ void PN532Component::dump_config() { LOG_PIN(" CS Pin: ", this->cs_); LOG_UPDATE_INTERVAL(this); + + for (auto *child : this->binary_sensors_) { + LOG_BINARY_SENSOR(" ", "Tag", child); + } } PN532BinarySensor::PN532BinarySensor(const std::string &name, const std::vector &uid, uint32_t update_interval) diff --git a/src/esphomelib/binary_sensor/status_binary_sensor.cpp b/src/esphomelib/binary_sensor/status_binary_sensor.cpp index 6028a2d5..268c5ba0 100644 --- a/src/esphomelib/binary_sensor/status_binary_sensor.cpp +++ b/src/esphomelib/binary_sensor/status_binary_sensor.cpp @@ -5,11 +5,14 @@ #include "esphomelib/binary_sensor/status_binary_sensor.h" #include "esphomelib/mqtt/mqtt_client_component.h" #include "esphomelib/wifi_component.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace binary_sensor { +static const char *TAG = "binary_sensor.status"; + std::string StatusBinarySensor::device_class() { return "connectivity"; } @@ -39,6 +42,9 @@ float StatusBinarySensor::get_setup_priority() const { bool StatusBinarySensor::is_status_binary_sensor() const { return true; } +void StatusBinarySensor::dump_config() { + LOG_BINARY_SENSOR("", "Status Binary Sensor", this); +} } // namespace binary_sensor diff --git a/src/esphomelib/binary_sensor/status_binary_sensor.h b/src/esphomelib/binary_sensor/status_binary_sensor.h index 30da5754..de736006 100644 --- a/src/esphomelib/binary_sensor/status_binary_sensor.h +++ b/src/esphomelib/binary_sensor/status_binary_sensor.h @@ -23,6 +23,7 @@ class StatusBinarySensor : public BinarySensor, public Component { void loop() override; void setup() override; + void dump_config() override; float get_setup_priority() const override; diff --git a/src/esphomelib/binary_sensor/template_binary_sensor.cpp b/src/esphomelib/binary_sensor/template_binary_sensor.cpp index daf97d5c..805cf7a9 100644 --- a/src/esphomelib/binary_sensor/template_binary_sensor.cpp +++ b/src/esphomelib/binary_sensor/template_binary_sensor.cpp @@ -3,11 +3,14 @@ #ifdef USE_TEMPLATE_BINARY_SENSOR #include "esphomelib/binary_sensor/template_binary_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace binary_sensor { +static const char *TAG = "binary_sensor.template"; + TemplateBinarySensor::TemplateBinarySensor(const std::string &name) : BinarySensor(name) { @@ -29,6 +32,9 @@ float TemplateBinarySensor::get_setup_priority() const { void TemplateBinarySensor::set_template(std::function()> &&f) { this->f_ = std::move(f); } +void TemplateBinarySensor::dump_config() { + LOG_BINARY_SENSOR("", "Template Binary Sensor", this); +} } // namespace binary_sensor diff --git a/src/esphomelib/binary_sensor/template_binary_sensor.h b/src/esphomelib/binary_sensor/template_binary_sensor.h index 4dba0287..30dcfbfc 100644 --- a/src/esphomelib/binary_sensor/template_binary_sensor.h +++ b/src/esphomelib/binary_sensor/template_binary_sensor.h @@ -19,6 +19,7 @@ class TemplateBinarySensor : public Component, public BinarySensor { void set_template(std::function()> &&f); void loop() override; + void dump_config() override; float get_setup_priority() const override; diff --git a/src/esphomelib/cover/cover.h b/src/esphomelib/cover/cover.h index ba823ff9..47af0f20 100644 --- a/src/esphomelib/cover/cover.h +++ b/src/esphomelib/cover/cover.h @@ -31,6 +31,14 @@ class CloseAction; template class StopAction; +#define LOG_COVER(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \ + if (obj->optimistic()) { \ + ESP_LOGCONFIG(TAG, prefix " Optimistic: YES"); \ + } \ + } + class Cover : public Nameable { public: explicit Cover(const std::string &name); diff --git a/src/esphomelib/cover/mqtt_cover_component.cpp b/src/esphomelib/cover/mqtt_cover_component.cpp index ae1e9aef..118ddc01 100644 --- a/src/esphomelib/cover/mqtt_cover_component.cpp +++ b/src/esphomelib/cover/mqtt_cover_component.cpp @@ -34,9 +34,6 @@ void MQTTCoverComponent::setup() { void MQTTCoverComponent::dump_config() { ESP_LOGCONFIG(TAG, "MQTT cover '%s':", this->cover_->get_name().c_str()); - if (this->cover_->optimistic()) { - ESP_LOGCONFIG(TAG, " Optimistic: YES"); - } LOG_MQTT_COMPONENT(true, true) } void MQTTCoverComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) { diff --git a/src/esphomelib/cover/template_cover.cpp b/src/esphomelib/cover/template_cover.cpp index 6ba4d463..91cd032c 100644 --- a/src/esphomelib/cover/template_cover.cpp +++ b/src/esphomelib/cover/template_cover.cpp @@ -3,11 +3,14 @@ #ifdef USE_TEMPLATE_COVER #include "esphomelib/cover/template_cover.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace cover { +static const char *TAG = "cover.template"; + TemplateCover::TemplateCover(const std::string &name) : Cover(name), open_trigger_(new Trigger()), close_trigger_(new Trigger), stop_trigger_(new Trigger()) { @@ -66,6 +69,9 @@ void TemplateCover::write_command(CoverCommand command) { } } } +void TemplateCover::dump_config() { + LOG_COVER("Template Cover", "", this); +} } // namespace cover diff --git a/src/esphomelib/cover/template_cover.h b/src/esphomelib/cover/template_cover.h index 63bb494f..32c5b0ab 100644 --- a/src/esphomelib/cover/template_cover.h +++ b/src/esphomelib/cover/template_cover.h @@ -23,6 +23,7 @@ class TemplateCover : public Cover, public Component { void set_optimistic(bool optimistic); void loop() override; + void dump_config() override; float get_setup_priority() const override; diff --git a/src/esphomelib/display/display.cpp b/src/esphomelib/display/display.cpp index 3c372aef..53e3bd26 100644 --- a/src/esphomelib/display/display.cpp +++ b/src/esphomelib/display/display.cpp @@ -303,19 +303,6 @@ void DisplayBuffer::do_update() { (*this->writer_)(*this); } } -const char *DisplayBuffer::rotation_str_() { - switch (this->rotation_) { - case DISPLAY_ROTATION_90_DEGREES: - return "90°"; - case DISPLAY_ROTATION_180_DEGREES: - return "180°"; - case DISPLAY_ROTATION_270_DEGREES: - return "270°"; - case DISPLAY_ROTATION_0_DEGREES: - default: - return "0°"; - } -} #ifdef USE_TIME void DisplayBuffer::strftime(int x, int y, Font *font, int color, TextAlign align, const char *format, time::EsphomelibTime time) { diff --git a/src/esphomelib/display/display.h b/src/esphomelib/display/display.h index f5373588..658d4f02 100644 --- a/src/esphomelib/display/display.h +++ b/src/esphomelib/display/display.h @@ -73,9 +73,9 @@ namespace display { enum DisplayRotation { DISPLAY_ROTATION_0_DEGREES = 0, - DISPLAY_ROTATION_90_DEGREES, - DISPLAY_ROTATION_180_DEGREES, - DISPLAY_ROTATION_270_DEGREES, + DISPLAY_ROTATION_90_DEGREES = 90, + DISPLAY_ROTATION_180_DEGREES = 180, + DISPLAY_ROTATION_270_DEGREES = 270, }; class Font; @@ -84,6 +84,13 @@ class DisplayBuffer; using display_writer_t = std::function; +#define LOG_DISPLAY(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type); \ + ESP_LOGCONFIG(TAG, prefix " Rotations: %d °", obj->rotation_); \ + ESP_LOGCONFIG(TAG, prefix " Dimensions: %dpx x %dpx", obj->get_width(), obj->get_height()); \ + } + class DisplayBuffer { public: /// Fill the entire screen with the given color. @@ -287,8 +294,6 @@ class DisplayBuffer { void do_update(); - const char *rotation_str_(); - uint8_t *buffer_{nullptr}; DisplayRotation rotation_{DISPLAY_ROTATION_0_DEGREES}; optional writer_{}; diff --git a/src/esphomelib/display/ssd1306.cpp b/src/esphomelib/display/ssd1306.cpp index ec0b394c..cb471431 100644 --- a/src/esphomelib/display/ssd1306.cpp +++ b/src/esphomelib/display/ssd1306.cpp @@ -252,13 +252,12 @@ void SPISSD1306::setup() { SSD1306::setup(); } void SPISSD1306::dump_config() { - ESP_LOGCONFIG(TAG, "SPI SSD1306:"); + LOG_DISPLAY("", "SPI SSD1306", this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " External VCC: %s", YESNO(this->external_vcc_)); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_UPDATE_INTERVAL(this); } void SPISSD1306::command(uint8_t value) { @@ -312,12 +311,11 @@ void I2CSSD1306::setup() { SSD1306::setup(); } void I2CSSD1306::dump_config() { - ESP_LOGCONFIG(TAG, "I2C SSD1306:"); + LOG_DISPLAY("", "I2C SSD1306", this); LOG_I2C_DEVICE(this); ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_()); LOG_PIN(" Reset Pin: ", this->reset_pin_); ESP_LOGCONFIG(TAG, " External VCC: %s", YESNO(this->external_vcc_)); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_UPDATE_INTERVAL(this); if (this->error_code_ == COMMUNICATION_FAILED) { diff --git a/src/esphomelib/display/waveshare_epaper.cpp b/src/esphomelib/display/waveshare_epaper.cpp index 0552605d..ba41a219 100644 --- a/src/esphomelib/display/waveshare_epaper.cpp +++ b/src/esphomelib/display/waveshare_epaper.cpp @@ -170,7 +170,7 @@ void WaveshareEPaperTypeA::setup() { this->data(0x03); // from top left to bottom right } void WaveshareEPaperTypeA::dump_config() { - ESP_LOGCONFIG(TAG, "Waveshare E-Paper:"); + LOG_DISPLAY("", "Waveshare E-Paper", this); switch (this->model_) { case WAVESHARE_EPAPER_1_54_IN: ESP_LOGCONFIG(TAG, " Model: 1.54in"); @@ -183,7 +183,6 @@ void WaveshareEPaperTypeA::dump_config() { break; } ESP_LOGCONFIG(TAG, " Full Update Every: %u", this->full_update_every_); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Busy Pin: ", this->busy_pin_); @@ -457,9 +456,8 @@ int WaveshareEPaper2P7In::get_height_internal_() { WaveshareEPaper2P7In::WaveshareEPaper2P7In(SPIComponent *parent, GPIOPin *cs, GPIOPin *dc_pin, uint32_t update_interval) : WaveshareEPaper(parent, cs, dc_pin, update_interval) {} void WaveshareEPaper2P7In::dump_config() { - ESP_LOGCONFIG(TAG, "Waveshare E-Paper:"); + LOG_DISPLAY("", "Waveshare E-Paper", this); ESP_LOGCONFIG(TAG, " Model: 2.7in"); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Busy Pin: ", this->busy_pin_); @@ -590,9 +588,8 @@ int WaveshareEPaper4P2In::get_height_internal_() { WaveshareEPaper4P2In::WaveshareEPaper4P2In(SPIComponent *parent, GPIOPin *cs, GPIOPin *dc_pin, uint32_t update_interval) : WaveshareEPaper(parent, cs, dc_pin, update_interval) {} void WaveshareEPaper4P2In::dump_config() { - ESP_LOGCONFIG(TAG, "Waveshare E-Paper:"); + LOG_DISPLAY("", "Waveshare E-Paper", this); ESP_LOGCONFIG(TAG, " Model: 4.2in"); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Busy Pin: ", this->busy_pin_); @@ -678,9 +675,8 @@ int WaveshareEPaper7P5In::get_height_internal_() { WaveshareEPaper7P5In::WaveshareEPaper7P5In(SPIComponent *parent, GPIOPin *cs, GPIOPin *dc_pin, uint32_t update_interval) : WaveshareEPaper(parent, cs, dc_pin, update_interval) {} void WaveshareEPaper7P5In::dump_config() { - ESP_LOGCONFIG(TAG, "Waveshare E-Paper:"); + LOG_DISPLAY("", "Waveshare E-Paper", this); ESP_LOGCONFIG(TAG, " Model: 7.5in"); - ESP_LOGCONFIG(TAG, " Rotation: %s", this->rotation_str_()); LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" Busy Pin: ", this->busy_pin_); diff --git a/src/esphomelib/esp32_ble_tracker.cpp b/src/esphomelib/esp32_ble_tracker.cpp index f09a8fc9..5de56634 100644 --- a/src/esphomelib/esp32_ble_tracker.cpp +++ b/src/esphomelib/esp32_ble_tracker.cpp @@ -742,6 +742,25 @@ void ESP32BLETracker::set_scan_interval(uint32_t scan_interval) { uint32_t ESP32BLETracker::get_scan_interval() const { return this->scan_interval_; } +void ESP32BLETracker::dump_config() { + ESP_LOGCONFIG(TAG, "BLE Tracker:"); + ESP_LOGCONFIG(TAG, " Scan Interval: %u s", this->scan_interval_); + for (auto *child : this->presence_sensors_) { + LOG_BINARY_SENSOR(" ", "Presence", child); + } + for (auto *child : this->rssi_sensors_) { + LOG_SENSOR(" ", "RSSI", child); + } + for (auto *child : this->xiaomi_devices_) { + ESP_LOGCONFIG(TAG, " Xiaomi %s", child->unique_id().c_str()); + LOG_SENSOR(" ", "Temperature ", child->get_temperature_sensor()); + LOG_SENSOR(" ", "Humidity ", child->get_humidity_sensor()); + LOG_SENSOR(" ", "Moisture ", child->get_moisture_sensor()); + LOG_SENSOR(" ", "Illuminance ", child->get_illuminance_sensor()); + LOG_SENSOR(" ", "Conductivity ", child->get_conductivity_sensor()); + LOG_SENSOR(" ", "Battery Level ", child->get_battery_level_sensor()); + } +} std::string ESP32BLERSSISensor::unit_of_measurement() { return "dB"; diff --git a/src/esphomelib/esp32_ble_tracker.h b/src/esphomelib/esp32_ble_tracker.h index 725865bd..41105878 100644 --- a/src/esphomelib/esp32_ble_tracker.h +++ b/src/esphomelib/esp32_ble_tracker.h @@ -85,6 +85,7 @@ class ESP32BLETracker : public Component { // (In most use cases you won't need these) /// Setup the FreeRTOS task and the Bluetooth stack. void setup() override; + void dump_config() override; void loop() override; diff --git a/src/esphomelib/fan/basic_fan_component.cpp b/src/esphomelib/fan/basic_fan_component.cpp index 98b1b1fa..6fa60ff6 100644 --- a/src/esphomelib/fan/basic_fan_component.cpp +++ b/src/esphomelib/fan/basic_fan_component.cpp @@ -42,15 +42,15 @@ void BasicFanComponent::setup() { } void BasicFanComponent::dump_config() { ESP_LOGCONFIG(TAG, "Fan '%s':", this->state_->get_name().c_str()); - if (this->oscillating_output_ != nullptr) { + if (this->state_->get_traits().supports_oscillation()) { ESP_LOGCONFIG(TAG, " Oscillation: YES"); } - if (this->binary_output_ == nullptr) { - ESP_LOGCONFIG(TAG, " Mode: Binary"); - } else { + if (this->state_->get_traits().supports_speed()) { ESP_LOGCONFIG(TAG, " Mode: Speed"); ESP_LOGCONFIG(TAG, " Speeds: Low=%.0f%% Medium=%.0f%% High=%.0f%%", - this->low_speed_ * 100.0f, this->medium_speed_ * 100.0f, this->high_speed_ * 100.0f); + this->low_speed_ * 100.0f, this->medium_speed_ * 100.0f, this->high_speed_ * 100.0f); + } else { + ESP_LOGCONFIG(TAG, " Mode: Binary"); } } void BasicFanComponent::loop() { diff --git a/src/esphomelib/light/light_state.cpp b/src/esphomelib/light/light_state.cpp index 23e5c557..3ffddb9d 100644 --- a/src/esphomelib/light/light_state.cpp +++ b/src/esphomelib/light/light_state.cpp @@ -265,6 +265,17 @@ LightState::StateCall LightState::make_call() { uint32_t LightState::hash_base_() { return 1114400283; } +void LightState::dump_config() { + ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str()); + if (this->get_traits().has_brightness()) { + ESP_LOGCONFIG(TAG, " Default Transition Length: %u ms", this->default_transition_length_); + ESP_LOGCONFIG(TAG, " Gamma Correct: %.2f", this->gamma_correct_); + } + if (this->get_traits().has_color_temperature()) { + ESP_LOGCONFIG(TAG, " Min Mireds: %.1f", this->get_traits().get_min_mireds()); + ESP_LOGCONFIG(TAG, " Max Mireds: %.1f", this->get_traits().get_max_mireds()); + } +} LightState::StateCall &LightState::StateCall::set_state(bool state) { this->binary_state_ = state; diff --git a/src/esphomelib/light/light_state.h b/src/esphomelib/light/light_state.h index fd7827fb..357e29db 100644 --- a/src/esphomelib/light/light_state.h +++ b/src/esphomelib/light/light_state.h @@ -153,6 +153,7 @@ class LightState : public Nameable, public Component { // (In most use cases you won't need these) /// Load state from preferences void setup() override; + void dump_config() override; void loop() override; /// Shortly after HARDWARE. float get_setup_priority() const override; diff --git a/src/esphomelib/log_component.cpp b/src/esphomelib/log_component.cpp index 298e57f0..33d55616 100644 --- a/src/esphomelib/log_component.cpp +++ b/src/esphomelib/log_component.cpp @@ -57,11 +57,16 @@ void LogComponent::pre_setup() { global_log_component = this; #ifdef ARDUINO_ARCH_ESP32 esp_log_set_vprintf(esp_idf_log_vprintf_); - esp_log_level_t log_level; if (this->global_log_level_ >= ESPHOMELIB_LOG_LEVEL_VERBOSE) { esp_log_level_set("*", ESP_LOG_VERBOSE); } #endif +#ifdef ARDUINO_ARCH_ESP8266 + if (this->global_log_level_ >= ESPHOMELIB_LOG_LEVEL_VERBOSE) { + if (this->baud_rate_ > 0) + Serial.setDebugOutput(true); + } +#endif ESP_LOGI(TAG, "Log initialized"); } diff --git a/src/esphomelib/mqtt/mqtt_client_component.h b/src/esphomelib/mqtt/mqtt_client_component.h index f0dc2482..b7558225 100644 --- a/src/esphomelib/mqtt/mqtt_client_component.h +++ b/src/esphomelib/mqtt/mqtt_client_component.h @@ -262,7 +262,7 @@ class MQTTClientComponent : public Component { /// The discovery info options for Home Assistant. Undefined optional means /// default and empty prefix means disabled. MQTTDiscoveryInfo discovery_info_{ - .prefix = "api", + .prefix = "homeassistant", .retain = true }; std::string topic_prefix_{}; diff --git a/src/esphomelib/remote/remote_receiver.cpp b/src/esphomelib/remote/remote_receiver.cpp index b5594715..7b2ed9e2 100644 --- a/src/esphomelib/remote/remote_receiver.cpp +++ b/src/esphomelib/remote/remote_receiver.cpp @@ -179,6 +179,10 @@ void RemoteReceiverComponent::dump_config() { if (this->is_failed()) { ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_)); } + + for (auto *child : this->decoders_) { + LOG_BINARY_SENSOR(" ", "Binary Sensor", child); + } } void RemoteReceiverComponent::loop() { @@ -324,6 +328,10 @@ void RemoteReceiverComponent::dump_config() { ESP_LOGCONFIG(TAG, " Tolerance: %u%%", this->tolerance_); ESP_LOGCONFIG(TAG, " Filter out pulses shorter than: %u us", this->filter_us_); ESP_LOGCONFIG(TAG, " Signal is done after %u us of no changes", this->idle_us_); + + for (auto *child : this->decoders_) { + LOG_BINARY_SENSOR(" ", "Binary Sensor", child); + } } void RemoteReceiverComponent::loop() { diff --git a/src/esphomelib/remote/remote_transmitter.cpp b/src/esphomelib/remote/remote_transmitter.cpp index d06ae5ad..cb392467 100644 --- a/src/esphomelib/remote/remote_transmitter.cpp +++ b/src/esphomelib/remote/remote_transmitter.cpp @@ -103,13 +103,16 @@ void RemoteTransmitterComponent::dump_config() { LOG_PIN(" Pin: ", this->pin_); if (this->current_carrier_frequency_ != 0 && this->carrier_duty_percent_ != 100) { - ESP_LOGCONFIG(TAG, " Carrier Frequency: %uHz", this->current_carrier_frequency_); ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_); } if (this->is_failed()) { ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_)); } + + for (auto *child : this->transmitters_) { + LOG_SWITCH(" ", "Transmitter", child); + } } void RemoteTransmitterComponent::configure_rmt() { @@ -224,6 +227,10 @@ void RemoteTransmitterComponent::dump_config() { ESP_LOGCONFIG(TAG, "Remote Transmitter..."); ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_); LOG_PIN(" Pin: ", this->pin_); + + for (auto *child : this->transmitters_) { + LOG_SWITCH(" ", "Transmitter", child); + } } void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequency, diff --git a/src/esphomelib/sensor/adc.cpp b/src/esphomelib/sensor/adc.cpp index 4562b8ae..3f8987b2 100644 --- a/src/esphomelib/sensor/adc.cpp +++ b/src/esphomelib/sensor/adc.cpp @@ -33,7 +33,7 @@ void ADCSensorComponent::setup() { #endif } void ADCSensorComponent::dump_config() { - ESP_LOGCONFIG(TAG, "ADC '%s':", this->get_name().c_str()); + LOG_SENSOR("", "ADC Sensor", this); #ifdef ARDUINO_ARCH_ESP8266 #ifdef USE_ADC_SENSOR_VCC ESP_LOGCONFIG(TAG, " Pin: VCC"); diff --git a/src/esphomelib/sensor/ads1115_component.cpp b/src/esphomelib/sensor/ads1115_component.cpp index 9d55041d..b09ed186 100644 --- a/src/esphomelib/sensor/ads1115_component.cpp +++ b/src/esphomelib/sensor/ads1115_component.cpp @@ -76,7 +76,7 @@ void ADS1115Component::dump_config() { } for (auto *sensor : this->sensors_) { - ESP_LOGCONFIG(TAG, " Sensor %s", sensor->get_name().c_str()); + LOG_SENSOR(" ", "Sensor", sensor); ESP_LOGCONFIG(TAG, " Multiplexer: %u", sensor->get_multiplexer()); ESP_LOGCONFIG(TAG, " Gain: %u", sensor->get_gain()); } diff --git a/src/esphomelib/sensor/bh1750_sensor.cpp b/src/esphomelib/sensor/bh1750_sensor.cpp index bbe3daa7..df5faead 100644 --- a/src/esphomelib/sensor/bh1750_sensor.cpp +++ b/src/esphomelib/sensor/bh1750_sensor.cpp @@ -30,7 +30,7 @@ void BH1750Sensor::setup() { } } void BH1750Sensor::dump_config() { - ESP_LOGCONFIG(TAG, "BH1750 '%s':", this->name_.c_str()); + LOG_SENSOR("", "BH1750", this); LOG_I2C_DEVICE(this); if (this->is_failed()) { ESP_LOGE(TAG, "Communication with BH1750 failed!"); diff --git a/src/esphomelib/sensor/bme280_component.cpp b/src/esphomelib/sensor/bme280_component.cpp index 1ffcb763..e3096ec8 100644 --- a/src/esphomelib/sensor/bme280_component.cpp +++ b/src/esphomelib/sensor/bme280_component.cpp @@ -161,12 +161,15 @@ void BME280Component::dump_config() { default: break; } - - ESP_LOGCONFIG(TAG, " Temperature Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); - ESP_LOGCONFIG(TAG, " Pressure Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); - ESP_LOGCONFIG(TAG, " Humidity Oversampling: %s", oversampling_to_str(this->humidity_oversampling_)); - ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_)); + ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_)); LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); + LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->humidity_oversampling_)); } float BME280Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/bme680_component.cpp b/src/esphomelib/sensor/bme680_component.cpp index 512095d2..7cc63d1e 100644 --- a/src/esphomelib/sensor/bme680_component.cpp +++ b/src/esphomelib/sensor/bme680_component.cpp @@ -210,18 +210,22 @@ void BME680Component::dump_config() { if (this->is_failed()) { ESP_LOGE(TAG, "Communication with BME680 failed!"); } - - ESP_LOGCONFIG(TAG, " Temperature Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); - ESP_LOGCONFIG(TAG, " Pressure Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); - ESP_LOGCONFIG(TAG, " Humidity Oversampling: %s", oversampling_to_str(this->humidity_oversampling_)); ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_)); + LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); + LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->humidity_oversampling_)); + LOG_SENSOR(" ", "Gas Resistance", this->gas_resistance_sensor_); if (this->heater_duration_ == 0 || this->heater_temperature_ == 0) { ESP_LOGCONFIG(TAG, " Heater OFF"); } else { ESP_LOGCONFIG(TAG, " Heater temperature=%u°C duration=%ums", this->heater_temperature_, this->heater_duration_); } - LOG_UPDATE_INTERVAL(this); } float BME680Component::get_setup_priority() const { diff --git a/src/esphomelib/sensor/bmp085_component.cpp b/src/esphomelib/sensor/bmp085_component.cpp index 7babe87b..7846b636 100644 --- a/src/esphomelib/sensor/bmp085_component.cpp +++ b/src/esphomelib/sensor/bmp085_component.cpp @@ -58,6 +58,9 @@ void BMP085Component::dump_config() { ESP_LOGE(TAG, "Connection with BMP085 failed!"); } LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Pressure", this->pressure_); } BMP085Component::BMP085Component(I2CComponent *parent, const std::string &temperature_name, const std::string &pressure_name, diff --git a/src/esphomelib/sensor/bmp280_component.cpp b/src/esphomelib/sensor/bmp280_component.cpp index 7378721d..1c266f00 100644 --- a/src/esphomelib/sensor/bmp280_component.cpp +++ b/src/esphomelib/sensor/bmp280_component.cpp @@ -115,10 +115,13 @@ void BMP280Component::dump_config() { default: break; } + ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_)); + LOG_UPDATE_INTERVAL(this); - ESP_LOGCONFIG(TAG, " Temperature Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); - ESP_LOGCONFIG(TAG, " Pressure Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); - ESP_LOGCONFIG(TAG, " IIR Filter: %s", iir_filter_to_str(this->iir_filter_)); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->temperature_oversampling_)); + LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); + ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_)); } float BMP280Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/cse7766.cpp b/src/esphomelib/sensor/cse7766.cpp index eab61ac3..6ab22c70 100644 --- a/src/esphomelib/sensor/cse7766.cpp +++ b/src/esphomelib/sensor/cse7766.cpp @@ -181,6 +181,9 @@ CSE7766PowerSensor *CSE7766Component::make_power_sensor(const std::string &name) void CSE7766Component::dump_config() { ESP_LOGCONFIG(TAG, "CSE7766:"); LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Voltage", this->voltage_); + LOG_SENSOR(" ", "Current", this->current_); + LOG_SENSOR(" ", "Power", this->power_); } } // namespace sensor diff --git a/src/esphomelib/sensor/custom_sensor.cpp b/src/esphomelib/sensor/custom_sensor.cpp index df260ef9..9bb55ee2 100644 --- a/src/esphomelib/sensor/custom_sensor.cpp +++ b/src/esphomelib/sensor/custom_sensor.cpp @@ -3,17 +3,25 @@ #ifdef USE_CUSTOM_SENSOR #include "esphomelib/sensor/custom_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { +static const char *TAG = "sensor.custom"; + CustomSensorConstructor::CustomSensorConstructor(const std::function()> &init) { this->sensors_ = init(); } Sensor *CustomSensorConstructor::get_sensor(int i) { return this->sensors_[i]; } +void CustomSensorConstructor::dump_config() { + for (auto *child : this->sensors_) { + LOG_SENSOR("", "Custom Sensor", child); + } +} } // namespace sensor diff --git a/src/esphomelib/sensor/custom_sensor.h b/src/esphomelib/sensor/custom_sensor.h index 4cb0ff4f..eac110ec 100644 --- a/src/esphomelib/sensor/custom_sensor.h +++ b/src/esphomelib/sensor/custom_sensor.h @@ -5,18 +5,21 @@ #ifdef USE_CUSTOM_SENSOR +#include "esphomelib/component.h" #include "esphomelib/sensor/sensor.h" ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { -class CustomSensorConstructor { +class CustomSensorConstructor : public Component { public: CustomSensorConstructor(const std::function()> &init); Sensor *get_sensor(int i); + void dump_config() override; + protected: std::vector sensors_; }; diff --git a/src/esphomelib/sensor/dallas_component.cpp b/src/esphomelib/sensor/dallas_component.cpp index ebb3db5b..3f171479 100644 --- a/src/esphomelib/sensor/dallas_component.cpp +++ b/src/esphomelib/sensor/dallas_component.cpp @@ -78,6 +78,7 @@ void DallasComponent::setup() { void DallasComponent::dump_config() { ESP_LOGCONFIG(TAG, "DallasComponent:"); LOG_PIN(" Pin: ", this->one_wire_->get_pin()); + LOG_UPDATE_INTERVAL(this); if (this->found_sensors_.empty()) { ESP_LOGW(TAG, " Found no sensors!"); @@ -90,7 +91,7 @@ void DallasComponent::dump_config() { } for (auto sensor : this->sensors_) { - ESP_LOGCONFIG(TAG, " Device '%s':", sensor->get_name().c_str()); + LOG_SENSOR(" ", "Device", sensor); if (sensor->get_index().has_value()) { ESP_LOGCONFIG(TAG, " Index %u", *sensor->get_index()); if (*sensor->get_index() >= this->found_sensors_.size()) { diff --git a/src/esphomelib/sensor/dht12_component.cpp b/src/esphomelib/sensor/dht12_component.cpp index ee64364a..e3d4258c 100644 --- a/src/esphomelib/sensor/dht12_component.cpp +++ b/src/esphomelib/sensor/dht12_component.cpp @@ -61,6 +61,8 @@ void DHT12Component::dump_config() { if (this->is_failed()) { ESP_LOGE(TAG, "Communication with DHT12 failed!"); } + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); } float DHT12Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/dht_component.cpp b/src/esphomelib/sensor/dht_component.cpp index 63a89fca..95aa36f6 100644 --- a/src/esphomelib/sensor/dht_component.cpp +++ b/src/esphomelib/sensor/dht_component.cpp @@ -43,6 +43,9 @@ void DHTComponent::dump_config() { } LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); } void DHTComponent::update() { diff --git a/src/esphomelib/sensor/duty_cycle_sensor.cpp b/src/esphomelib/sensor/duty_cycle_sensor.cpp index ddd33131..9efa31c0 100644 --- a/src/esphomelib/sensor/duty_cycle_sensor.cpp +++ b/src/esphomelib/sensor/duty_cycle_sensor.cpp @@ -45,7 +45,7 @@ void DutyCycleSensor::setup() { attachInterrupt(this->pin_->get_pin(), gpio_intr, CHANGE); } void DutyCycleSensor::dump_config() { - ESP_LOGCONFIG(TAG, "Duty Cycle Sensor '%s':", this->name_.c_str()); + LOG_SENSOR("", "Duty Cycle Sensor", this); LOG_PIN(" Pin: ", this->pin_); LOG_UPDATE_INTERVAL(this); } diff --git a/src/esphomelib/sensor/esp32_hall_sensor.cpp b/src/esphomelib/sensor/esp32_hall_sensor.cpp index 9d0f0f47..65f9371e 100644 --- a/src/esphomelib/sensor/esp32_hall_sensor.cpp +++ b/src/esphomelib/sensor/esp32_hall_sensor.cpp @@ -4,18 +4,22 @@ #include "esphomelib/sensor/esp32_hall_sensor.h" #include "esphomelib/esphal.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { +static const char *TAG = "sensor.esp32_hall"; + ESP32HallSensor::ESP32HallSensor(const std::string &name, uint32_t update_interval) : PollingSensorComponent(name, update_interval) { } void ESP32HallSensor::update() { - float value = hallRead() / 4095.0f; - this->publish_state(value * 10000.0f); + float value = (hallRead() / 4095.0f) * 10000.0f; + ESP_LOGCONFIG(TAG, "'%s': Got reading %.0f µT", this->name_.c_str(), value); + this->publish_state(value); } std::string ESP32HallSensor::unit_of_measurement() { return "µT"; @@ -29,6 +33,9 @@ int8_t ESP32HallSensor::accuracy_decimals() { std::string ESP32HallSensor::unique_id() { return get_mac_address() + "-hall"; } +void ESP32HallSensor::dump_config() { + LOG_SENSOR("", "ESP32 Hall Sensor", this); +} } // namespace sensor diff --git a/src/esphomelib/sensor/esp32_hall_sensor.h b/src/esphomelib/sensor/esp32_hall_sensor.h index 3437a0c2..19321966 100644 --- a/src/esphomelib/sensor/esp32_hall_sensor.h +++ b/src/esphomelib/sensor/esp32_hall_sensor.h @@ -15,6 +15,8 @@ class ESP32HallSensor : public PollingSensorComponent { public: explicit ESP32HallSensor(const std::string &name, uint32_t update_interval = 15000); + void dump_config() override; + void update() override; std::string unit_of_measurement() override; diff --git a/src/esphomelib/sensor/hdc1080_component.cpp b/src/esphomelib/sensor/hdc1080_component.cpp index 5af32483..e5111ef7 100644 --- a/src/esphomelib/sensor/hdc1080_component.cpp +++ b/src/esphomelib/sensor/hdc1080_component.cpp @@ -48,6 +48,8 @@ void HDC1080Component::dump_config() { ESP_LOGE(TAG, "Communication with HDC1080 failed!"); } LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Humidity", this->humidity_); } void HDC1080Component::update() { uint16_t raw_temp; diff --git a/src/esphomelib/sensor/hlw8012.cpp b/src/esphomelib/sensor/hlw8012.cpp index e5d0f2f2..ac10f5f9 100644 --- a/src/esphomelib/sensor/hlw8012.cpp +++ b/src/esphomelib/sensor/hlw8012.cpp @@ -39,6 +39,9 @@ void HLW8012Component::dump_config() { ESP_LOGCONFIG(TAG, " Current resistor: %.1f mΩ", this->current_resistor_ * 1000.0f); ESP_LOGCONFIG(TAG, " Voltage Divider: %.1f", this->voltage_divider_); LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Voltage", this->voltage_sensor_); + LOG_SENSOR(" ", "Current", this->current_sensor_); + LOG_SENSOR(" ", "Power", this->power_sensor_); } float HLW8012Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/hmc5883l.cpp b/src/esphomelib/sensor/hmc5883l.cpp index bea21e94..bbd725de 100644 --- a/src/esphomelib/sensor/hmc5883l.cpp +++ b/src/esphomelib/sensor/hmc5883l.cpp @@ -81,8 +81,12 @@ void HMC5883LComponent::dump_config() { } else if (this->error_code_ == ID_REGISTERS) { ESP_LOGE(TAG, "The ID registers don't match - Is this really an HMC5883L?"); } - LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "X Axis", this->x_sensor_); + LOG_SENSOR(" ", "Y Axis", this->y_sensor_); + LOG_SENSOR(" ", "Z Axis", this->z_sensor_); + LOG_SENSOR(" ", "Heading", this->heading_sensor_); } float HMC5883LComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/homeassistant_sensor.cpp b/src/esphomelib/sensor/homeassistant_sensor.cpp index d4e20e92..6130272c 100644 --- a/src/esphomelib/sensor/homeassistant_sensor.cpp +++ b/src/esphomelib/sensor/homeassistant_sensor.cpp @@ -14,7 +14,10 @@ static const char *TAG = "sensor.homeassistant"; HomeassistantSensor::HomeassistantSensor(const std::string &name, const std::string &entity_id) : Sensor(name), entity_id_(entity_id) { - api::global_api_server->subscribe_home_assistant_state(entity_id, [this](std::string state) { + +} +void HomeassistantSensor::setup() { + api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) { char *end; float value = ::strtof(state.c_str(), &end); if (end == nullptr) { @@ -27,6 +30,13 @@ HomeassistantSensor::HomeassistantSensor(const std::string &name, const std::str this->publish_state(value); }); } +void HomeassistantSensor::dump_config() { + LOG_SENSOR("", "Homeassistant Sensor", this); + ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str()); +} +float HomeassistantSensor::get_setup_priority() const { + return setup_priority::WIFI; +} } // namespace sensor diff --git a/src/esphomelib/sensor/homeassistant_sensor.h b/src/esphomelib/sensor/homeassistant_sensor.h index b305a1b3..6481e82a 100644 --- a/src/esphomelib/sensor/homeassistant_sensor.h +++ b/src/esphomelib/sensor/homeassistant_sensor.h @@ -12,9 +12,12 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { -class HomeassistantSensor : public Sensor { +class HomeassistantSensor : public Sensor, public Component { public: HomeassistantSensor(const std::string &name, const std::string &entity_id); + void setup() override; + void dump_config() override; + float get_setup_priority() const override; protected: std::string entity_id_; }; diff --git a/src/esphomelib/sensor/htu21d_component.cpp b/src/esphomelib/sensor/htu21d_component.cpp index 6272228c..e3dd1220 100644 --- a/src/esphomelib/sensor/htu21d_component.cpp +++ b/src/esphomelib/sensor/htu21d_component.cpp @@ -47,6 +47,8 @@ void HTU21DComponent::dump_config() { ESP_LOGE(TAG, "Communication with HTU21D failed!"); } LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Temperature", this->temperature_); + LOG_SENSOR(" ", "Humidity", this->humidity_); } void HTU21DComponent::update() { uint16_t raw_temperature; diff --git a/src/esphomelib/sensor/hx711.cpp b/src/esphomelib/sensor/hx711.cpp index 63601470..6ca989dd 100644 --- a/src/esphomelib/sensor/hx711.cpp +++ b/src/esphomelib/sensor/hx711.cpp @@ -21,7 +21,7 @@ void HX711Sensor::setup() { } void HX711Sensor::dump_config() { - ESP_LOGCONFIG(TAG, "HX711 '%s':", this->name_.c_str()); + LOG_SENSOR("", "HX711", this); LOG_PIN(" DOUT Pin: ", this->dout_pin_); LOG_PIN(" SCK Pin: ", this->sck_pin_); LOG_UPDATE_INTERVAL(this); diff --git a/src/esphomelib/sensor/ina219.cpp b/src/esphomelib/sensor/ina219.cpp index 7088ca91..93b5acc5 100644 --- a/src/esphomelib/sensor/ina219.cpp +++ b/src/esphomelib/sensor/ina219.cpp @@ -143,8 +143,12 @@ void INA219Component::dump_config() { this->mark_failed(); return; } - LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_); + LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_); + LOG_SENSOR(" ", "Current", this->current_sensor_); + LOG_SENSOR(" ", "Power", this->power_sensor_); } float INA219Component::get_setup_priority() const { diff --git a/src/esphomelib/sensor/ina3221.cpp b/src/esphomelib/sensor/ina3221.cpp index 0e25fd6e..ecb86717 100644 --- a/src/esphomelib/sensor/ina3221.cpp +++ b/src/esphomelib/sensor/ina3221.cpp @@ -66,8 +66,20 @@ void INA3221Component::dump_config() { if (this->is_failed()) { ESP_LOGE(TAG, "Communication with INA3221 failed!"); } - LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Bus Voltage #1", this->channels[0].bus_voltage_sensor_); + LOG_SENSOR(" ", "Shunt Voltage #1", this->channels[0].shunt_voltage_sensor_); + LOG_SENSOR(" ", "Current #1", this->channels[0].current_sensor_); + LOG_SENSOR(" ", "Power #1", this->channels[0].power_sensor_); + LOG_SENSOR(" ", "Bus Voltage #2", this->channels[1].bus_voltage_sensor_); + LOG_SENSOR(" ", "Shunt Voltage #2", this->channels[1].shunt_voltage_sensor_); + LOG_SENSOR(" ", "Current #2", this->channels[1].current_sensor_); + LOG_SENSOR(" ", "Power #2", this->channels[1].power_sensor_); + LOG_SENSOR(" ", "Bus Voltage #3", this->channels[2].bus_voltage_sensor_); + LOG_SENSOR(" ", "Shunt Voltage #3", this->channels[2].shunt_voltage_sensor_); + LOG_SENSOR(" ", "Current #3", this->channels[2].current_sensor_); + LOG_SENSOR(" ", "Power #3", this->channels[2].power_sensor_); } inline uint8_t ina3221_bus_voltage_register(int channel) { diff --git a/src/esphomelib/sensor/max31855_sensor.cpp b/src/esphomelib/sensor/max31855_sensor.cpp index 1fa6d380..169a30eb 100644 --- a/src/esphomelib/sensor/max31855_sensor.cpp +++ b/src/esphomelib/sensor/max31855_sensor.cpp @@ -29,7 +29,7 @@ void MAX31855Sensor::setup() { this->spi_setup(); } void MAX31855Sensor::dump_config() { - ESP_LOGCONFIG(TAG, "MAX31855 '%s':", this->name_.c_str()); + LOG_SENSOR("", "MAX31855", this); LOG_PIN(" CS Pin: ", this->cs_); LOG_UPDATE_INTERVAL(this); } diff --git a/src/esphomelib/sensor/max6675_sensor.cpp b/src/esphomelib/sensor/max6675_sensor.cpp index 293f4fb0..082da07f 100644 --- a/src/esphomelib/sensor/max6675_sensor.cpp +++ b/src/esphomelib/sensor/max6675_sensor.cpp @@ -29,7 +29,7 @@ void MAX6675Sensor::setup() { this->spi_setup(); } void MAX6675Sensor::dump_config() { - ESP_LOGCONFIG(TAG, "MAX6675 '%s':", this->name_.c_str()); + LOG_SENSOR("", "MAX6675", this); LOG_PIN(" CS Pin: ", this->cs_); LOG_UPDATE_INTERVAL(this); } diff --git a/src/esphomelib/sensor/mhz19_component.cpp b/src/esphomelib/sensor/mhz19_component.cpp index 88016afc..0997e862 100644 --- a/src/esphomelib/sensor/mhz19_component.cpp +++ b/src/esphomelib/sensor/mhz19_component.cpp @@ -81,6 +81,11 @@ MHZ19CO2Sensor *MHZ19Component::get_co2_sensor() const { float MHZ19Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } +void MHZ19Component::dump_config() { + ESP_LOGCONFIG(TAG, "MH-Z19:"); + LOG_SENSOR(" ", "CO2", this->co2_sensor_); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); +} } // namespace sensor diff --git a/src/esphomelib/sensor/mhz19_component.h b/src/esphomelib/sensor/mhz19_component.h index 64e3cb83..262d1e8b 100644 --- a/src/esphomelib/sensor/mhz19_component.h +++ b/src/esphomelib/sensor/mhz19_component.h @@ -24,6 +24,7 @@ class MHZ19Component : public PollingComponent, public UARTDevice { float get_setup_priority() const override; void update() override; + void dump_config() override; MHZ19TemperatureSensor *make_temperature_sensor(const std::string &name); MHZ19CO2Sensor *get_co2_sensor() const; diff --git a/src/esphomelib/sensor/mpu6050_component.cpp b/src/esphomelib/sensor/mpu6050_component.cpp index 4b9e7ce0..f8ec0bc1 100644 --- a/src/esphomelib/sensor/mpu6050_component.cpp +++ b/src/esphomelib/sensor/mpu6050_component.cpp @@ -103,6 +103,13 @@ void MPU6050Component::dump_config() { ESP_LOGE(TAG, "Communication with MPU6050 failed!"); } LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Acceleration X", this->accel_x_sensor_); + LOG_SENSOR(" ", "Acceleration Y", this->accel_y_sensor_); + LOG_SENSOR(" ", "Acceleration Z", this->accel_z_sensor_); + LOG_SENSOR(" ", "Gyro X", this->gyro_x_sensor_); + LOG_SENSOR(" ", "Gyro Y", this->gyro_y_sensor_); + LOG_SENSOR(" ", "Gyro Z", this->gyro_z_sensor_); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); } void MPU6050Component::update() { diff --git a/src/esphomelib/sensor/mqtt_sensor_component.cpp b/src/esphomelib/sensor/mqtt_sensor_component.cpp index e3241e7f..78e092fd 100644 --- a/src/esphomelib/sensor/mqtt_sensor_component.cpp +++ b/src/esphomelib/sensor/mqtt_sensor_component.cpp @@ -30,14 +30,6 @@ void MQTTSensorComponent::dump_config() { if (this->get_expire_after() > 0) { ESP_LOGCONFIG(TAG, " Expire After: %us", this->get_expire_after() / 1000); } - ESP_LOGCONFIG(TAG, " Unit of Measurement: '%s'", this->sensor_->get_unit_of_measurement().c_str()); - ESP_LOGCONFIG(TAG, " Accuracy Decimals: %d", this->sensor_->get_accuracy_decimals()); - if (!this->sensor_->get_icon().empty()) { - ESP_LOGCONFIG(TAG, " Icon: '%s'", this->sensor_->get_icon().c_str()); - } - if (!this->sensor_->unique_id().empty()) { - ESP_LOGCONFIG(TAG, " Unique ID: '%s'", this->sensor_->unique_id().c_str()); - } LOG_MQTT_COMPONENT(true, false) } diff --git a/src/esphomelib/sensor/mqtt_subscribe_sensor.cpp b/src/esphomelib/sensor/mqtt_subscribe_sensor.cpp index 3f679339..74116be3 100644 --- a/src/esphomelib/sensor/mqtt_subscribe_sensor.cpp +++ b/src/esphomelib/sensor/mqtt_subscribe_sensor.cpp @@ -39,7 +39,7 @@ void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; } void MQTTSubscribeSensor::dump_config() { - ESP_LOGCONFIG(TAG, "MQTT Subscribe Sensor '%s':", this->name_.c_str()); + LOG_SENSOR("", "MQTT Subscribe", this); ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str()); } diff --git a/src/esphomelib/sensor/ms5611.cpp b/src/esphomelib/sensor/ms5611.cpp index 83942f5f..0a439c43 100644 --- a/src/esphomelib/sensor/ms5611.cpp +++ b/src/esphomelib/sensor/ms5611.cpp @@ -39,6 +39,8 @@ void MS5611Component::dump_config() { ESP_LOGE(TAG, "Communication with MS5611 failed!"); } LOG_UPDATE_INTERVAL(this); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Pressure", this->pressure_sensor_); } float MS5611Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/pmsx003.cpp b/src/esphomelib/sensor/pmsx003.cpp index 92412507..e958e13b 100644 --- a/src/esphomelib/sensor/pmsx003.cpp +++ b/src/esphomelib/sensor/pmsx003.cpp @@ -172,6 +172,15 @@ PMSX003Sensor *PMSX003Component::make_formaldehyde_sensor(const std::string &nam PMSX003Component::PMSX003Component(UARTComponent *parent, PMSX003Type type) : UARTDevice(parent), type_(type) { } +void PMSX003Component::dump_config() { + ESP_LOGCONFIG(TAG, "PMSX003:"); + LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_); + LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); + LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); + LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_); +} std::string PMSX003Sensor::unit_of_measurement() { switch (this->type_) { diff --git a/src/esphomelib/sensor/pmsx003.h b/src/esphomelib/sensor/pmsx003.h index da4f8937..ec6de0a2 100644 --- a/src/esphomelib/sensor/pmsx003.h +++ b/src/esphomelib/sensor/pmsx003.h @@ -53,6 +53,7 @@ class PMSX003Component : public UARTDevice, public Component { void loop() override; float get_setup_priority() const override; + void dump_config() override; PMSX003Sensor *make_pm_1_0_sensor(const std::string &name); PMSX003Sensor *make_pm_2_5_sensor(const std::string &name); diff --git a/src/esphomelib/sensor/pulse_counter.cpp b/src/esphomelib/sensor/pulse_counter.cpp index 580278bf..95ab62a8 100644 --- a/src/esphomelib/sensor/pulse_counter.cpp +++ b/src/esphomelib/sensor/pulse_counter.cpp @@ -167,7 +167,7 @@ void PulseCounterSensorComponent::setup() { } void PulseCounterSensorComponent::dump_config() { - ESP_LOGCONFIG(TAG, "Pulse Counter '%s':", this->name_.c_str()); + LOG_SENSOR("", "Pulse Counter", this); LOG_PIN(" Pin: ", this->pin_); ESP_LOGCONFIG(TAG, " Rising Edge: %s", EDGE_MODE_TO_STRING[this->rising_edge_mode_]); ESP_LOGCONFIG(TAG, " Falling Edge: %s", EDGE_MODE_TO_STRING[this->falling_edge_mode_]); diff --git a/src/esphomelib/sensor/rotary_encoder.cpp b/src/esphomelib/sensor/rotary_encoder.cpp index 39a7be9f..d188a5b9 100644 --- a/src/esphomelib/sensor/rotary_encoder.cpp +++ b/src/esphomelib/sensor/rotary_encoder.cpp @@ -79,8 +79,7 @@ static uint16_t state_lookup_table[32] = { RotaryEncoderSensor::RotaryEncoderSensor(const std::string &name, GPIOPin *pin_a, GPIOPin *pin_b) : Sensor(name), Component(), pin_a_(pin_a), pin_b_(pin_b) { - // Filters make no sense with this - this->clear_filters(); + } void RotaryEncoderSensor::setup() { ESP_LOGCONFIG(TAG, "Setting up Rotary Encoder '%s'...", this->name_.c_str()); @@ -99,7 +98,7 @@ void RotaryEncoderSensor::setup() { } void RotaryEncoderSensor::dump_config() { - ESP_LOGCONFIG(TAG, "Rotary Encoder '%s':", this->name_.c_str()); + LOG_SENSOR("", "Rotary Encoder", this); LOG_PIN(" Pin A: ", this->pin_a_); LOG_PIN(" Pin B: ", this->pin_b_); LOG_PIN(" Pin I: ", this->pin_i_); diff --git a/src/esphomelib/sensor/sensor.cpp b/src/esphomelib/sensor/sensor.cpp index 1e9bd9a6..a083a8e9 100644 --- a/src/esphomelib/sensor/sensor.cpp +++ b/src/esphomelib/sensor/sensor.cpp @@ -42,8 +42,7 @@ int8_t Sensor::accuracy_decimals() { } Sensor::Sensor(const std::string &name) : Nameable(name), state(NAN), raw_state(NAN) { - // By default, apply a smoothing over the last 15 values - this->add_filter(new SlidingWindowMovingAverageFilter(15, 15)); + } Sensor::Sensor() : Sensor("") { @@ -104,9 +103,9 @@ void Sensor::set_filters(const std::vector &filters) { this->add_filters(filters); } void Sensor::clear_filters() { - ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this); - // note: not deallocating here, it makes the code faster (no virtual destructor) - // plus this is not called too often. + if (this->filter_list_ != nullptr) { + ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this); + } this->filter_list_ = nullptr; } float Sensor::get_value() const { @@ -126,9 +125,11 @@ std::string Sensor::unique_id() { return ""; } void Sensor::send_state_to_frontend_internal_(float state) { this->has_state_ = true; this->state = state; - ESP_LOGD(TAG, "'%s': Sending state %.5f%s with %d decimals of accuracy", - this->get_name().c_str(), state, this->get_unit_of_measurement().c_str(), - this->get_accuracy_decimals()); + if (this->filter_list_ != nullptr) { + ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", + this->get_name().c_str(), state, this->get_unit_of_measurement().c_str(), + this->get_accuracy_decimals()); + } this->callback_.call(state); } SensorStateTrigger *Sensor::make_state_trigger() { diff --git a/src/esphomelib/sensor/sensor.h b/src/esphomelib/sensor/sensor.h index 57fb069c..eeeb806e 100644 --- a/src/esphomelib/sensor/sensor.h +++ b/src/esphomelib/sensor/sensor.h @@ -24,6 +24,19 @@ class ValueRangeTrigger; template class SensorInRangeCondition; +#define LOG_SENSOR(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \ + ESP_LOGCONFIG(TAG, prefix " Unit of Measurement: '%s'", obj->get_unit_of_measurement().c_str()); \ + ESP_LOGCONFIG(TAG, prefix " Accuracy Decimals: %d", obj->get_accuracy_decimals()); \ + if (!obj->get_icon().empty()) { \ + ESP_LOGCONFIG(TAG, prefix " Icon: '%s'", obj->get_icon().c_str()); \ + } \ + if (!obj->unique_id().empty()) { \ + ESP_LOGV(TAG, prefix " Unique ID: '%s'", obj->unique_id().c_str()); \ + } \ + } + /** Base-class for all sensors. * * A sensor has unit of measurement and can use publish_state to send out a new value with the specified accuracy. diff --git a/src/esphomelib/sensor/sht3xd_component.cpp b/src/esphomelib/sensor/sht3xd_component.cpp index 481e5ec1..953d0e61 100644 --- a/src/esphomelib/sensor/sht3xd_component.cpp +++ b/src/esphomelib/sensor/sht3xd_component.cpp @@ -59,6 +59,9 @@ void SHT3XDComponent::dump_config() { ESP_LOGE(TAG, "Communication with SHT3xD failed!"); } LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); + LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); } float SHT3XDComponent::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/tcs34725.cpp b/src/esphomelib/sensor/tcs34725.cpp index 1dd73352..be9a527e 100644 --- a/src/esphomelib/sensor/tcs34725.cpp +++ b/src/esphomelib/sensor/tcs34725.cpp @@ -57,6 +57,13 @@ void TCS34725Component::dump_config() { ESP_LOGE(TAG, "Communication with TCS34725 failed!"); } LOG_UPDATE_INTERVAL(this); + + LOG_SENSOR(" ", "Clear Channel", this->clear_sensor_); + LOG_SENSOR(" ", "Red Channel", this->red_sensor_); + LOG_SENSOR(" ", "Green Channel", this->green_sensor_); + LOG_SENSOR(" ", "Blue Channel", this->blue_sensor_); + LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_); + LOG_SENSOR(" ", "Color Temperature", this->color_temperature_sensor_); } float TCS34725Component::get_setup_priority() const { return setup_priority::HARDWARE_LATE; diff --git a/src/esphomelib/sensor/template_sensor.cpp b/src/esphomelib/sensor/template_sensor.cpp index eebe3d23..0b99d320 100644 --- a/src/esphomelib/sensor/template_sensor.cpp +++ b/src/esphomelib/sensor/template_sensor.cpp @@ -28,7 +28,7 @@ void TemplateSensor::set_template(std::function()> &&f) { this->f_ = std::move(f); } void TemplateSensor::dump_config() { - ESP_LOGCONFIG(TAG, "Template Sensor '%s':", this->name_.c_str()); + LOG_SENSOR("", "Template Sensor", this); LOG_UPDATE_INTERVAL(this); } diff --git a/src/esphomelib/sensor/total_daily_energy.cpp b/src/esphomelib/sensor/total_daily_energy.cpp index d9354596..f81a2d72 100644 --- a/src/esphomelib/sensor/total_daily_energy.cpp +++ b/src/esphomelib/sensor/total_daily_energy.cpp @@ -9,6 +9,8 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { +static const char *TAG = "sensor.total_daily_energy"; + void TotalDailyEnergy::setup() { this->pref_ = global_preferences.make_preference(this->get_object_id_hash()); @@ -23,6 +25,9 @@ void TotalDailyEnergy::setup() { auto f = std::bind(&TotalDailyEnergy::process_new_state_, this, std::placeholders::_1); this->parent_->add_on_state_callback(f); } +void TotalDailyEnergy::dump_config() { + LOG_SENSOR("", "Total Daily Energy", this); +} float TotalDailyEnergy::get_setup_priority() const { return setup_priority::HARDWARE_LATE; } @@ -69,7 +74,7 @@ void TotalDailyEnergy::publish_state_and_save_(float state) { } TotalDailyEnergy::TotalDailyEnergy(const std::string &name, time::RealTimeClockComponent *time, Sensor *parent) : Sensor(name), time_(time), parent_(parent) { - this->clear_filters(); + } } // namespace sensor diff --git a/src/esphomelib/sensor/total_daily_energy.h b/src/esphomelib/sensor/total_daily_energy.h index e21e653a..91853468 100644 --- a/src/esphomelib/sensor/total_daily_energy.h +++ b/src/esphomelib/sensor/total_daily_energy.h @@ -18,6 +18,7 @@ class TotalDailyEnergy : public Sensor, public Component { public: TotalDailyEnergy(const std::string &name, time::RealTimeClockComponent *time, Sensor *parent); void setup() override; + void dump_config() override; float get_setup_priority() const override; uint32_t update_interval() override; std::string unit_of_measurement() override; diff --git a/src/esphomelib/sensor/tsl2561_sensor.cpp b/src/esphomelib/sensor/tsl2561_sensor.cpp index c9f609e2..f18a2fa8 100644 --- a/src/esphomelib/sensor/tsl2561_sensor.cpp +++ b/src/esphomelib/sensor/tsl2561_sensor.cpp @@ -50,7 +50,7 @@ void TSL2561Sensor::setup() { this->tsl2561_write_byte(TSL2561_REGISTER_TIMING, timing); } void TSL2561Sensor::dump_config() { - ESP_LOGCONFIG(TAG, "TSL2561:"); + LOG_SENSOR("", "TSL2561", this); LOG_I2C_DEVICE(this); if (this->is_failed()) { diff --git a/src/esphomelib/sensor/ultrasonic_sensor.cpp b/src/esphomelib/sensor/ultrasonic_sensor.cpp index 465989cc..ebe37b02 100644 --- a/src/esphomelib/sensor/ultrasonic_sensor.cpp +++ b/src/esphomelib/sensor/ultrasonic_sensor.cpp @@ -27,7 +27,7 @@ void UltrasonicSensorComponent::setup() { this->trigger_pin_->digital_write(false); } void UltrasonicSensorComponent::dump_config() { - ESP_LOGCONFIG(TAG, "Ultrasonic Sensor '%s':", this->name_.c_str()); + LOG_SENSOR("", "Ultrasonic Sensor", this); LOG_PIN(" Echo Pin: ", this->echo_pin_); LOG_PIN(" Trigger Pin: ", this->trigger_pin_); ESP_LOGCONFIG(TAG, " Pulse time: %u µs", this->pulse_time_us_); diff --git a/src/esphomelib/sensor/uptime_sensor.cpp b/src/esphomelib/sensor/uptime_sensor.cpp index ef761c7e..32767170 100644 --- a/src/esphomelib/sensor/uptime_sensor.cpp +++ b/src/esphomelib/sensor/uptime_sensor.cpp @@ -13,7 +13,7 @@ static const char *TAG = "sensor.uptime"; UptimeSensor::UptimeSensor(const std::string &name, uint32_t update_interval) : PollingSensorComponent(name, update_interval) { - this->clear_filters(); + } void UptimeSensor::update() { const uint32_t ms = millis(); diff --git a/src/esphomelib/sensor/wifi_signal_sensor.cpp b/src/esphomelib/sensor/wifi_signal_sensor.cpp index ad8f4549..3056d23c 100644 --- a/src/esphomelib/sensor/wifi_signal_sensor.cpp +++ b/src/esphomelib/sensor/wifi_signal_sensor.cpp @@ -8,11 +8,14 @@ #else #include #endif +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace sensor { +static const char *TAG = "sensor.wifi_signal"; + WiFiSignalSensor::WiFiSignalSensor(const std::string &name, uint32_t update_interval) : PollingSensorComponent(name, update_interval) { @@ -35,6 +38,9 @@ std::string WiFiSignalSensor::unique_id() { float WiFiSignalSensor::get_setup_priority() const { return setup_priority::WIFI; } +void WiFiSignalSensor::dump_config() { + LOG_SENSOR("", "WiFi Signal", this); +} } // namespace sensor diff --git a/src/esphomelib/sensor/wifi_signal_sensor.h b/src/esphomelib/sensor/wifi_signal_sensor.h index c9ff1fcc..200c0dd5 100644 --- a/src/esphomelib/sensor/wifi_signal_sensor.h +++ b/src/esphomelib/sensor/wifi_signal_sensor.h @@ -16,6 +16,7 @@ class WiFiSignalSensor : public PollingSensorComponent { explicit WiFiSignalSensor(const std::string &name, uint32_t update_interval = 15000); void update() override; + void dump_config() override; std::string unit_of_measurement() override; std::string icon() override; diff --git a/src/esphomelib/switch_/custom_switch.cpp b/src/esphomelib/switch_/custom_switch.cpp index 374723a6..38562fc3 100644 --- a/src/esphomelib/switch_/custom_switch.cpp +++ b/src/esphomelib/switch_/custom_switch.cpp @@ -3,17 +3,26 @@ #ifdef USE_CUSTOM_SWITCH #include "esphomelib/switch_/custom_switch.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { +static const char *TAG = "switch.custom"; + CustomSwitchConstructor::CustomSwitchConstructor(std::function()> init) { this->switches_ = init(); } Switch *CustomSwitchConstructor::get_switch(int i) { return this->switches_[i]; } +void CustomSwitchConstructor::dump_config() { + for (auto *child : this->switches_) { + LOG_SWITCH("", "Custom Switch", child); + } +} + } // namespace switch_ ESPHOMELIB_NAMESPACE_END diff --git a/src/esphomelib/switch_/custom_switch.h b/src/esphomelib/switch_/custom_switch.h index 321ca77e..b236a061 100644 --- a/src/esphomelib/switch_/custom_switch.h +++ b/src/esphomelib/switch_/custom_switch.h @@ -6,16 +6,19 @@ #ifdef USE_CUSTOM_SWITCH #include "esphomelib/switch_/switch.h" +#include "esphomelib/component.h" ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { -class CustomSwitchConstructor { +class CustomSwitchConstructor : public Component { public: CustomSwitchConstructor(std::function()> init); Switch *get_switch(int i); + + void dump_config() override; protected: std::vector switches_; }; diff --git a/src/esphomelib/switch_/gpio_switch.cpp b/src/esphomelib/switch_/gpio_switch.cpp index d0247538..0421a957 100644 --- a/src/esphomelib/switch_/gpio_switch.cpp +++ b/src/esphomelib/switch_/gpio_switch.cpp @@ -31,9 +31,8 @@ void GPIOSwitch::setup() { } } void GPIOSwitch::dump_config() { - ESP_LOGCONFIG(TAG, "GPIO Switch '%s':", this->name_.c_str()); + LOG_SWITCH("", "GPIO Switch", this); LOG_PIN(" Pin: ", this->pin_); - LOG_SWITCH(this); } void GPIOSwitch::write_state(bool state) { this->pin_->digital_write(state); diff --git a/src/esphomelib/switch_/mqtt_switch_component.cpp b/src/esphomelib/switch_/mqtt_switch_component.cpp index 040a5024..597c74a3 100644 --- a/src/esphomelib/switch_/mqtt_switch_component.cpp +++ b/src/esphomelib/switch_/mqtt_switch_component.cpp @@ -46,12 +46,6 @@ void MQTTSwitchComponent::setup() { } void MQTTSwitchComponent::dump_config() { ESP_LOGCONFIG(TAG, "MQTT switch '%s': ", this->switch_->get_name().c_str()); - if (!this->switch_->get_icon().empty()) { - ESP_LOGCONFIG(TAG, " Icon: '%s'", this->switch_->get_icon().c_str()); - } - if (this->switch_->optimistic()) { - ESP_LOGCONFIG(TAG, " Optimistic: YES"); - } LOG_MQTT_COMPONENT(true, true); } diff --git a/src/esphomelib/switch_/output_switch.cpp b/src/esphomelib/switch_/output_switch.cpp index 2a7a88a0..681179ec 100644 --- a/src/esphomelib/switch_/output_switch.cpp +++ b/src/esphomelib/switch_/output_switch.cpp @@ -3,13 +3,14 @@ #ifdef USE_OUTPUT_SWITCH #include "esphomelib/switch_/output_switch.h" - #include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { +static const char *TAG = "switch.output"; + OutputSwitch::OutputSwitch(const std::string &name, output::BinaryOutput *output) : Switch(name), output_(output) { @@ -36,6 +37,9 @@ void OutputSwitch::setup() { float OutputSwitch::get_setup_priority() const { return setup_priority::HARDWARE; } +void OutputSwitch::dump_config() { + LOG_SWITCH("", "Output Switch", this); +} } // namespace switch_ diff --git a/src/esphomelib/switch_/output_switch.h b/src/esphomelib/switch_/output_switch.h index ae8e5cfd..85f06d1f 100644 --- a/src/esphomelib/switch_/output_switch.h +++ b/src/esphomelib/switch_/output_switch.h @@ -23,6 +23,7 @@ class OutputSwitch : public Switch, public Component { void setup() override; float get_setup_priority() const override; + void dump_config() override; protected: void write_state(bool state) override; diff --git a/src/esphomelib/switch_/restart_switch.cpp b/src/esphomelib/switch_/restart_switch.cpp index 37af0c28..5e94e1bd 100644 --- a/src/esphomelib/switch_/restart_switch.cpp +++ b/src/esphomelib/switch_/restart_switch.cpp @@ -30,6 +30,9 @@ void RestartSwitch::write_state(bool state) { safe_reboot("restart"); } } +void RestartSwitch::dump_config() { + LOG_SWITCH("", "Restart Switch", this); +} } // namespace switch_ diff --git a/src/esphomelib/switch_/restart_switch.h b/src/esphomelib/switch_/restart_switch.h index f421270d..3b33a813 100644 --- a/src/esphomelib/switch_/restart_switch.h +++ b/src/esphomelib/switch_/restart_switch.h @@ -11,11 +11,14 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { /// A simple switch that restarts the device when triggered. -class RestartSwitch : public Switch { +class RestartSwitch : public Switch, public Component { public: explicit RestartSwitch(const std::string &name); std::string icon() override; + + void dump_config() override; + protected: void write_state(bool state) override; }; diff --git a/src/esphomelib/switch_/shutdown_switch.cpp b/src/esphomelib/switch_/shutdown_switch.cpp index 8900e3ce..fd2efe8d 100644 --- a/src/esphomelib/switch_/shutdown_switch.cpp +++ b/src/esphomelib/switch_/shutdown_switch.cpp @@ -35,6 +35,9 @@ void ShutdownSwitch::write_state(bool state) { #endif } } +void ShutdownSwitch::dump_config() { + LOG_SWITCH("", "Shutdown Switch", this); +} } // namespace switch_ diff --git a/src/esphomelib/switch_/shutdown_switch.h b/src/esphomelib/switch_/shutdown_switch.h index 023ee91d..c666f5da 100644 --- a/src/esphomelib/switch_/shutdown_switch.h +++ b/src/esphomelib/switch_/shutdown_switch.h @@ -11,11 +11,13 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { /// A simple switch that will put the node into deep sleep indefinitely. -class ShutdownSwitch : public Switch { +class ShutdownSwitch : public Switch, public Component { public: explicit ShutdownSwitch(const std::string &name); std::string icon() override; + + void dump_config() override; protected: void write_state(bool state) override; }; diff --git a/src/esphomelib/switch_/switch.cpp b/src/esphomelib/switch_/switch.cpp index b2766274..ade43eb1 100644 --- a/src/esphomelib/switch_/switch.cpp +++ b/src/esphomelib/switch_/switch.cpp @@ -68,6 +68,9 @@ void Switch::set_inverted(bool inverted) { uint32_t Switch::hash_base_() { return 3129890955UL; } +bool Switch::is_inverted() const { + return this->inverted_; +} } // namespace switch_ diff --git a/src/esphomelib/switch_/switch.h b/src/esphomelib/switch_/switch.h index 842690f5..4c6226dd 100644 --- a/src/esphomelib/switch_/switch.h +++ b/src/esphomelib/switch_/switch.h @@ -23,8 +23,19 @@ class TurnOnAction; template class SwitchCondition; -#define LOG_SWITCH(this) \ - if (this->inverted_) { ESP_LOGCONFIG(TAG, " Inverted: YES"); } +#define LOG_SWITCH(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \ + if (!obj->get_icon().empty()) { \ + ESP_LOGCONFIG(TAG, prefix " Icon: '%s'", obj->get_icon().c_str()); \ + } \ + if (obj->optimistic()) { \ + ESP_LOGCONFIG(TAG, prefix " Optimistic: YES"); \ + } \ + if (obj->is_inverted()) { \ + ESP_LOGCONFIG(TAG, prefix " Inverted: YES"); \ + } \ + } /** Base class for all switches. * @@ -109,6 +120,8 @@ class Switch : public Nameable { */ virtual bool optimistic(); + bool is_inverted() const; + protected: /** Write the given state to hardware. You should implement this * abstract method if you want to create your own switch. diff --git a/src/esphomelib/switch_/template_switch.cpp b/src/esphomelib/switch_/template_switch.cpp index 538743a5..fe2f63dd 100644 --- a/src/esphomelib/switch_/template_switch.cpp +++ b/src/esphomelib/switch_/template_switch.cpp @@ -72,8 +72,8 @@ void TemplateSwitch::setup() { } } void TemplateSwitch::dump_config() { - ESP_LOGCONFIG(TAG, "Template Switch '%s':", this->name_.c_str()); - LOG_SWITCH(this); + LOG_SWITCH("", "Template Switch", this); + ESP_LOGCONFIG(TAG, " Restore State: %s", YESNO(this->restore_state_)); } void TemplateSwitch::set_restore_state(bool restore_state) { this->restore_state_ = restore_state; diff --git a/src/esphomelib/switch_/uart_switch.cpp b/src/esphomelib/switch_/uart_switch.cpp index 38e00f9a..58eb5ab0 100644 --- a/src/esphomelib/switch_/uart_switch.cpp +++ b/src/esphomelib/switch_/uart_switch.cpp @@ -27,6 +27,9 @@ void UARTSwitch::write_state(bool state) { this->write_array(this->data_.data(), this->data_.size()); this->publish_state(false); } +void UARTSwitch::dump_config() { + LOG_SWITCH("", "UART Switch", this); +} } // namespace switch_ diff --git a/src/esphomelib/switch_/uart_switch.h b/src/esphomelib/switch_/uart_switch.h index 20e16f17..e24e7603 100644 --- a/src/esphomelib/switch_/uart_switch.h +++ b/src/esphomelib/switch_/uart_switch.h @@ -12,10 +12,12 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { -class UARTSwitch : public Switch, public UARTDevice { +class UARTSwitch : public Switch, public UARTDevice, public Component { public: UARTSwitch(UARTComponent *parent, const std::string &name, const std::vector &data); + void dump_config() override; + protected: void write_state(bool state) override; std::vector data_; diff --git a/src/esphomelib/text_sensor/custom_text_sensor.cpp b/src/esphomelib/text_sensor/custom_text_sensor.cpp index 07439772..c7020597 100644 --- a/src/esphomelib/text_sensor/custom_text_sensor.cpp +++ b/src/esphomelib/text_sensor/custom_text_sensor.cpp @@ -3,17 +3,26 @@ #ifdef USE_CUSTOM_TEXT_SENSOR #include "esphomelib/text_sensor/custom_text_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace text_sensor { +static const char *TAG = "text_sensor.custom"; + CustomTextSensorConstructor::CustomTextSensorConstructor(std::function()> init) { this->text_sensors_ = init(); } TextSensor *CustomTextSensorConstructor::get_text_sensor(int i) { return this->text_sensors_[i]; } +void CustomTextSensorConstructor::dump_config() { + for (auto *child : this->text_sensors_) { + LOG_TEXT_SENSOR("", "Custom Text Sensor", child); + } +} + } // namespace text_sensor ESPHOMELIB_NAMESPACE_END diff --git a/src/esphomelib/text_sensor/custom_text_sensor.h b/src/esphomelib/text_sensor/custom_text_sensor.h index bdca192b..fe51d723 100644 --- a/src/esphomelib/text_sensor/custom_text_sensor.h +++ b/src/esphomelib/text_sensor/custom_text_sensor.h @@ -6,17 +6,20 @@ #ifdef USE_CUSTOM_TEXT_SENSOR #include "esphomelib/text_sensor/text_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace text_sensor { -class CustomTextSensorConstructor { +class CustomTextSensorConstructor : public Component { public: CustomTextSensorConstructor(std::function()> init); TextSensor *get_text_sensor(int i); + void dump_config() override; + protected: std::vector text_sensors_; }; diff --git a/src/esphomelib/text_sensor/homeassistant_text_sensor.cpp b/src/esphomelib/text_sensor/homeassistant_text_sensor.cpp index f1056d67..7aa4e870 100644 --- a/src/esphomelib/text_sensor/homeassistant_text_sensor.cpp +++ b/src/esphomelib/text_sensor/homeassistant_text_sensor.cpp @@ -14,7 +14,14 @@ static const char *TAG = "text_sensor.homeassistant"; HomeassistantTextSensor::HomeassistantTextSensor(const std::string &name, const std::string &entity_id) : TextSensor(name), entity_id_(entity_id) { - api::global_api_server->subscribe_home_assistant_state(entity_id, [this](std::string state) { + +} +void HomeassistantTextSensor::dump_config() { + LOG_TEXT_SENSOR("", "Homeassistant Text Sensor", this); + ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str()); +} +void HomeassistantTextSensor::setup() { + api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) { ESP_LOGD(TAG, "'%s': Got state '%s'", this->entity_id_.c_str(), state.c_str()); this->publish_state(state); }); diff --git a/src/esphomelib/text_sensor/homeassistant_text_sensor.h b/src/esphomelib/text_sensor/homeassistant_text_sensor.h index 19245033..473e8170 100644 --- a/src/esphomelib/text_sensor/homeassistant_text_sensor.h +++ b/src/esphomelib/text_sensor/homeassistant_text_sensor.h @@ -12,9 +12,11 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace text_sensor { -class HomeassistantTextSensor : public TextSensor { +class HomeassistantTextSensor : public TextSensor, public Component { public: HomeassistantTextSensor(const std::string &name, const std::string &entity_id); + void dump_config() override; + void setup() override; protected: std::string entity_id_; }; diff --git a/src/esphomelib/text_sensor/mqtt_subscribe_text_sensor.cpp b/src/esphomelib/text_sensor/mqtt_subscribe_text_sensor.cpp index 903a4079..42a3fe55 100644 --- a/src/esphomelib/text_sensor/mqtt_subscribe_text_sensor.cpp +++ b/src/esphomelib/text_sensor/mqtt_subscribe_text_sensor.cpp @@ -27,7 +27,7 @@ void MQTTSubscribeTextSensor::set_qos(uint8_t qos) { this->qos_ = qos; } void MQTTSubscribeTextSensor::dump_config() { - ESP_LOGCONFIG(TAG, "MQTT Subscribe Text Sensor '%s':", this->name_.c_str()); + LOG_TEXT_SENSOR("", "MQTT Subscribe Text Sensor", this); ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str()); } diff --git a/src/esphomelib/text_sensor/mqtt_text_sensor.cpp b/src/esphomelib/text_sensor/mqtt_text_sensor.cpp index 5398b450..3654e4e9 100644 --- a/src/esphomelib/text_sensor/mqtt_text_sensor.cpp +++ b/src/esphomelib/text_sensor/mqtt_text_sensor.cpp @@ -32,12 +32,6 @@ void MQTTTextSensor::setup() { void MQTTTextSensor::dump_config() { ESP_LOGCONFIG(TAG, "MQTT Text Sensor '%s':", this->sensor_->get_name().c_str()); - if (!this->sensor_->get_icon().empty()) { - ESP_LOGCONFIG(TAG, " Icon: '%s'", this->sensor_->get_icon().c_str()); - } - if (!this->sensor_->unique_id().empty()) { - ESP_LOGCONFIG(TAG, " Unique ID: '%s'", this->sensor_->unique_id().c_str()); - } LOG_MQTT_COMPONENT(true, false); } diff --git a/src/esphomelib/text_sensor/template_text_sensor.cpp b/src/esphomelib/text_sensor/template_text_sensor.cpp index 27990cd0..3ee8b265 100644 --- a/src/esphomelib/text_sensor/template_text_sensor.cpp +++ b/src/esphomelib/text_sensor/template_text_sensor.cpp @@ -3,11 +3,14 @@ #ifdef USE_TEMPLATE_TEXT_SENSOR #include "esphomelib/text_sensor/template_text_sensor.h" +#include "esphomelib/log.h" ESPHOMELIB_NAMESPACE_BEGIN namespace text_sensor { +static const char *TAG = "text_sensor.template"; + TemplateTextSensor::TemplateTextSensor(const std::string &name, uint32_t update_interval) : TextSensor(name), PollingComponent(update_interval) { @@ -24,6 +27,9 @@ float TemplateTextSensor::get_setup_priority() const { void TemplateTextSensor::set_template(std::function()> &&f) { this->f_ = std::move(f); } +void TemplateTextSensor::dump_config() { + LOG_TEXT_SENSOR("", "Template Sensor", this); +} } // namespace text_sensor diff --git a/src/esphomelib/text_sensor/template_text_sensor.h b/src/esphomelib/text_sensor/template_text_sensor.h index c1db376b..66c7b422 100644 --- a/src/esphomelib/text_sensor/template_text_sensor.h +++ b/src/esphomelib/text_sensor/template_text_sensor.h @@ -22,6 +22,8 @@ class TemplateTextSensor : public TextSensor, public PollingComponent { float get_setup_priority() const override; + void dump_config() override; + protected: std::function()> f_{}; }; diff --git a/src/esphomelib/text_sensor/text_sensor.h b/src/esphomelib/text_sensor/text_sensor.h index 4f7841a4..e0ec63db 100644 --- a/src/esphomelib/text_sensor/text_sensor.h +++ b/src/esphomelib/text_sensor/text_sensor.h @@ -15,6 +15,17 @@ namespace text_sensor { class TextSensorStateTrigger; +#define LOG_TEXT_SENSOR(prefix, type, obj) \ + if (obj != nullptr) { \ + ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \ + if (!obj->get_icon().empty()) { \ + ESP_LOGCONFIG(TAG, prefix " Icon: '%s'", obj->get_icon().c_str()); \ + } \ + if (!obj->unique_id().empty()) { \ + ESP_LOGV(TAG, prefix " Unique ID: '%s'", obj->unique_id().c_str()); \ + } \ + } + class TextSensor : public Nameable { public: explicit TextSensor(const std::string &name) : Nameable(name) {} diff --git a/src/esphomelib/text_sensor/version_text_sensor.cpp b/src/esphomelib/text_sensor/version_text_sensor.cpp index d9bb0c7e..303ca2a2 100644 --- a/src/esphomelib/text_sensor/version_text_sensor.cpp +++ b/src/esphomelib/text_sensor/version_text_sensor.cpp @@ -3,12 +3,15 @@ #ifdef USE_VERSION_TEXT_SENSOR #include "esphomelib/text_sensor/version_text_sensor.h" +#include "esphomelib/log.h" #include "esphomelib/application.h" ESPHOMELIB_NAMESPACE_BEGIN namespace text_sensor { +static const char *TAG = "text_sensor.version"; + void VersionTextSensor::setup() { if (App.get_compilation_time().empty()) { this->publish_state(ESPHOMELIB_VERSION); @@ -27,6 +30,9 @@ std::string VersionTextSensor::icon() { std::string VersionTextSensor::unique_id() { return get_mac_address() + "-version"; } +void VersionTextSensor::dump_config() { + LOG_TEXT_SENSOR("", "Version Text Sensor", this); +} } // namespace text_sensor diff --git a/src/esphomelib/text_sensor/version_text_sensor.h b/src/esphomelib/text_sensor/version_text_sensor.h index caf4cb59..6a40679d 100644 --- a/src/esphomelib/text_sensor/version_text_sensor.h +++ b/src/esphomelib/text_sensor/version_text_sensor.h @@ -16,6 +16,7 @@ class VersionTextSensor : public TextSensor, public Component { public: explicit VersionTextSensor(const std::string &name); void setup() override; + void dump_config() override; float get_setup_priority() const override; std::string icon() override; std::string unique_id() override; diff --git a/src/esphomelib/wifi_component.cpp b/src/esphomelib/wifi_component.cpp index aea6e310..bcb47d5f 100644 --- a/src/esphomelib/wifi_component.cpp +++ b/src/esphomelib/wifi_component.cpp @@ -343,7 +343,7 @@ void WiFiComponent::check_connecting_finished() { } uint32_t now = millis(); - if (now - this->action_started_ > 60000) { + if (now - this->action_started_ > 30000) { ESP_LOGW(TAG, "Timeout while connecting to WiFi."); this->retry_connect(); return; @@ -668,6 +668,8 @@ wl_status_t WiFiComponent::wifi_sta_status_() { } } bool WiFiComponent::wifi_scan_start_() { + static bool first_scan = false; + // enable STA if (!this->wifi_mode_(true, {})) return false; @@ -685,8 +687,14 @@ bool WiFiComponent::wifi_scan_start_() { config.channel = 0; config.show_hidden = 1; config.scan_type = WIFI_SCAN_TYPE_ACTIVE; - config.scan_time.active.min = 100; - config.scan_time.active.max = 200; + if (first_scan) { + config.scan_time.active.min = 100; + config.scan_time.active.max = 200; + } else { + config.scan_time.active.min = 400; + config.scan_time.active.max = 500; + } + first_scan = false; bool ret = wifi_station_scan(&config, &WiFiComponent::s_wifi_scan_done_callback_); if (!ret) { ESP_LOGV(TAG, "wifi_station_scan failed!"); @@ -920,15 +928,14 @@ bool WiFiComponent::wifi_sta_ip_config_(optional manual_ip) { info.gw.addr = static_cast(manual_ip->gateway); info.netmask.addr = static_cast(manual_ip->subnet); - if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED) { - esp_err_t dhcp_stop_ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);; - if (dhcp_stop_ret != ESP_OK) { - ESP_LOGV(TAG, "Stopping DHCP client failed! %d", dhcp_stop_ret); - } + esp_err_t dhcp_stop_ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); + if (dhcp_stop_ret != ESP_OK) { + ESP_LOGV(TAG, "Stopping DHCP client failed! %d", dhcp_stop_ret); } + esp_err_t wifi_set_info_ret = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info); if (wifi_set_info_ret != ESP_OK) { - ESP_LOGV(TAG, "Setting manual IP info failed! %d", wifi_set_info_ret); + ESP_LOGV(TAG, "Setting manual IP info failed! %s", esp_err_to_name(wifi_set_info_ret)); } ip_addr_t dns; @@ -1131,12 +1138,10 @@ bool WiFiComponent::wifi_ap_ip_config_(optional manual_ip) { } tcpip_adapter_dhcp_status_t dhcp_status; tcpip_adapter_dhcps_get_status(TCPIP_ADAPTER_IF_AP, &dhcp_status); - if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED) { - err = tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); - if (err != ESP_OK) { - ESP_LOGV(TAG, "tcpip_adapter_dhcps_stop failed! %d", err); - return false; - } + err = tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); + if (err != ESP_OK) { + ESP_LOGV(TAG, "tcpip_adapter_dhcps_stop failed! %d", err); + return false; } err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info);