Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert custom component to external component #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
__pycache__
.idea/
secrets.yaml
.esphome/
**/.pioenvs/
**/.piolibdeps/
**/lib/
**/src/
**/partitions.csv

__pycache__/
*.py[cod]
*$py.class

*~
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ InfluxDB database with UDP.

## Installation

Clone this repository into `custom_components/influxdb` from the directory
where your ESPHome configuration is stored.

```bash
git clone https://github.com/la7dja/esphome-influxdb custom_components/influxdb
```

## Installation

You can install this component with [ESPHome external components feature](https://esphome.io/components/external_components.html) like this:

```yaml
external_components:
- source: github://la7dja/esphome-influxdb@master
```

## Usage

Add `influxdb` section to your ESPHome configuration file.
Expand Down
6 changes: 3 additions & 3 deletions __init__.py → components/influxdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def to_code(config):
measurement = f"\"{sensor_config[CONF_MEASUREMENT]}\""
else:
measurement = f"{sensor_id}->get_object_id()"
cg.add(var.add_setup_callback(cg.RawExpression(f"[]() -> Nameable* {{ {sensor_id}->add_on_state_callback([](float state) {{ {config[CONF_ID]}->on_sensor_update({sensor_id}, {measurement}, \"{tags}\", state); }}); return {sensor_id}; }}")))
cg.add(var.add_setup_callback(cg.RawExpression(f"[]() -> EntityBase* {{ {sensor_id}->add_on_state_callback([](float state) {{ {config[CONF_ID]}->on_sensor_update({sensor_id}, {measurement}, \"{tags}\", state); }}); return {sensor_id}; }}")))
else:
cg.add(var.add_setup_callback(cg.RawExpression(f"[]() -> Nameable* {{ return {sensor_id}; }}")))
cg.add(var.add_setup_callback(cg.RawExpression(f"[]() -> EntityBase* {{ return {sensor_id}; }}")))

cg.add_define('USE_INFLUXDB')
cg.add_global(influxdb_ns.using)
10 changes: 5 additions & 5 deletions influxdb_writer.cpp → components/influxdb/influxdb_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ static const char *TAG = "influxdb";

void InfluxDBWriter::setup() {
ESP_LOGCONFIG(TAG, "Setting up InfluxDB Writer...");
std::vector<Nameable*> objs;
std::vector<EntityBase*> objs;
for(auto fun: setup_callbacks)
objs.push_back(fun());

if (publish_all) {
#ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors()) {
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](Nameable *o) {return o == obj;}))
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](EntityBase *o) {return o == obj;}))
obj->add_on_state_callback([this, obj](bool state) { this->on_sensor_update(obj, obj->get_object_id(), tags, state); });
}
#endif
#ifdef USE_SENSOR
for (auto *obj : App.get_sensors()) {
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](Nameable *o) {return o == obj;}))
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](EntityBase *o) {return o == obj;}))
obj->add_on_state_callback([this, obj](float state) { this->on_sensor_update(obj, obj->get_object_id(), tags, state); });
}
#endif
#ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors()) {
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](Nameable *o) {return o == obj;}))
if (!obj->is_internal() && std::none_of(objs.begin(), objs.end(), [&obj](EntityBase *o) {return o == obj;}))
obj->add_on_state_callback([this, obj](std::string state) { this->on_sensor_update(obj, obj->get_object_id(), tags, state); });
}
#endif
Expand Down Expand Up @@ -68,7 +68,7 @@ void InfluxDBWriter::write(std::string measurement, std::string tags, const std:
} else
udp.write('\n');

udp.write(line.data(), line.size());
udp.write((const uint8_t*)line.data(), line.size());
packet_size += line.size();
}

Expand Down
5 changes: 3 additions & 2 deletions influxdb_writer.h → components/influxdb/influxdb_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "esphome/core/controller.h"
#include "esphome/core/defines.h"
#include "esphome/core/log.h"
#include "esphome/core/entity_base.h"

namespace esphome {
namespace influxdb {
Expand Down Expand Up @@ -39,7 +40,7 @@ class InfluxDBWriter : public Component {
void set_max_packet_size(int size) { max_packet_size = size; };
void set_tags(std::string tags) { this->tags = tags; };
void set_publish_all(bool all) { publish_all = all; };
void add_setup_callback(std::function<Nameable*()> fun) { setup_callbacks.push_back(fun); };
void add_setup_callback(std::function<EntityBase*()> fun) { setup_callbacks.push_back(fun); };

protected:
void write(std::string measurement, std::string tags, const std::string value, bool is_string);
Expand All @@ -51,7 +52,7 @@ class InfluxDBWriter : public Component {
std::string tags;
bool publish_all;

std::vector<std::function<Nameable*()>> setup_callbacks;
std::vector<std::function<EntityBase *()>> setup_callbacks;

WiFiUDP udp;
int packet_size;
Expand Down