Skip to content

Commit

Permalink
Fix some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter committed Dec 24, 2018
1 parent 7989025 commit f791e5b
Show file tree
Hide file tree
Showing 112 changed files with 524 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/esphomelib/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ message SubscribeLogsResponse {
LogLevel level = 1;
string tag = 2;
string message = 3;
bool send_failed = 4;
}

message SubscribeServiceCallsRequest {
Expand Down
32 changes: 19 additions & 13 deletions src/esphomelib/api/api_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ APIConnection::APIConnection(AsyncClient *client, APIServer *parent)
((APIConnection *) s)->on_data_(reinterpret_cast<uint8_t *>(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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<char *>(header_raw), header_buffer.get_length());
Expand Down Expand Up @@ -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<uint32_t>(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;
Expand Down Expand Up @@ -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()) {
Expand Down
19 changes: 12 additions & 7 deletions src/esphomelib/api/api_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class APIConnection {
void disconnect_client_();
bool send_buffer(APIMessageType type, APIBuffer &buf);
template<typename T>
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();

Expand Down Expand Up @@ -242,10 +241,16 @@ void HomeAssistantServiceCallAction<T>::play(T x) {
}

template<typename T>
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
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/api/list_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/esphomelib/api/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(value));
}
void APIBuffer::encode_bool(uint32_t field, bool value) {
if (!value)
return;
Expand Down
1 change: 1 addition & 0 deletions src/esphomelib/api/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 22 additions & 4 deletions src/esphomelib/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_),
Expand All @@ -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_),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<std::vector<BinarySensor *>()> &init) {
return this->register_component(new CustomBinarySensorConstructor(init));
}
#endif

#ifdef USE_CUSTOM_SENSOR
sensor::CustomSensorConstructor *Application::make_custom_sensor(const std::function<std::vector<Sensor *>()> &init) {
return this->register_component(new CustomSensorConstructor(init));
}
#endif

#ifdef USE_CUSTOM_SWITCH
switch_::CustomSwitchConstructor *Application::make_custom_switch(const std::function<std::vector<Switch *>()> &init) {
return this->register_component(new CustomSwitchConstructor(init));
}
#endif

Application App; // NOLINT

ESPHOMELIB_NAMESPACE_END
13 changes: 13 additions & 0 deletions src/esphomelib/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<binary_sensor::BinarySensor *>()> &init);
#endif




Expand Down Expand Up @@ -1010,6 +1014,11 @@ class Application {
#endif


#ifdef USE_CUSTOM_SENSOR
sensor::CustomSensorConstructor *make_custom_sensor(const std::function<std::vector<sensor::Sensor *>()> &init);
#endif





Expand Down Expand Up @@ -1258,6 +1267,10 @@ class Application {
MakeUARTSwitch make_uart_switch(UARTComponent *parent, const std::string &name, const std::vector<uint8_t> &data);
#endif

#ifdef USE_CUSTOM_SWITCH
switch_::CustomSwitchConstructor *make_custom_switch(const std::function<std::vector<switch_::Switch *>()> &init);
#endif




Expand Down
8 changes: 8 additions & 0 deletions src/esphomelib/binary_sensor/binary_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/esphomelib/binary_sensor/custom_binary_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<BinarySensor *>()> &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

Expand Down
5 changes: 4 additions & 1 deletion src/esphomelib/binary_sensor/custom_binary_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<BinarySensor *>()> &init);

BinarySensor *get_binary_sensor(int i);

void dump_config() override;

protected:
std::vector<BinarySensor *> binary_sensors_;
};
Expand Down
4 changes: 2 additions & 2 deletions src/esphomelib/binary_sensor/esp32_touch_binary_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

Expand Down
3 changes: 0 additions & 3 deletions src/esphomelib/binary_sensor/mqtt_binary_sensor_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/esphomelib/binary_sensor/pn532_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> &uid, uint32_t update_interval)
Expand Down
6 changes: 6 additions & 0 deletions src/esphomelib/binary_sensor/status_binary_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/esphomelib/binary_sensor/status_binary_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit f791e5b

Please sign in to comment.